Skip to content

Commit 8eacdfb

Browse files
committed
Added ability to set the pagination mode of standard or simple
1 parent 8e67e05 commit 8eacdfb

File tree

10 files changed

+146
-50
lines changed

10 files changed

+146
-50
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
1111
- Added support for optgroups in SelectFilter - https://github.com/rappasoft/laravel-livewire-tables/pull/962
1212
- Added information about applying filters on boot - https://github.com/rappasoft/laravel-livewire-tables/pull/949
1313
- Added ComponentColumn - https://github.com/rappasoft/laravel-livewire-tables/pull/827
14+
- Added ability to set the pagination mode of `standard` or `simple`
1415

1516
### Changed
1617

docs/pagination/available-methods.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,16 @@ public function configure(): void
159159
```
160160

161161
**Note:** The value set must be included in the `per page accepted` values.
162+
163+
## setPaginationMethod
164+
165+
Set the pagination method. By default, the table will use the `paginate` method.
166+
167+
You may specify `simplePaginate` like so:
168+
169+
```php
170+
public function configure(): void
171+
{
172+
$this->setPaginationMethod('simple');
173+
}
174+
```

resources/views/components/pagination.blade.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
@if ($component->paginationVisibilityIsEnabled())
1515
<div class="mt-4 px-4 md:p-0 sm:flex justify-between items-center space-y-4 sm:space-y-0">
1616
<div>
17-
@if ($component->paginationIsEnabled() && $rows->lastPage() > 1)
17+
@if ($component->paginationIsEnabled() && $component->isPaginationMethod('standard') && $rows->lastPage() > 1)
1818
<p class="paged-pagination-results text-sm text-gray-700 leading-5 dark:text-white">
1919
<span>@lang('Showing')</span>
2020
<span class="font-medium">{{ $rows->firstItem() }}</span>
@@ -24,6 +24,13 @@
2424
<span class="font-medium">{{ $rows->total() }}</span>
2525
<span>@lang('results')</span>
2626
</p>
27+
@elseif ($component->paginationIsEnabled() && $component->isPaginationMethod('simple'))
28+
<p class="paged-pagination-results text-sm text-gray-700 leading-5 dark:text-white">
29+
<span>@lang('Showing')</span>
30+
<span class="font-medium">{{ $rows->firstItem() }}</span>
31+
<span>@lang('to')</span>
32+
<span class="font-medium">{{ $rows->lastItem() }}</span>
33+
</p>
2734
@else
2835
<p class="total-pagination-results text-sm text-gray-700 leading-5 dark:text-white">
2936
@lang('Showing')
@@ -42,7 +49,7 @@
4249
@elseif ($theme === 'bootstrap-4')
4350
<div>
4451
@if ($component->paginationVisibilityIsEnabled())
45-
@if ($component->paginationIsEnabled() && $rows->lastPage() > 1)
52+
@if ($component->paginationIsEnabled() && $component->isPaginationMethod('standard') && $rows->lastPage() > 1)
4653
<div class="row mt-3">
4754
<div class="col-12 col-md-6 overflow-auto">
4855
{{ $rows->links('livewire-tables::specific.bootstrap-4.pagination') }}
@@ -58,6 +65,19 @@
5865
<span>@lang('results')</span>
5966
</div>
6067
</div>
68+
@elseif ($component->paginationIsEnabled() && $component->isPaginationMethod('simple'))
69+
<div class="row mt-3">
70+
<div class="col-12 col-md-6 overflow-auto">
71+
{{ $rows->links('livewire-tables::specific.bootstrap-4.pagination') }}
72+
</div>
73+
74+
<div class="col-12 col-md-6 text-center text-md-right text-muted">
75+
<span>@lang('Showing')</span>
76+
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
77+
<span>@lang('to')</span>
78+
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
79+
</div>
80+
</div>
6181
@else
6282
<div class="row mt-3">
6383
<div class="col-12 text-muted">
@@ -72,7 +92,7 @@
7292
@elseif ($theme === 'bootstrap-5')
7393
<div>
7494
@if ($component->paginationVisibilityIsEnabled())
75-
@if ($component->paginationIsEnabled() && $rows->lastPage() > 1)
95+
@if ($component->paginationIsEnabled() && $component->isPaginationMethod('standard') && $rows->lastPage() > 1)
7696
<div class="row mt-3">
7797
<div class="col-12 col-md-6 overflow-auto">
7898
{{ $rows->links('livewire-tables::specific.bootstrap-4.pagination') }}
@@ -88,6 +108,19 @@
88108
<span>@lang('results')</span>
89109
</div>
90110
</div>
111+
@elseif ($component->paginationIsEnabled() && $component->isPaginationMethod('simple'))
112+
<div class="row mt-3">
113+
<div class="col-12 col-md-6 overflow-auto">
114+
{{ $rows->links('livewire-tables::specific.bootstrap-4.pagination') }}
115+
</div>
116+
117+
<div class="col-12 col-md-6 text-center text-md-end text-muted">
118+
<span>@lang('Showing')</span>
119+
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
120+
<span>@lang('to')</span>
121+
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
122+
</div>
123+
</div>
91124
@else
92125
<div class="row mt-3">
93126
<div class="col-12 text-muted">
@@ -103,4 +136,4 @@
103136

104137
@if ($component->hasConfigurableAreaFor('after-pagination'))
105138
@include($component->getConfigurableAreaFor('after-pagination'), $component->getParametersForConfigurableArea('after-pagination'))
106-
@endif
139+
@endif
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<div>
22
@if ($paginator->hasPages())
33
@php(isset($this->numberOfPaginatorsRendered[$paginator->getPageName()]) ? $this->numberOfPaginatorsRendered[$paginator->getPageName()]++ : $this->numberOfPaginatorsRendered[$paginator->getPageName()] = 1)
4-
4+
55
<nav>
66
<ul class="pagination">
77
{{-- Previous Page Link --}}
@@ -16,23 +16,25 @@
1616
@endif
1717

1818
{{-- Pagination Elements --}}
19-
@foreach ($elements as $element)
20-
{{-- "Three Dots" Separator --}}
21-
@if (is_string($element))
22-
<li class="page-item disabled" aria-disabled="true"><span class="page-link">{{ $element }}</span></li>
23-
@endif
19+
@if ($elements ?? null)
20+
@foreach ($elements as $element)
21+
{{-- "Three Dots" Separator --}}
22+
@if (is_string($element))
23+
<li class="page-item disabled" aria-disabled="true"><span class="page-link">{{ $element }}</span></li>
24+
@endif
2425

25-
{{-- Array Of Links --}}
26-
@if (is_array($element))
27-
@foreach ($element as $page => $url)
28-
@if ($page == $paginator->currentPage())
29-
<li class="page-item active" wire:key="paginator-{{ $paginator->getPageName() }}-{{ $this->numberOfPaginatorsRendered[$paginator->getPageName()] }}-page-{{ $page }}" aria-current="page"><span class="page-link">{{ $page }}</span></li>
30-
@else
31-
<li class="page-item" wire:key="paginator-{{ $paginator->getPageName() }}-{{ $this->numberOfPaginatorsRendered[$paginator->getPageName()] }}-page-{{ $page }}"><button type="button" class="page-link" wire:click="gotoPage({{ $page }}, '{{ $paginator->getPageName() }}')">{{ $page }}</button></li>
32-
@endif
33-
@endforeach
34-
@endif
35-
@endforeach
26+
{{-- Array Of Links --}}
27+
@if (is_array($element))
28+
@foreach ($element as $page => $url)
29+
@if ($page == $paginator->currentPage())
30+
<li class="page-item active" wire:key="paginator-{{ $paginator->getPageName() }}-{{ $this->numberOfPaginatorsRendered[$paginator->getPageName()] }}-page-{{ $page }}" aria-current="page"><span class="page-link">{{ $page }}</span></li>
31+
@else
32+
<li class="page-item" wire:key="paginator-{{ $paginator->getPageName() }}-{{ $this->numberOfPaginatorsRendered[$paginator->getPageName()] }}-page-{{ $page }}"><button type="button" class="page-link" wire:click="gotoPage({{ $page }}, '{{ $paginator->getPageName() }}')">{{ $page }}</button></li>
33+
@endif
34+
@endforeach
35+
@endif
36+
@endforeach
37+
@endif
3638

3739
{{-- Next Page Link --}}
3840
@if ($paginator->hasMorePages())
@@ -47,4 +49,4 @@
4749
</ul>
4850
</nav>
4951
@endif
50-
</div>
52+
</div>

resources/views/specific/tailwind/pagination.blade.php

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,31 +52,33 @@
5252
</span>
5353

5454
{{-- Pagination Elements --}}
55-
@foreach ($elements as $element)
56-
{{-- "Three Dots" Separator --}}
57-
@if (is_string($element))
58-
<span aria-disabled="true">
59-
<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-700 bg-white border border-gray-300 cursor-default leading-5 dark:bg-gray-700 dark:text-white dark:border-gray-600">{{ $element }}</span>
60-
</span>
61-
@endif
62-
63-
{{-- Array Of Links --}}
64-
@if (is_array($element))
65-
@foreach ($element as $page => $url)
66-
<span wire:key="paginator-{{ $paginator->getPageName() }}-{{ $this->numberOfPaginatorsRendered[$paginator->getPageName()] }}-page{{ $page }}">
67-
@if ($page == $paginator->currentPage())
68-
<span aria-current="page">
69-
<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5 dark:bg-gray-500 dark:text-white dark:border-gray-500">{{ $page }}</span>
70-
</span>
71-
@else
72-
<button wire:click="gotoPage({{ $page }}, '{{ $paginator->getPageName() }}')" class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 hover:text-gray-500 focus:z-10 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150 dark:bg-gray-700 dark:text-white dark:ring-gray-600 dark:border-gray-600 dark:hover:bg-gray-600" aria-label="{{ __('Go to page :page', ['page' => $page]) }}">
73-
{{ $page }}
74-
</button>
75-
@endif
55+
@if ($elements ?? null)
56+
@foreach ($elements as $element)
57+
{{-- "Three Dots" Separator --}}
58+
@if (is_string($element))
59+
<span aria-disabled="true">
60+
<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-700 bg-white border border-gray-300 cursor-default leading-5 dark:bg-gray-700 dark:text-white dark:border-gray-600">{{ $element }}</span>
7661
</span>
77-
@endforeach
78-
@endif
79-
@endforeach
62+
@endif
63+
64+
{{-- Array Of Links --}}
65+
@if (is_array($element))
66+
@foreach ($element as $page => $url)
67+
<span wire:key="paginator-{{ $paginator->getPageName() }}-{{ $this->numberOfPaginatorsRendered[$paginator->getPageName()] }}-page{{ $page }}">
68+
@if ($page == $paginator->currentPage())
69+
<span aria-current="page">
70+
<span class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-500 bg-white border border-gray-300 cursor-default leading-5 dark:bg-gray-500 dark:text-white dark:border-gray-500">{{ $page }}</span>
71+
</span>
72+
@else
73+
<button wire:click="gotoPage({{ $page }}, '{{ $paginator->getPageName() }}')" class="relative inline-flex items-center px-4 py-2 -ml-px text-sm font-medium text-gray-700 bg-white border border-gray-300 leading-5 hover:text-gray-500 focus:z-10 focus:outline-none focus:border-blue-300 focus:shadow-outline-blue active:bg-gray-100 active:text-gray-700 transition ease-in-out duration-150 dark:bg-gray-700 dark:text-white dark:ring-gray-600 dark:border-gray-600 dark:hover:bg-gray-600" aria-label="{{ __('Go to page :page', ['page' => $page]) }}">
74+
{{ $page }}
75+
</button>
76+
@endif
77+
</span>
78+
@endforeach
79+
@endif
80+
@endforeach
81+
@endif
8082

8183
<span>
8284
{{-- Next Page Link --}}

src/Traits/Configuration/PaginationConfiguration.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,4 +154,16 @@ public function setPerPage(int $perPage): self
154154

155155
return $this;
156156
}
157+
158+
/**
159+
* @param string $paginationMethod
160+
*
161+
* @return $this
162+
*/
163+
public function setPaginationMethod(string $paginationMethod): self
164+
{
165+
$this->paginationMethod = $paginationMethod;
166+
167+
return $this;
168+
}
157169
}

src/Traits/Helpers/PaginationHelpers.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,14 @@ public function perPageVisibilityIsDisabled(): bool
133133
{
134134
return $this->getPerPageVisibilityStatus() === false;
135135
}
136+
137+
/**
138+
* @param string $paginationMethod
139+
*
140+
* @return bool
141+
*/
142+
public function isPaginationMethod(string $paginationMethod): bool
143+
{
144+
return $this->paginationMethod === $paginationMethod;
145+
}
136146
}

src/Traits/WithData.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait WithData
1313
public function getRows()
1414
{
1515
$this->baseQuery();
16-
16+
1717
return $this->executeQuery();
1818
}
1919

@@ -38,9 +38,17 @@ protected function baseQuery(): Builder
3838

3939
protected function executeQuery()
4040
{
41-
return $this->paginationIsEnabled() ?
42-
$this->getBuilder()->paginate($this->getPerPage() === -1 ? $this->getBuilder()->count() : $this->getPerPage(), ['*'], $this->getComputedPageName()) :
43-
$this->getBuilder()->get();
41+
if ($this->paginationIsEnabled()) {
42+
if ($this->isPaginationMethod('standard')) {
43+
return $this->getBuilder()->paginate($this->getPerPage() === -1 ? $this->getBuilder()->count() : $this->getPerPage(), ['*'], $this->getComputedPageName());
44+
}
45+
46+
if ($this->isPaginationMethod('simple')) {
47+
return $this->getBuilder()->simplePaginate($this->getPerPage() === -1 ? $this->getBuilder()->count() : $this->getPerPage(), ['*'], $this->getComputedPageName());
48+
}
49+
}
50+
51+
return $this->getBuilder()->get();
4452
}
4553

4654
protected function joinRelations(): Builder

src/Traits/WithPagination.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ trait WithPagination
1919
public bool $paginationStatus = true;
2020
public bool $paginationVisibilityStatus = true;
2121
public bool $perPageVisibilityStatus = true;
22+
public string $paginationMethod = 'standard';
2223

2324
// TODO: Test
2425
public function setupPagination(): void

tests/Traits/Helpers/PaginationHelpersTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,18 @@ public function can_get_per_page_visibility_status(): void
7373

7474
$this->assertTrue($this->basicTable->perPageVisibilityIsEnabled());
7575
}
76+
77+
/** @test */
78+
public function can_check_and_set_pagination_method(): void
79+
{
80+
$this->assertTrue($this->basicTable->isPaginationMethod('standard'));
81+
82+
$this->basicTable->setPaginationMethod('simple');
83+
84+
$this->assertTrue($this->basicTable->isPaginationMethod('simple'));
85+
86+
$this->basicTable->setPaginationMethod('standard');
87+
88+
$this->assertTrue($this->basicTable->isPaginationMethod('standard'));
89+
}
7690
}

0 commit comments

Comments
 (0)