diff options
author | Sam Light <samlight1994@gmail.com> | 2023-11-06 00:06:49 +0000 |
---|---|---|
committer | Sam Light <samlight1994@gmail.com> | 2023-11-06 00:06:49 +0000 |
commit | d8d11445460a1a9e35c39937772d789036286fdf (patch) | |
tree | 94b427e2077079c61179be8cf9c30747abce06f7 | |
parent | 8f69cb7f3df70c40cbae47a5661dc9ae67aae728 (diff) |
Made link column
-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>"); + } + +} |