summaryrefslogtreecommitdiff
path: root/src/Columns
diff options
context:
space:
mode:
Diffstat (limited to 'src/Columns')
-rw-r--r--src/Columns/Column.php68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/Columns/Column.php b/src/Columns/Column.php
new file mode 100644
index 0000000..7b9f248
--- /dev/null
+++ b/src/Columns/Column.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Lightscale\LaralightTables\Columns;
+
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\View\ComponentAttributeBag;
+
+use Closure;
+
+class Column {
+
+ private TableComponent $table;
+
+ private Closure $displayFn;
+ private ?Closure $sortFn = null;
+ private ?Closure $searchFn = null;
+ private ?Closure $attributesFn = null;
+
+ public function __construct(
+ public string $name,
+ public string $title
+ ) {
+ $this->displayFn = Closure::fromCallable([$this, 'defaultDisplay']);
+ }
+
+ public static function make(string $name, $title) : static
+ {
+ return new static($name, $title);
+ }
+
+ private function defaultDisplay(Model $row, Column $column)
+ {
+ return $row->{$column->name};
+ }
+
+ public function display(callable $fn) : static
+ {
+ $this->displayFn = Closure::fromCallable($fn);
+ return $this;
+ }
+
+ public function sortable(callable $fn) : static
+ {
+ $this->sortFn = Closure::fromCallable($fn);
+ return $this;
+ }
+
+ public function searchable(callable $fn) : static
+ {
+ $this->searchFn = Closure::fromCallable($fn);
+ return $this;
+ }
+
+ public function attributes(callable $fn) : static
+ {
+ $this->attributesFn = Closure::fromCallable($fn);
+ return $this;
+ }
+
+ 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'));
+ }
+
+}