This repository was archived by the owner on Feb 15, 2024. It is now read-only.
forked from rappasoft/laravel-livewire-tables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataTableComponent.php
110 lines (95 loc) · 3.34 KB
/
DataTableComponent.php
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
105
106
107
108
109
110
<?php
namespace Rappasoft\LaravelLivewireTables;
use Illuminate\Database\Eloquent\Builder;
use Livewire\Component;
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
use Rappasoft\LaravelLivewireTables\Traits\ComponentUtilities;
use Rappasoft\LaravelLivewireTables\Traits\WithBulkActions;
use Rappasoft\LaravelLivewireTables\Traits\WithColumns;
use Rappasoft\LaravelLivewireTables\Traits\WithColumnSelect;
use Rappasoft\LaravelLivewireTables\Traits\WithData;
use Rappasoft\LaravelLivewireTables\Traits\WithDebugging;
use Rappasoft\LaravelLivewireTables\Traits\WithFilters;
use Rappasoft\LaravelLivewireTables\Traits\WithFooter;
use Rappasoft\LaravelLivewireTables\Traits\WithPagination;
use Rappasoft\LaravelLivewireTables\Traits\WithRefresh;
use Rappasoft\LaravelLivewireTables\Traits\WithReordering;
use Rappasoft\LaravelLivewireTables\Traits\WithSearch;
use Rappasoft\LaravelLivewireTables\Traits\WithSecondaryHeader;
use Rappasoft\LaravelLivewireTables\Traits\WithSorting;
abstract class DataTableComponent extends Component
{
use ComponentUtilities,
WithBulkActions,
WithColumns,
WithColumnSelect,
WithData,
WithDebugging,
WithFilters,
WithFooter,
WithSecondaryHeader,
WithPagination,
WithRefresh,
WithReordering,
WithSearch,
WithSorting;
protected $listeners = ['refreshDatatable' => '$refresh'];
/**
* Runs on every request, immediately after the component is instantiated, but before any other lifecycle methods are called
*/
public function boot(): void
{
$this->{$this->tableName} = [
'sorts' => $this->{$this->tableName}['sorts'] ?? [],
'filters' => $this->{$this->tableName}['filters'] ?? [],
];
// Set the user defined columns to work with
$this->setColumns();
// Call the child configuration, if any
$this->configure();
// Make sure a primary key is set
if (! $this->hasPrimaryKey()) {
throw new DataTableConfigurationException('You must set a primary key using setPrimaryKey in the configure method.');
}
// Set the filter defaults based on the filter type
$this->setFilterDefaults();
}
/**
* Runs on every request, after the component is mounted or hydrated, but before any update methods are called
*/
public function booted(): void
{
$this->setTheme();
}
/**
* Set any configuration options
*/
abstract public function configure(): void;
/**
* The array defining the columns of the table.
*
* @return array
*/
abstract public function columns(): array;
/**
* The base query.
*/
public function builder(): Builder
{
if ($this->hasModel()) {
return $this->getModel()::query();
}
throw new DataTableConfigurationException('You must either specify a model or implement the builder method.');
}
/**
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
*/
public function render()
{
return view('livewire-tables::datatable')
->with([
'columns' => $this->getColumns(),
'rows' => $this->getRows(),
]);
}
}