Skip to content

Commit 0b8691c

Browse files
committed
making pagination options with property $pagination. added tests.
1 parent 6b112ce commit 0b8691c

File tree

9 files changed

+65
-9
lines changed

9 files changed

+65
-9
lines changed

resources/views/bootstrap-4/includes/pagination.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showPagination)
1+
@if ($pagination ?? false || $showPerPage ?? false)
22
<div class="row">
33
<div class="col-12 col-md-6">
44
{{ $rows->links() }}

resources/views/bootstrap-4/includes/per-page.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showPerPage)
1+
@if ($pagination ?? false || $showPerPage ?? false)
22
<select
33
wire:model="perPage"
44
id="perPage"

resources/views/bootstrap-5/includes/pagination.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showPagination)
1+
@if ($pagination ?? false || $showPerPage ?? false)
22
<div class="row">
33
<div class="col-12 col-md-6">
44
{{ $rows->links() }}

resources/views/bootstrap-5/includes/per-page.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showPerPage)
1+
@if ($pagination ?? false || $showPerPage ?? false)
22
<select
33
wire:model="perPage"
44
id="perPage"

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showPagination)
1+
@if ($pagination ?? false && $showPerPage ?? false)
22
<div class="p-6 md:p-0">
33
{{ $rows->links() }}
44
</div>

resources/views/tailwind/includes/per-page.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@if ($showPerPage)
1+
@if ($pagination ?? false && $showPerPage ?? false)
22
<select
33
wire:model="perPage"
44
id="perPage"

src/DataTableComponent.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Database\Eloquent\Builder;
66
use Illuminate\Pagination\LengthAwarePaginator;
7+
use Illuminate\Support\Collection;
78
use Livewire\Component;
89
use Rappasoft\LaravelLivewireTables\Traits\WithBulkActions;
910
use Rappasoft\LaravelLivewireTables\Traits\WithCustomPagination;
@@ -13,6 +14,8 @@
1314

1415
/**
1516
* Class TableComponent.
17+
*
18+
* @property LengthAwarePaginator|Collection|null $rows
1619
*/
1720
abstract class DataTableComponent extends Component
1821
{
@@ -159,11 +162,15 @@ public function getRowsQueryProperty(): Builder
159162
/**
160163
* Get the rows paginated collection that will be returned to the view.
161164
*
162-
* @return LengthAwarePaginator
165+
* @return LengthAwarePaginator|Collection
163166
*/
164-
public function getRowsProperty(): LengthAwarePaginator
167+
public function getRowsProperty()
165168
{
166-
return $this->applyPagination($this->rowsQuery);
169+
if ($this->pagination ?? false === true) {
170+
return $this->applyPagination($this->rowsQuery);
171+
} else {
172+
return $this->rowsQuery->get();
173+
}
167174
}
168175

169176
/**

src/Traits/WithPerPagePagination.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,21 @@
77
*/
88
trait WithPerPagePagination
99
{
10+
/**
11+
* Enable / Disable Pagination
12+
*
13+
* @var bool
14+
*/
15+
public bool $pagination = true;
16+
17+
/**
18+
* @var int
19+
*/
1020
public int $perPage = 10;
21+
22+
/**
23+
* @var array|int[]
24+
*/
1125
protected array $perPageAccepted = [10, 25, 50];
1226

1327
public function mountWithPerPagePagination(): void
@@ -19,6 +33,9 @@ public function mountWithPerPagePagination(): void
1933
}
2034
}
2135

36+
/**
37+
* @param $value
38+
*/
2239
public function updatedPerPage($value): void
2340
{
2441
if (in_array(session()->get($this->tableName.'-perPage', $this->perPage), $this->perPageAccepted, true)) {
@@ -30,6 +47,10 @@ public function updatedPerPage($value): void
3047
$this->resetPage();
3148
}
3249

50+
/**
51+
* @param $query
52+
* @return mixed
53+
*/
3354
public function applyPagination($query)
3455
{
3556
return $query->paginate($this->perPage, ['*'], $this->pageName());

tests/DataTableComponentTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables\Tests;
44

55
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6+
use Illuminate\Support\Collection;
67
use Rappasoft\LaravelLivewireTables\DataTableComponent;
78
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
89

@@ -40,6 +41,33 @@ public function test_rows(): void
4041
$this->assertEquals(5, $this->table->getRowsProperty()->total());
4142
}
4243

44+
/** @test */
45+
public function test_pagination_default(): void
46+
{
47+
$this->assertInstanceOf(LengthAwarePaginator::class, $this->table->rows);
48+
$this->assertEquals(10, $this->table->perPage);
49+
$this->assertTrue($this->table->pagination);
50+
$this->assertTrue($this->table->showPerPage);
51+
}
52+
53+
/** @test */
54+
public function test_pagination(): void
55+
{
56+
$this->table->perPage = 2;
57+
$this->assertEquals(1, $this->table->rows->currentPage());
58+
$this->assertEquals(2, $this->table->rows->count());
59+
$this->assertEquals(3, $this->table->rows->lastPage());
60+
}
61+
62+
/** @test */
63+
public function test_pagination_disabled(): void
64+
{
65+
$this->table->pagination = false;
66+
$this->table->perPage = 2;
67+
$this->assertInstanceOf(Collection::class, $this->table->rows);
68+
$this->assertCount(5, $this->table->rows);
69+
}
70+
4371
/** @test */
4472
public function test_search_filter(): void
4573
{

0 commit comments

Comments
 (0)