diff options
Diffstat (limited to 'src/Columns/Column.php')
| -rw-r--r-- | src/Columns/Column.php | 55 | 
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>");      }  } | 
