summaryrefslogtreecommitdiff
path: root/src/TableComponent.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/TableComponent.php')
-rw-r--r--src/TableComponent.php64
1 files changed, 31 insertions, 33 deletions
diff --git a/src/TableComponent.php b/src/TableComponent.php
index 8e84d50..180d7c4 100644
--- a/src/TableComponent.php
+++ b/src/TableComponent.php
@@ -2,23 +2,19 @@
namespace Lightscale\LaralightTables;
-use Lightscale\LaralightTables\Columns\Column;
-use Lightscale\LaralightTables\Toolbar\Filter;
-use Lightscale\LaralightAssets\Facades\Assets;
-
-use Livewire\Component;
-use Livewire\WithPagination;
-use Livewire\Attributes\Url;
-
-use Illuminate\Pagination\Paginator;
+use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
-use Illuminate\Support\Str;
+use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
+use Illuminate\Support\Str;
use Illuminate\View\View;
-
-use Exception;
-
+use Lightscale\LaralightAssets\Facades\Assets;
+use Lightscale\LaralightTables\Columns\Column;
+use Lightscale\LaralightTables\Toolbar\Filter;
+use Livewire\Attributes\Url;
+use Livewire\Component;
+use Livewire\WithPagination;
/**
* @template TModel of Model
@@ -34,7 +30,6 @@ abstract class TableComponent extends Component
*/
protected $paginationTheme = 'bootstrap';
-
/**
* @var ?class-string<TModel>
*/
@@ -43,10 +38,13 @@ abstract class TableComponent extends Component
protected int $defaultPageSize = 10;
protected ?string $rootClass = null;
+
protected string $tableClass = 'table';
protected string $tableWrapperComponent = 'laralight-tables::wrapper';
+
protected string $paginationWrapperComponent = 'laralight-tables::wrapper';
+
protected string $toolbarsWrapperComponent = 'laralight-tables::wrapper';
// Properties
@@ -83,14 +81,14 @@ abstract class TableComponent extends Component
protected function setDefaultPageSize(): void
{
- if (!isset($this->pageSize)) {
+ if (! isset($this->pageSize)) {
$this->pageSize = $this->defaultPageSize;
}
}
protected function setDefaultActiveColumns(): void
{
- foreach($this->getColumns() as $column) {
+ foreach ($this->getColumns() as $column) {
$this->activeColumns[] = $column->name;
}
}
@@ -99,8 +97,9 @@ abstract class TableComponent extends Component
{
$search = $this
->getToolbars()
- ->first(fn(Toolbar $tb) => $tb->getSearch() !== null)
+ ->first(fn (Toolbar $tb) => $tb->getSearch() !== null)
?->getSearch();
+
return $search !== null && Str::length($this->search) >= $search->getMinLength();
}
@@ -122,12 +121,10 @@ abstract class TableComponent extends Component
if ($this->orderDirection === 'desc') {
$this->order = null;
$this->orderDirection = null;
- }
- else {
+ } else {
$this->orderDirection = 'desc';
}
- }
- else {
+ } else {
$this->order = $column;
$this->orderDirection = 'asc';
}
@@ -151,24 +148,27 @@ abstract class TableComponent extends Component
*/
protected function getToolbars(): Collection
{
- if (!isset($this->toolbarsCache)) {
+ if (! isset($this->toolbarsCache)) {
$this->toolbarsCache = collect($this->toolbars());
}
+
return $this->toolbarsCache;
}
/**
* @return Builder<TModel>
+ *
* @phpstan-return Builder<TModel>
*/
protected function query(): Builder
{
- if($this->model === null) {
+ if ($this->model === null) {
throw new Exception('Requires $model to be set or query() method to be overridden');
}
/** @var Builder<TModel> */
$query = $this->model::query();
+
return $query;
}
@@ -178,10 +178,9 @@ abstract class TableComponent extends Component
abstract protected function columns(): array;
/**
- * @param Builder<TModel> $builder
- * @param string $search
+ * @param Builder<TModel> $builder
*/
- protected function search(Builder $builder, string $search) : void {}
+ protected function search(Builder $builder, string $search): void {}
/**
* @return Collection<int, Filter>
@@ -189,18 +188,17 @@ abstract class TableComponent extends Component
protected function getFilters(): Collection
{
return $this->getToolbars()
- ->map(fn(Toolbar $toolbar): Collection => $toolbar->getFilters())
+ ->map(fn (Toolbar $toolbar): Collection => $toolbar->getFilters())
->flatten();
}
-
protected function getOrderColumn(): ?Column
{
return ($name = $this->order) === null ? null : $this->getColumns()[$name] ?? null;
}
/**
- * @param Builder<TModel> $query
+ * @param Builder<TModel> $query
*/
protected function applyOrder(Builder $query): void
{
@@ -213,7 +211,7 @@ abstract class TableComponent extends Component
/**
* @return Builder<TModel>
*/
- protected function buildQuery() : Builder
+ protected function buildQuery(): Builder
{
$query = $this->query();
@@ -240,9 +238,9 @@ abstract class TableComponent extends Component
*/
public function getColumns(): Collection
{
- if($this->columnsCache === null) {
+ if ($this->columnsCache === null) {
$this->columnsCache = collect($this->columns())->each(
- fn($c) => $c->setTable($this)
+ fn ($c) => $c->setTable($this)
)->keyBy('name');
}
@@ -253,7 +251,7 @@ abstract class TableComponent extends Component
{
$data = $this->buildQuery()->paginate($this->pageSize);
$allColumns = $this->getColumns();
- $columns = $allColumns->filter(fn($c) => in_array($c->name,$this->activeColumns));
+ $columns = $allColumns->filter(fn ($c) => in_array($c->name, $this->activeColumns));
$toolbars = $this->getToolbars();
Paginator::defaultView('laralight-tables::pagination');