diff options
author | Sam Light <samlight1994@gmail.com> | 2025-03-30 14:20:57 +0100 |
---|---|---|
committer | Sam Light <samlight1994@gmail.com> | 2025-03-30 14:20:57 +0100 |
commit | e744b8d67ef1d18050158dd523ba7d804c1c8528 (patch) | |
tree | 56c90a83912bd63bdd507e76e5ef00c82a904b50 /src/Toolbar | |
parent | a883170dd1c723bcd02916f9bc8a96ed85a61761 (diff) |
Filters, sorting, escaping and more....
Diffstat (limited to 'src/Toolbar')
-rw-r--r-- | src/Toolbar/ColumnSelect.php | 4 | ||||
-rw-r--r-- | src/Toolbar/Filter.php | 26 | ||||
-rw-r--r-- | src/Toolbar/Item.php | 3 | ||||
-rw-r--r-- | src/Toolbar/SelectFilter.php | 35 |
4 files changed, 66 insertions, 2 deletions
diff --git a/src/Toolbar/ColumnSelect.php b/src/Toolbar/ColumnSelect.php index a70c49a..9380650 100644 --- a/src/Toolbar/ColumnSelect.php +++ b/src/Toolbar/ColumnSelect.php @@ -11,7 +11,9 @@ class ColumnSelect extends Item public function render(): View { return view('laralight-tables::toolbar.column-select', [ - 'allColumns' => $this->getTable()->getColumns() + 'allColumns' => $this->getTable() + ->getColumns() + ->filter(fn($c) => $c->getShowInSelect()) ]); } } 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); + } + } } } diff --git a/src/Toolbar/Item.php b/src/Toolbar/Item.php index 168f877..e91698f 100644 --- a/src/Toolbar/Item.php +++ b/src/Toolbar/Item.php @@ -4,12 +4,15 @@ namespace Lightscale\LaralightTables\Toolbar; use Lightscale\LaralightTables\TableComponent; use Lightscale\LaralightTables\Toolbar; +use Lightscale\LaralightTables\Concerns\Makable; use Illuminate\View\View; use Illuminate\Support\HtmlString; abstract class Item { + use Makable; + private Toolbar $toolbar; public function setToolbar(Toolbar $toolbar): void diff --git a/src/Toolbar/SelectFilter.php b/src/Toolbar/SelectFilter.php new file mode 100644 index 0000000..0549885 --- /dev/null +++ b/src/Toolbar/SelectFilter.php @@ -0,0 +1,35 @@ +<?php + +namespace Lightscale\LaralightTables\Toolbar; + +use Illuminate\View\View; + +class SelectFilter extends Filter +{ + protected iterable $options; + protected ?string $placeholder = null; + + public function placeholder(?string $v): static + { + $this->placeholder = $v; + return $this; + } + + public function options(iterable $options): static + { + $this->options = $options; + return $this; + } + + public function render(): View + { + return view('laralight-tables::toolbar.select-filter', [ + 'id' => $this->makeId(), + 'key' => $this->key, + 'label' => $this->label, + 'options' => $this->options, + 'placeholder' => $this->placeholder, + ]); + } + +} |