diff options
| author | Sam Light <samlight1994@gmail.com> | 2023-11-06 00:06:39 +0000 | 
|---|---|---|
| committer | Sam Light <samlight1994@gmail.com> | 2023-11-06 00:06:39 +0000 | 
| commit | 8f69cb7f3df70c40cbae47a5661dc9ae67aae728 (patch) | |
| tree | 8ce246d53dc428c1fe95fad04e631a89048ddea0 /src/TableComponent.php | |
| parent | 5c24746657ac23c7a65c4e4efc89cf6bfcb5a52c (diff) | |
Lots of table improvments
Diffstat (limited to 'src/TableComponent.php')
| -rw-r--r-- | src/TableComponent.php | 65 | 
1 files changed, 57 insertions, 8 deletions
| diff --git a/src/TableComponent.php b/src/TableComponent.php index e530415..04d457e 100644 --- a/src/TableComponent.php +++ b/src/TableComponent.php @@ -6,7 +6,7 @@ use Livewire\Component;  use Livewire\WithPagination;  use Illuminate\Database\Eloquent\Builder; -use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Str;  use Exception; @@ -14,10 +14,34 @@ abstract class TableComponent extends Component  {      use WithPagination; +    // Config      protected $paginationTheme = 'bootstrap';      protected bool $searchable = true; +    protected int $searchMinLength = 2; +    protected int $searchDebounce = 250; +    protected bool $showPageSizeSelect = true; +    protected bool $showColumnSelect = true; -    protected Model $model = null; +    protected array $pageSizes = [10, 25, 50]; + +    protected $model = null; + +    // Properties +    public string $search = ''; +    public int $pageSize = 0; +    public array $activeColumns = []; + +    public function __construct() +    { +        $this->pageSize = $this->pageSizes[0] ?? 0; +    } + +    public function mount() +    { +        foreach($this->getColumns() as $column) { +            $this->activeColumns[] = $column->name; +        } +    }      protected function query() : Builder      { @@ -37,19 +61,44 @@ abstract class TableComponent extends Component          return [];      } -    protected buildQuery() : Builder +    protected function buildQuery() : Builder      {          $query = $this->query(); + +        if($this->searchable && (Str::length($this->search) >= $this->searchMinLength)) { +            $this->search($query, $this->search); +        } + +        return $query; +    } + +    protected function getColumns() +    { +        static $columns = null; + +        if($columns === null) { +            $columns = collect($this->columns())->each( +                fn($c) => $c->setTable($this) +            ); +        } + +        return $columns;      }      public function render()      { -        $this->buildQuery()->paginate(); -        $data = $query->paginate($this->pageSize); -        $columns = $this->columns(); +        $data = $this->buildQuery()->paginate($this->pageSize); +        $allColumns = $this->getColumns(); +        $columns = $allColumns->filter(fn($c) => in_array($c->name,$this->activeColumns)); -        return view('laralight-tables::table', compact( -            'data', 'columns' +        return view('laralight-tables::table', [ +            'searchable' => $this->searchable, +            'searchDebounce' => $this->searchDebounce, +            'showPageSizeSelect' => $this->showPageSizeSelect, +            'showColumnSelect' => $this->showColumnSelect, +            'pageSizes' => $this->pageSizes, +        ] + compact( +            'data', 'allColumns', 'columns'          ));      }  } | 
