summaryrefslogtreecommitdiff
path: root/src/TableComponent.php
blob: 04d457e2986adf4d3307c190da13bb598a31aa8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php

namespace Lightscale\LaralightTables;

use Livewire\Component;
use Livewire\WithPagination;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;

use Exception;

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 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
    {
        if($this->model === null) {
            throw new Exception('Requires $model to be set or query() method to be overridden');
        }

        return $this->model::query();
    }

    abstract protected function columns() : array;

    protected function search(Builder $builder, string $search) : void {}

    protected function filters() : array
    {
        return [];
    }

    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()
    {
        $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', [
            'searchable' => $this->searchable,
            'searchDebounce' => $this->searchDebounce,
            'showPageSizeSelect' => $this->showPageSizeSelect,
            'showColumnSelect' => $this->showColumnSelect,
            'pageSizes' => $this->pageSizes,
        ] + compact(
            'data', 'allColumns', 'columns'
        ));
    }
}