summaryrefslogtreecommitdiff
path: root/src/TableComponent.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/TableComponent.php')
-rw-r--r--src/TableComponent.php32
1 files changed, 24 insertions, 8 deletions
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',
));