Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions resources/views/vendor/datatables/components/pagination.blade.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
<div class="mt-4 flex justify-between items-center">
<span class="text-sm text-gray-600">
{{ __('datatable::datatables.page') }} {{ $rows->currentPage() }} van {{ $rows->lastPage() }} - {{ $rows->total() }} {{ __('datatable::datatables.results') }}
</span>
<span class="text-sm text-gray-600">
{{ __('datatable::datatables.page') }} {{ $currentPage }} {{ __('datatable::datatables.of') }} {{ $rows->lastPage() }} - {{ $rows->total() }} {{ __('datatable::datatables.results') }}
</span>

<div class="flex gap-2">
<button wire:click="previousPage"
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' }}"
{{ $rows->onFirstPage() ? 'disabled' : '' }}>
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' }}"
{{ $currentPage === 1 ? 'disabled' : '' }}>
{{ __('datatable::datatables.previous') }}
</button>

@if ($rows->lastPage() <= 10)
@for ($page = 1; $page <= $rows->lastPage(); $page++)
<button wire:click="gotoPage({{ $page }})"
class="px-4 py-2 rounded {{ $rows->currentPage() == $page ? 'bg-blue-500 text-white' : 'bg-gray-100 hover:bg-gray-200 text-gray-700' }}">
class="px-4 py-2 rounded {{ $currentPage == $page ? 'bg-blue-500 text-white' : 'bg-gray-100 hover:bg-gray-200 text-gray-700' }}">
{{ $page }}
</button>
@endfor
@else

<select wire:change="gotoPage($event.target.value)"
class="border border-gray-300 rounded px-3 py-1 text-sm w-16">
@for ($page = 1; $page <= $rows->lastPage(); $page++)
<option value="{{ $page }}" {{ $rows->currentPage() == $page ? 'selected' : '' }}>
<option value="{{ $page }}" {{ $currentPage == $page ? 'selected' : '' }}>
{{ $page }}
</option>
@endfor
</select>
@endif

{{-- Volgende knop --}}
<button wire:click="nextPage"
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' }}"
{{ $rows->hasMorePages() ? '' : 'disabled' }}>
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' }}"
{{ $currentPage === $rows->lastPage() ? 'disabled' : '' }}>
{{ __('datatable::datatables.next') }}
</button>
</div>
Expand Down
49 changes: 48 additions & 1 deletion src/Livewire/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ class DataTableComponent extends Component
/** @var bool Whether to show the select-all confirmation modal */
public bool $showSelectAllModal = false;

public int $currentPage = 1;

/** @var array Livewire event listeners */
protected $listeners = ['refreshTable' => '$refresh'];

Expand Down Expand Up @@ -245,7 +247,52 @@ public function getRows(): LengthAwarePaginator

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

return $query->paginate($this->perPage);

$total = $query->count();
$rows = $query->skip(($this->currentPage - 1) * $this->perPage)->take($this->perPage)->get();

return new \Illuminate\Pagination\LengthAwarePaginator(
$rows,
$total,
$this->perPage,
$this->currentPage
);
}

/**
* Navigate to a specific page without modifying the URL.
*
* @param int $page The page number to navigate to.
*/
public function gotoPage(int $page): void
{
$this->setPage($page, $this->getPaginationName());
}

/**
* Navigate to the previous page.
*/
public function previousPage(): void
{
$this->setPage($this->page - 1, $this->getPaginationName());
}

/**
* Navigate to the next page.
*/
public function nextPage(): void
{
$this->setPage($this->page + 1, $this->getPaginationName());
}

/**
* Get a unique pagination name for this DataTable instance.
*
* @return string The unique pagination name.
*/
protected function getPaginationName(): string
{
return md5($this->model . implode(',', $this->columns));
}

/**
Expand Down