Skip to content

Commit 9c64e9b

Browse files
Pagination adjustment. (#10)
1 parent 5d0400b commit 9c64e9b

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

resources/views/vendor/datatables/components/pagination.blade.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,36 @@
11
<div class="mt-4 flex justify-between items-center">
2-
<span class="text-sm text-gray-600">
3-
{{ __('datatable::datatables.page') }} {{ $rows->currentPage() }} van {{ $rows->lastPage() }} - {{ $rows->total() }} {{ __('datatable::datatables.results') }}
4-
</span>
2+
<span class="text-sm text-gray-600">
3+
{{ __('datatable::datatables.page') }} {{ $currentPage }} {{ __('datatable::datatables.of') }} {{ $rows->lastPage() }} - {{ $rows->total() }} {{ __('datatable::datatables.results') }}
4+
</span>
55

66
<div class="flex gap-2">
77
<button wire:click="previousPage"
8-
class="px-4 py-2 rounded {{ $rows->onFirstPage() ? 'bg-gray-200 text-gray-400 cursor-not-allowed' : 'bg-gray-100 hover:bg-gray-200 text-gray-700' }}"
9-
{{ $rows->onFirstPage() ? 'disabled' : '' }}>
8+
class="px-4 py-2 rounded {{ $currentPage === 1 ? 'bg-gray-200 text-gray-400 cursor-not-allowed' : 'bg-gray-100 hover:bg-gray-200 text-gray-700' }}"
9+
{{ $currentPage === 1 ? 'disabled' : '' }}>
1010
{{ __('datatable::datatables.previous') }}
1111
</button>
1212

1313
@if ($rows->lastPage() <= 10)
1414
@for ($page = 1; $page <= $rows->lastPage(); $page++)
1515
<button wire:click="gotoPage({{ $page }})"
16-
class="px-4 py-2 rounded {{ $rows->currentPage() == $page ? 'bg-blue-500 text-white' : 'bg-gray-100 hover:bg-gray-200 text-gray-700' }}">
16+
class="px-4 py-2 rounded {{ $currentPage == $page ? 'bg-blue-500 text-white' : 'bg-gray-100 hover:bg-gray-200 text-gray-700' }}">
1717
{{ $page }}
1818
</button>
1919
@endfor
2020
@else
21-
2221
<select wire:change="gotoPage($event.target.value)"
2322
class="border border-gray-300 rounded px-3 py-1 text-sm w-16">
2423
@for ($page = 1; $page <= $rows->lastPage(); $page++)
25-
<option value="{{ $page }}" {{ $rows->currentPage() == $page ? 'selected' : '' }}>
24+
<option value="{{ $page }}" {{ $currentPage == $page ? 'selected' : '' }}>
2625
{{ $page }}
2726
</option>
2827
@endfor
2928
</select>
3029
@endif
3130

32-
{{-- Volgende knop --}}
3331
<button wire:click="nextPage"
34-
class="px-4 py-2 rounded {{ $rows->hasMorePages() ? 'bg-gray-100 hover:bg-gray-200 text-gray-700' : 'bg-gray-200 text-gray-400 cursor-not-allowed' }}"
35-
{{ $rows->hasMorePages() ? '' : 'disabled' }}>
32+
class="px-4 py-2 rounded {{ $currentPage === $rows->lastPage() ? 'bg-gray-200 text-gray-400 cursor-not-allowed' : 'bg-gray-100 hover:bg-gray-200 text-gray-700' }}"
33+
{{ $currentPage === $rows->lastPage() ? 'disabled' : '' }}>
3634
{{ __('datatable::datatables.next') }}
3735
</button>
3836
</div>

src/Livewire/DataTableComponent.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ class DataTableComponent extends Component
6666
/** @var bool Whether to show the select-all confirmation modal */
6767
public bool $showSelectAllModal = false;
6868

69+
public int $currentPage = 1;
70+
6971
/** @var array Livewire event listeners */
7072
protected $listeners = ['refreshTable' => '$refresh'];
7173

@@ -245,7 +247,52 @@ public function getRows(): LengthAwarePaginator
245247

246248
$query->orderBy($this->sortColumn, $this->sortDirection);
247249

248-
return $query->paginate($this->perPage);
250+
251+
$total = $query->count();
252+
$rows = $query->skip(($this->currentPage - 1) * $this->perPage)->take($this->perPage)->get();
253+
254+
return new \Illuminate\Pagination\LengthAwarePaginator(
255+
$rows,
256+
$total,
257+
$this->perPage,
258+
$this->currentPage
259+
);
260+
}
261+
262+
/**
263+
* Navigate to a specific page without modifying the URL.
264+
*
265+
* @param int $page The page number to navigate to.
266+
*/
267+
public function gotoPage(int $page): void
268+
{
269+
$this->setPage($page, $this->getPaginationName());
270+
}
271+
272+
/**
273+
* Navigate to the previous page.
274+
*/
275+
public function previousPage(): void
276+
{
277+
$this->setPage($this->page - 1, $this->getPaginationName());
278+
}
279+
280+
/**
281+
* Navigate to the next page.
282+
*/
283+
public function nextPage(): void
284+
{
285+
$this->setPage($this->page + 1, $this->getPaginationName());
286+
}
287+
288+
/**
289+
* Get a unique pagination name for this DataTable instance.
290+
*
291+
* @return string The unique pagination name.
292+
*/
293+
protected function getPaginationName(): string
294+
{
295+
return md5($this->model . implode(',', $this->columns));
249296
}
250297

251298
/**

0 commit comments

Comments
 (0)