diff options
author | Sam Light <samlight1994@gmail.com> | 2025-04-02 23:18:08 +0100 |
---|---|---|
committer | Sam Light <samlight1994@gmail.com> | 2025-04-02 23:18:08 +0100 |
commit | 40607777a5752523f6750312675db09665726a5f (patch) | |
tree | 61004cf8148cb9e57bec12a5e92a2b352fc1528b | |
parent | 350dc0827e0ff9e4f47c2df23187a35e8a8cf58b (diff) |
Switch to allow multiple toolbars
-rw-r--r-- | resources/views/table.blade.php | 4 | ||||
-rw-r--r-- | src/TableComponent.php | 27 | ||||
-rw-r--r-- | workbench/app/Livewire/ProductsTable.php | 14 | ||||
-rw-r--r-- | workbench/app/Livewire/Table.php | 5 |
4 files changed, 27 insertions, 23 deletions
diff --git a/resources/views/table.blade.php b/resources/views/table.blade.php index ec2cd25..8812b0c 100644 --- a/resources/views/table.blade.php +++ b/resources/views/table.blade.php @@ -1,5 +1,7 @@ <div> - {{ $toolbar?->render() }} + @foreach($toolbars as $toolbar) + {{ $toolbar?->render() }} + @endforeach <table class="table"> <colgroup> @foreach($columns as $column) diff --git a/src/TableComponent.php b/src/TableComponent.php index 7186b1f..14d631b 100644 --- a/src/TableComponent.php +++ b/src/TableComponent.php @@ -68,7 +68,10 @@ abstract class TableComponent extends Component protected function isSearching(): bool { - $search = $this->getToolbar()?->getSearch(); + $search = $this + ->getToolbars() + ->first(fn(Toolbar $tb) => $tb->getSearch() !== null) + ?->getSearch(); return $search !== null && Str::length($this->search) >= $search->getMinLength(); } @@ -101,19 +104,19 @@ abstract class TableComponent extends Component } } - protected function toolbar(): ?Toolbar + protected function toolbars(): array { - return null; + return []; } - private ?Toolbar $toolbarCache; + private Collection $toolbarsCache; - protected function getToolbar(): ?Toolbar + protected function getToolbars(): Collection { - if (!isset($this->_toolbar)) { - $this->toolbar = $this->toolbar(); + if (!isset($this->toolbarsCache)) { + $this->toolbarsCache = collect($this->toolbars()); } - return $this->toolbar; + return $this->toolbarsCache; } protected function query() : Builder @@ -131,7 +134,9 @@ abstract class TableComponent extends Component protected function getFilters(): Collection { - return $this->getToolbar()?->getFilters() ?? collect(); + return $this->getToolbars() + ->map(fn(Toolbar $toolbar) => $toolbar->getFilters()) + ->flatten(); } @@ -182,12 +187,12 @@ abstract class TableComponent extends Component $data = $this->buildQuery()->paginate($this->pageSize); $allColumns = $this->getColumns(); $columns = $allColumns->filter(fn($c) => in_array($c->name,$this->activeColumns)); - $toolbar = $this->toolbar(); + $toolbars = $this->getToolbars(); Paginator::defaultView('laralight-tables::pagination'); return view('laralight-tables::table', compact( - 'data', 'allColumns', 'columns', 'toolbar', + 'data', 'allColumns', 'columns', 'toolbars', )); } } diff --git a/workbench/app/Livewire/ProductsTable.php b/workbench/app/Livewire/ProductsTable.php index 1ae33be..9f06770 100644 --- a/workbench/app/Livewire/ProductsTable.php +++ b/workbench/app/Livewire/ProductsTable.php @@ -18,7 +18,7 @@ class ProductsTable extends Table { protected $model = Product::class; - public function toolbar(): Toolbar + public function toolbars(): array { $categoryFilter = SelectFilter::make('category') ->placeholder(__('Filter category')) @@ -30,11 +30,13 @@ class ProductsTable extends Table ) ); - return parent::toolbar() - ->appendStart(Search::make()) - ->appendStart($categoryFilter) - ->appendEnd(PageSize::make()) - ->appendEnd(ColumnSelect::make()); + return [ + Toolbar::make($this) + ->appendStart(Search::make()) + ->appendStart($categoryFilter) + ->appendEnd(PageSize::make()) + ->appendEnd(ColumnSelect::make()), + ]; } public function query(): Builder diff --git a/workbench/app/Livewire/Table.php b/workbench/app/Livewire/Table.php index 5bffaed..0cda575 100644 --- a/workbench/app/Livewire/Table.php +++ b/workbench/app/Livewire/Table.php @@ -8,9 +8,4 @@ use Lightscale\LaralightTables\Toolbar; abstract class Table extends TableComponent { - public function toolbar(): Toolbar - { - return new Toolbar($this); - } - } |