diff options
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 |