summaryrefslogtreecommitdiff
path: root/src/TableComponent.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/TableComponent.php')
-rw-r--r--src/TableComponent.php65
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'
));
}
}