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/Columns/Column.php | |
parent | a883170dd1c723bcd02916f9bc8a96ed85a61761 (diff) |
Filters, sorting, escaping and more....
Diffstat (limited to 'src/Columns/Column.php')
-rw-r--r-- | src/Columns/Column.php | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/src/Columns/Column.php b/src/Columns/Column.php index 895708f..a8dc2e6 100644 --- a/src/Columns/Column.php +++ b/src/Columns/Column.php @@ -5,6 +5,7 @@ namespace Lightscale\LaralightTables\Columns; use Lightscale\LaralightTables\TableComponent; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Builder; use Illuminate\View\ComponentAttributeBag; use Illuminate\Support\HtmlString; @@ -14,6 +15,7 @@ class Column { private TableComponent $table; private bool $showInSelect; + private bool $shouldEscape = true; private ?Closure $slotFn = null; private ?Closure $sortFn = null; @@ -54,12 +56,24 @@ class Column { return $this; } - public function sortable(callable $fn) : static + public function sortable(?callable $fn): static { - $this->sortFn = Closure::fromCallable($fn); + $this->sortFn = $fn; return $this; } + public function isSortable(): bool + { + return $this->sortFn !== null; + } + + public function applySort(Builder $query, string $dir): void + { + if ($this->sortFn !== null) { + ($this->sortFn)($query, $dir); + } + } + public function colClass(string $v) : static { $this->colClass = $v; @@ -88,9 +102,23 @@ class Column { return $this->showInSelect; } + public function shouldEscape(bool $v): static + { + $this->shouldEscape = $v; + return $this; + } + + public function escape(string $content): string + { + return $this->shouldEscape ? e($content) : $content; + } + protected function getContent(Model $row): string { - return $this->slotFn?->call($this, $row, $this) ?? $this->defaultSlot($row); + return $this->escape( + $this->slotFn?->call($this, $row, $this) ?? + $this->defaultSlot($row) + ); } public function view(Model $row): HtmlString |