diff options
Diffstat (limited to 'src/Columns/Column.php')
-rw-r--r-- | src/Columns/Column.php | 41 |
1 files changed, 26 insertions, 15 deletions
diff --git a/src/Columns/Column.php b/src/Columns/Column.php index e260472..0638490 100644 --- a/src/Columns/Column.php +++ b/src/Columns/Column.php @@ -2,30 +2,32 @@ namespace Lightscale\LaralightTables\Columns; -use Lightscale\LaralightTables\TableComponent; -use Lightscale\LaralightTables\Concerns\Makable; - -use Illuminate\Database\Eloquent\Model; +use Closure; use Illuminate\Database\Eloquent\Builder; -use Illuminate\View\ComponentAttributeBag; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\HtmlString; +use Illuminate\View\ComponentAttributeBag; +use Lightscale\LaralightTables\Concerns\Makable; +use Lightscale\LaralightTables\TableComponent; -use Closure; - -class Column { +class Column +{ use Makable; /** * @var TableComponent<Model> */ private TableComponent $table; + private bool $showInSelect; + private bool $shouldEscape = true; /** * @var ?Closure(Model, Column): string */ private ?Closure $slotFn = null; + private ?Closure $sortFn = null; /** @@ -44,7 +46,8 @@ class Column { /** * @template TModel of Model - * @param TableComponent<TModel> $table + * + * @param TableComponent<TModel> $table */ public function setTable(TableComponent $table): void { @@ -61,9 +64,10 @@ class Column { return (string) $row->{$this->name}; } - public function slot(callable $fn) : static + public function slot(callable $fn): static { $this->slotFn = Closure::fromCallable($fn); + return $this; } @@ -72,6 +76,7 @@ class Column { if ($fn !== null) { $this->sortFn = Closure::fromCallable($fn); } + return $this; } @@ -82,7 +87,8 @@ class Column { /** * @template TModel of Model - * @param Builder<TModel> $query + * + * @param Builder<TModel> $query */ public function applySort(Builder $query, ?string $dir): void { @@ -91,26 +97,29 @@ class Column { } } - public function colClass(string $v) : static + public function colClass(string $v): static { $this->colClass = $v; + return $this; } - public function getColClass() : ?string + public function getColClass(): ?string { return $this->colClass; } - public function tdAttributes(callable $fn) : static + public function tdAttributes(callable $fn): static { $this->tdAttributesFn = Closure::fromCallable($fn); + return $this; } public function showInSelect(bool $show = true): static { $this->showInSelect = $show; + return $this; } @@ -122,6 +131,7 @@ class Column { public function shouldEscape(bool $v): static { $this->shouldEscape = $v; + return $this; } @@ -133,6 +143,7 @@ class Column { protected function getContent(Model $row): string { $content = $this->slotFn === null ? $this->defaultSlot($row) : ($this->slotFn)($row, $this); + return $this->escape($content); } @@ -149,7 +160,7 @@ class Column { $attributes = $this->tdAttributesFn?->call($this, $row) ?? []; $attributes = (new ComponentAttributeBag($attributes))->toHtml(); $content = $this->getContent($row); + return new HtmlString("<td {$attributes}>{$content}</td>"); } - } |