summaryrefslogtreecommitdiff
path: root/src/Toolbar/Filter.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Toolbar/Filter.php')
-rw-r--r--src/Toolbar/Filter.php26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/Toolbar/Filter.php b/src/Toolbar/Filter.php
index 19f29cc..51e0d9a 100644
--- a/src/Toolbar/Filter.php
+++ b/src/Toolbar/Filter.php
@@ -2,12 +2,30 @@
namespace Lightscale\LaralightTables\Toolbar;
+use Illuminate\Database\Eloquent\Builder;
+
use Closure;
abstract class Filter extends Item
{
protected ?Closure $filterCallback = null;
+ public function __construct(
+ protected string $key,
+ protected ?string $label = null,
+ ) {}
+
+ public function label(string $v): static
+ {
+ $this->label = $v;
+ return $this;
+ }
+
+ public function makeId(): string
+ {
+ return "filter_{$this->key}";
+ }
+
public function filter(callable $filterCB): static
{
$this->filterCallback = $filterCB;
@@ -16,7 +34,13 @@ abstract class Filter extends Item
public function applyFilter(Builder $query): void
{
- ($this->filterCallback)($query, $value);
+ if ($this->filterCallback !== null) {
+ $value = $this->getTable()->filters[$this->key] ?? null;
+
+ if (!empty($value)) {
+ ($this->filterCallback)($query, $value);
+ }
+ }
}
}