From 8370bc42fa0514b7336694799ced6339a85aad1e Mon Sep 17 00:00:00 2001 From: renardudezert Date: Tue, 31 Jan 2023 13:24:26 +0100 Subject: [PATCH 1/6] Bugfix : use LengthAwarePaginator in order to respect limit - If the Query Builder use a limit, we have to use the LengthAwarePaginator in order to have the limit in the pagination. --- src/Http/Livewire/LivewireDatatable.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Http/Livewire/LivewireDatatable.php b/src/Http/Livewire/LivewireDatatable.php index d799f8cf..5f543e8b 100644 --- a/src/Http/Livewire/LivewireDatatable.php +++ b/src/Http/Livewire/LivewireDatatable.php @@ -19,6 +19,7 @@ use Mediconesystems\LivewireDatatables\Traits\WithCallbacks; use Mediconesystems\LivewireDatatables\Traits\WithPresetDateFilters; use Mediconesystems\LivewireDatatables\Traits\WithPresetTimeFilters; +use Illuminate\Pagination\LengthAwarePaginator; class LivewireDatatable extends Component { @@ -1126,8 +1127,15 @@ public function getResultsProperty() { $this->row = 1; + $paginatedQuery = $this->getQuery()->paginate($this->perPage); + $total = ($this->getQuery()->limit) ?: $paginatedQuery->total(); + $paginatedQuery = new LengthAwarePaginator( + $paginatedQuery->toArray()['data'], + $paginatedQuery->total() < $total ? $paginatedQuery->total() : $total, + $this->perPage + ); return $this->mapCallbacks( - $this->getQuery()->paginate($this->perPage) + $paginatedQuery ); } From a800899342921a9e2843c443d021491a1d956440 Mon Sep 17 00:00:00 2001 From: renardudezert Date: Tue, 31 Jan 2023 13:30:59 +0100 Subject: [PATCH 2/6] Fix StyleCI --- src/Http/Livewire/LivewireDatatable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Livewire/LivewireDatatable.php b/src/Http/Livewire/LivewireDatatable.php index 5f543e8b..9efd9c5b 100644 --- a/src/Http/Livewire/LivewireDatatable.php +++ b/src/Http/Livewire/LivewireDatatable.php @@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Database\Query\Expression; +use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Arr; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; @@ -19,7 +20,6 @@ use Mediconesystems\LivewireDatatables\Traits\WithCallbacks; use Mediconesystems\LivewireDatatables\Traits\WithPresetDateFilters; use Mediconesystems\LivewireDatatables\Traits\WithPresetTimeFilters; -use Illuminate\Pagination\LengthAwarePaginator; class LivewireDatatable extends Component { From 9b7d5bb548fe3a72f42120e58b18c493242db925 Mon Sep 17 00:00:00 2001 From: renardudezert Date: Tue, 31 Jan 2023 13:32:15 +0100 Subject: [PATCH 3/6] Fix StyleCI-2 Carriage return before return() --- src/Http/Livewire/LivewireDatatable.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Http/Livewire/LivewireDatatable.php b/src/Http/Livewire/LivewireDatatable.php index 9efd9c5b..4ed7ce66 100644 --- a/src/Http/Livewire/LivewireDatatable.php +++ b/src/Http/Livewire/LivewireDatatable.php @@ -1134,6 +1134,7 @@ public function getResultsProperty() $paginatedQuery->total() < $total ? $paginatedQuery->total() : $total, $this->perPage ); + return $this->mapCallbacks( $paginatedQuery ); From 1711884f1181cd2d786765d144a2f09166d1a894 Mon Sep 17 00:00:00 2001 From: renardudezert Date: Tue, 31 Jan 2023 13:34:52 +0100 Subject: [PATCH 4/6] Fix StyleCI-3 --- src/Http/Livewire/LivewireDatatable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Livewire/LivewireDatatable.php b/src/Http/Livewire/LivewireDatatable.php index 4ed7ce66..bd3e53ba 100644 --- a/src/Http/Livewire/LivewireDatatable.php +++ b/src/Http/Livewire/LivewireDatatable.php @@ -1134,7 +1134,7 @@ public function getResultsProperty() $paginatedQuery->total() < $total ? $paginatedQuery->total() : $total, $this->perPage ); - + return $this->mapCallbacks( $paginatedQuery ); From 17cef766a56e8f7fc60787bf172f4eea0f5c9dd3 Mon Sep 17 00:00:00 2001 From: renardudezert Date: Wed, 1 Feb 2023 14:08:39 +0100 Subject: [PATCH 5/6] Bugfix : case when limit is lower as perPage --- src/Http/Livewire/LivewireDatatable.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Http/Livewire/LivewireDatatable.php b/src/Http/Livewire/LivewireDatatable.php index bd3e53ba..200073b3 100644 --- a/src/Http/Livewire/LivewireDatatable.php +++ b/src/Http/Livewire/LivewireDatatable.php @@ -1127,7 +1127,8 @@ public function getResultsProperty() { $this->row = 1; - $paginatedQuery = $this->getQuery()->paginate($this->perPage); + $perPage = ($this->getQuery()->limit && ($this->getQuery()->limit < $this->perPage)) ? $this->getQuery()->limit : $this->perPage; + $paginatedQuery = $this->getQuery()->paginate($perPage); $total = ($this->getQuery()->limit) ?: $paginatedQuery->total(); $paginatedQuery = new LengthAwarePaginator( $paginatedQuery->toArray()['data'], From 63b039583d5a5411f83cddfb7b23b9fb6d60b755 Mon Sep 17 00:00:00 2001 From: renardudezert Date: Thu, 2 Feb 2023 17:22:37 +0100 Subject: [PATCH 6/6] Add options and currentPage to LengthAwarePaginator - Bugfix toggleSelectAll() where limit is used on Query Builder - Bugfix in checkboxQuery() : don't use reorder (if defaultSort() is used, it will break the toggleSelectAll behaviour) --- src/Http/Livewire/LivewireDatatable.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Http/Livewire/LivewireDatatable.php b/src/Http/Livewire/LivewireDatatable.php index 200073b3..0c1aae51 100644 --- a/src/Http/Livewire/LivewireDatatable.php +++ b/src/Http/Livewire/LivewireDatatable.php @@ -1131,9 +1131,11 @@ public function getResultsProperty() $paginatedQuery = $this->getQuery()->paginate($perPage); $total = ($this->getQuery()->limit) ?: $paginatedQuery->total(); $paginatedQuery = new LengthAwarePaginator( - $paginatedQuery->toArray()['data'], + $paginatedQuery->items(), $paginatedQuery->total() < $total ? $paginatedQuery->total() : $total, - $this->perPage + $this->perPage, + $paginatedQuery->currentPage(), + $paginatedQuery->getOptions() ); return $this->mapCallbacks( @@ -1714,7 +1716,7 @@ public function getQuery($export = false) public function checkboxQuery() { - return $this->query->reorder()->get()->map(function ($row) { + return $this->query->get()->map(function ($row) { return (string) $row->checkbox_attribute; }); } @@ -1733,7 +1735,7 @@ public function toggleSelectAll() $this->visibleSelected = $visible_checkboxes; } } else { - if (count($this->selected) === $this->getQuery()->getCountForPagination()) { + if (count($this->selected) === ($this->getQuery()->limit ?: $this->getQuery()->getCountForPagination())) { $this->selected = []; } else { $this->selected = $this->checkboxQuery()->values()->toArray();