summaryrefslogtreecommitdiff
path: root/src/Columns
diff options
context:
space:
mode:
authorSam Light <samlight1994@gmail.com>2023-11-06 00:06:39 +0000
committerSam Light <samlight1994@gmail.com>2023-11-06 00:06:39 +0000
commit8f69cb7f3df70c40cbae47a5661dc9ae67aae728 (patch)
tree8ce246d53dc428c1fe95fad04e631a89048ddea0 /src/Columns
parent5c24746657ac23c7a65c4e4efc89cf6bfcb5a52c (diff)
Lots of table improvments
Diffstat (limited to 'src/Columns')
-rw-r--r--src/Columns/Column.php55
1 files changed, 36 insertions, 19 deletions
diff --git a/src/Columns/Column.php b/src/Columns/Column.php
index 7b9f248..f3fe811 100644
--- a/src/Columns/Column.php
+++ b/src/Columns/Column.php
@@ -2,40 +2,48 @@
namespace Lightscale\LaralightTables\Columns;
+use Lightscale\LaralightTables\TableComponent;
+
use Illuminate\Database\Eloquent\Model;
use Illuminate\View\ComponentAttributeBag;
+use Illuminate\Support\HtmlString;
use Closure;
class Column {
private TableComponent $table;
+ private bool $showInSelect;
- private Closure $displayFn;
+ private ?Closure $slotFn = null;
private ?Closure $sortFn = null;
- private ?Closure $searchFn = null;
- private ?Closure $attributesFn = null;
+ private ?Closure $tdAttributesFn = null;
public function __construct(
public string $name,
- public string $title
+ public ?string $title = null
) {
- $this->displayFn = Closure::fromCallable([$this, 'defaultDisplay']);
+ $this->showInSelect = $this->title !== null;
}
- public static function make(string $name, $title) : static
+ public static function make(string $name, ?string $title = null) : static
{
return new static($name, $title);
}
- private function defaultDisplay(Model $row, Column $column)
+ public function setTable(TableComponent $table) : void
+ {
+ $this->table = $table;
+ }
+
+ private function defaultSlot(Model $row)
{
- return $row->{$column->name};
+ return $row->{$this->name};
}
- public function display(callable $fn) : static
+ public function slot(callable $fn) : static
{
- $this->displayFn = Closure::fromCallable($fn);
+ $this->slotFn = Closure::fromCallable($fn);
return $this;
}
@@ -45,24 +53,33 @@ class Column {
return $this;
}
- public function searchable(callable $fn) : static
+ public function tdAttributes(callable $fn) : static
{
- $this->searchFn = Closure::fromCallable($fn);
+ $this->tdAttributesFn = Closure::fromCallable($fn);
return $this;
}
- public function attributes(callable $fn) : static
+ public function showInSelect($show = true)
{
- $this->attributesFn = Closure::fromCallable($fn);
- return $this;
+ $this->showInSelect = $show;
+ }
+
+ public function getShowInSelect()
+ {
+ return $this->showInSelect;
+ }
+
+ protected function getContent(Model $row)
+ {
+ return $this->slotFn?->call($this, $row, $this) ?? $this->defaultSlot($row);
}
public function view(Model $row)
{
- $attributes = $this->attributesFn?->call($this, $row) ?? [];
- $attributes = new ComponentAttributeBag($attributes);
- $content = $this->displayFn->call($this, $row, $this);
- return view('laralight-tables::column', compact('attributes', 'content'));
+ $attributes = $this->tdAttributesFn?->call($this, $row) ?? [];
+ $attributes = (new ComponentAttributeBag($attributes))->toHtml();
+ $content = $this->getContent($row);
+ return new HtmlString("<td {$attributes}>{$content}</td>");
}
}