diff options
Diffstat (limited to 'src/Columns')
-rw-r--r-- | src/Columns/LinkColumn.php | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/Columns/LinkColumn.php b/src/Columns/LinkColumn.php new file mode 100644 index 0000000..7ba3a95 --- /dev/null +++ b/src/Columns/LinkColumn.php @@ -0,0 +1,38 @@ +<?php + +namespace Lightscale\LaralightTables\Columns; + +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\HtmlString; +use Illuminate\View\ComponentAttributeBag; + +use Closure; + +class LinkColumn extends Column +{ + private Closure $urlFn; + private ?Closure $linkAttributesFn = null; + + public function url(callable $fn) : static + { + $this->urlFn = Closure::fromCallable($fn); + return $this; + } + + public function attributes(callable $fn) : static + { + $this->linkAttributesFn = $fn; + return $this; + } + + protected function getContent(Model $row) + { + $content = parent::getContent($row); + $url = $this->urlFn->call($this, $row); + $attributes = $this->linkAttributesFn?->call($this, $row) ?? []; + $attributes = (new ComponentAttributeBag(['href' => $url] + $attributes))->toHtml(); + + return new HtmlString("<a {$attributes}>{$content}</a>"); + } + +} |