diff options
| -rw-r--r-- | resources/views/table.blade.php | 17 | ||||
| -rw-r--r-- | src/Columns/ButtonColumn.php | 18 | ||||
| -rw-r--r-- | src/Columns/Column.php | 20 | ||||
| -rw-r--r-- | src/Columns/ElementColumn.php | 39 | ||||
| -rw-r--r-- | src/Columns/LinkColumn.php | 24 | ||||
| -rw-r--r-- | src/TableComponent.php | 13 | 
6 files changed, 102 insertions, 29 deletions
diff --git a/resources/views/table.blade.php b/resources/views/table.blade.php index 3ee29ab..783255e 100644 --- a/resources/views/table.blade.php +++ b/resources/views/table.blade.php @@ -1,13 +1,13 @@  <div>      @if($searchable || $showColumnSelect || $showPageSizeSelect)          <div class="table-controls pb-2 d-flex justify-content-between align-items-center"> -            @if($searchable) -                <div> +            <div> +                @if($searchable)                      <input class="form-control border-secondary" type="search"                             wire:model.live.debounce.{{ $searchDebounce}}="search"                             placeholder="{{ __('Search') }}..." /> -                </div> -            @endif +                @endif +            </div>              <div class="d-flex gap-3">                  @if($showColumnSelect)                      <div class="dropdown"> @@ -21,7 +21,7 @@                              <label class="d-block">                                  <input type="checkbox" wire:model.live="activeColumns"                                         value="{{ $column->name }}" /> -                                {{ $column->title }} +                                {{ $column->getTitle() }}                              </label>                              @endforeach                          </div> @@ -38,10 +38,15 @@          </div>      @endif      <table class="table"> +        <colgroup> +            @foreach($columns as $column) +                <col @class([$column->getColClass()]) /> +            @endforeach +        </colgroup>          <thead>              <tr>                  @foreach($columns as $column) -                    <th>{{ $column->title }}</th> +                    <th scope="col">{{ $column->getTitle() }}</th>                  @endforeach              </tr>          </thead> diff --git a/src/Columns/ButtonColumn.php b/src/Columns/ButtonColumn.php new file mode 100644 index 0000000..794b175 --- /dev/null +++ b/src/Columns/ButtonColumn.php @@ -0,0 +1,18 @@ +<?php + +namespace Lightscale\LaralightTables\Columns; + +use Illuminate\Database\Eloquent\Model; + +class ButtonColumn extends ElementColumn +{ +    protected string $element = 'button'; + +    protected function getElemAttributes(Model $row) : array +    { +        return parent::getElemAttributes($row) + [ +            'type' => 'button' +        ]; +    } + +} diff --git a/src/Columns/Column.php b/src/Columns/Column.php index f3fe811..94e386f 100644 --- a/src/Columns/Column.php +++ b/src/Columns/Column.php @@ -19,9 +19,11 @@ class Column {      private ?Closure $sortFn = null;      private ?Closure $tdAttributesFn = null; +    private ?string $colClass = null; +      public function __construct(          public string $name, -        public ?string $title = null +        private ?string $title = null      ) {          $this->showInSelect = $this->title !== null;      } @@ -36,6 +38,11 @@ class Column {          $this->table = $table;      } +    public function getTitle() +    { +        return $this->title; +    } +      private function defaultSlot(Model $row)      {          return $row->{$this->name}; @@ -53,6 +60,17 @@ class Column {          return $this;      } +    public function colClass(string $v) : static +    { +        $this->colClass = $v; +        return $this; +    } + +    public function getColClass() : ?string +    { +        return $this->colClass; +    } +      public function tdAttributes(callable $fn) : static      {          $this->tdAttributesFn = Closure::fromCallable($fn); diff --git a/src/Columns/ElementColumn.php b/src/Columns/ElementColumn.php new file mode 100644 index 0000000..5ad7c7c --- /dev/null +++ b/src/Columns/ElementColumn.php @@ -0,0 +1,39 @@ +<?php + +namespace Lightscale\LaralightTables\Columns; + +use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\HtmlString; +use Illuminate\View\ComponentAttributeBag; + +use Closure; + +abstract class ElementColumn extends Column +{ + +    protected string $element; +    private ?Closure $elemAttributesFn = null; + +    public function attributes(callable $fn) : static +    { +        $this->elemAttributesFn = Closure::fromCallable($fn); +        return $this; +    } + +    protected function getElemAttributes(Model $row) : array +    { +        return $this->elemAttributesFn?->call($this, $row) ?? []; +    } + +    protected function getContent(Model $row) +    { +        $content = parent::getContent($row); +        $attributes = new ComponentAttributeBag($this->getElemAttributes($row)); +        $attributes = $attributes->toHtml(); + +        return new HtmlString( +            "<{$this->element} {$attributes}>{$content}</{$this->element}>" +        ); +    } + +} diff --git a/src/Columns/LinkColumn.php b/src/Columns/LinkColumn.php index 7ba3a95..edb1140 100644 --- a/src/Columns/LinkColumn.php +++ b/src/Columns/LinkColumn.php @@ -3,15 +3,14 @@  namespace Lightscale\LaralightTables\Columns;  use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\HtmlString; -use Illuminate\View\ComponentAttributeBag;  use Closure; -class LinkColumn extends Column +class LinkColumn extends ElementColumn  { +    protected string $element = 'a'; +      private Closure $urlFn; -    private ?Closure $linkAttributesFn = null;      public function url(callable $fn) : static      { @@ -19,20 +18,11 @@ class LinkColumn extends Column          return $this;      } -    public function attributes(callable $fn) : static -    { -        $this->linkAttributesFn = $fn; -        return $this; -    } - -    protected function getContent(Model $row) +    public function getElemAttributes(Model $row) : array      { -        $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>"); +        return parent::getElemAttributes($row) + [ +            'href' => $this->urlFn->call($this, $row), +        ];      }  } diff --git a/src/TableComponent.php b/src/TableComponent.php index 04d457e..da31a90 100644 --- a/src/TableComponent.php +++ b/src/TableComponent.php @@ -4,6 +4,7 @@ namespace Lightscale\LaralightTables;  use Livewire\Component;  use Livewire\WithPagination; +use Livewire\Attributes\Url;  use Illuminate\Database\Eloquent\Builder;  use Illuminate\Support\Str; @@ -28,6 +29,8 @@ abstract class TableComponent extends Component      // Properties      public string $search = ''; + +    #[Url]      public int $pageSize = 0;      public array $activeColumns = []; @@ -72,17 +75,17 @@ abstract class TableComponent extends Component          return $query;      } +    private $columnsCache = null; +      protected function getColumns()      { -        static $columns = null; - -        if($columns === null) { -            $columns = collect($this->columns())->each( +        if($this->columnsCache === null) { +            $this->columnsCache = collect($this->columns())->each(                  fn($c) => $c->setTable($this)              );          } -        return $columns; +        return $this->columnsCache;      }      public function render()  | 
