diff options
| author | Sam Light <samlight1994@gmail.com> | 2023-11-05 19:38:00 +0000 | 
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2023-11-05 19:38:00 +0000 | 
| commit | 5c24746657ac23c7a65c4e4efc89cf6bfcb5a52c (patch) | |
| tree | 3de215fe85096429e615127172a2cee0b56661b0 /src/Columns/Column.php | |
| parent | bb6795b604e5686ee069c48dd7f7ce8cc3bf73c3 (diff) | |
Built basic table
Diffstat (limited to 'src/Columns/Column.php')
| -rw-r--r-- | src/Columns/Column.php | 68 | 
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')); +    } + +} | 
