From 1dc1088933c906d4676056d53395d34ce0a68d01 Mon Sep 17 00:00:00 2001 From: Sam Light Date: Mon, 27 Jul 2026 22:28:18 +0100 Subject: changed to use sort direction --- src/TableComponent.php | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'src/TableComponent.php') diff --git a/src/TableComponent.php b/src/TableComponent.php index e36029e..196d5f8 100644 --- a/src/TableComponent.php +++ b/src/TableComponent.php @@ -13,6 +13,7 @@ use Illuminate\Support\Str; use Illuminate\View\View; use Lightscale\LaralightAssets\Facades\Assets; use Lightscale\LaralightTables\Columns\Column; +use Lightscale\LaralightTables\Support\SortDirections; use Lightscale\LaralightTables\Toolbar\Filter; use Livewire\Attributes\Url; use Livewire\Component; @@ -66,10 +67,12 @@ abstract class TableComponent extends Component public ?string $order = null; /** - * @var SortDirection|'asc'|'desc'|null + * Wire/URL transport only — read through getOrderDirection(). + * + * @var 'asc'|'desc'|null */ #[Url] - public SortDirection|string|null $orderDirection = null; + public ?string $orderDirection = null; public function mount(): void { @@ -115,18 +118,28 @@ abstract class TableComponent extends Component $this->resetPage(); } + public function getOrderDirection(): ?SortDirection + { + return SortDirections::tryFrom($this->orderDirection); + } + + protected function setOrderDirection(?SortDirection $dir): void + { + $this->orderDirection = $dir === null ? null : SortDirections::value($dir); + } + public function orderBy(string $column): void { if ($column === $this->order) { - if ($this->orderDirection === 'desc') { + if ($this->getOrderDirection() === SortDirection::Descending) { $this->order = null; - $this->orderDirection = null; + $this->setOrderDirection(null); } else { - $this->orderDirection = 'desc'; + $this->setOrderDirection(SortDirection::Descending); } } else { $this->order = $column; - $this->orderDirection = 'asc'; + $this->setOrderDirection(SortDirection::Ascending); } } @@ -203,8 +216,10 @@ abstract class TableComponent extends Component protected function applyOrder(Builder $query): void { $column = $this->getOrderColumn(); - if ($column) { - $column->applySort($query, $this->orderDirection); + $dir = $this->getOrderDirection(); + + if ($column !== null && $dir !== null) { + $column->applySort($query, $dir); } } @@ -267,6 +282,7 @@ abstract class TableComponent extends Component return view('laralight-tables::table', [ 'rootClass' => $this->rootClass, 'tableClass' => $this->tableClass, + 'sortDirection' => $this->getOrderDirection(), ] + compact( 'data', 'allColumns', 'columns', 'toolbars', )); -- cgit v1.2.3