Skip to content

Commit

Permalink
Optionally disable count for simple pagination (#1755)
Browse files Browse the repository at this point in the history
* Add option for setShouldRetrieveTotalItemCountStatus

* Fix styling

---------

Co-authored-by: lrljoe <lrljoe@users.noreply.github.com>
  • Loading branch information
lrljoe and lrljoe committed Jul 1, 2024
1 parent 318bdeb commit 3633743
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 7 deletions.
33 changes: 33 additions & 0 deletions docs/pagination/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,36 @@ public function configure(): void
]);
}
```

## setShouldRetrieveTotalItemCountStatus

Used when "simple" pagination is being used, allows the enabling/disabling of the "total records" count. This may be desirable to disable in larger data sets. This is enabled by default.

```php
public function configure(): void
{
$this->setShouldRetrieveTotalItemCountStatus(false);
}
```

## setShouldRetrieveTotalItemCountEnabled

Used when "simple" pagination is being used, enables the "total records" count.

```php
public function configure(): void
{
$this->setShouldRetrieveTotalItemCountEnabled();
}
```

## setShouldRetrieveTotalItemCountDisabled

Used when "simple" pagination is being used, disables the "total records" count.

```php
public function configure(): void
{
$this->setShouldRetrieveTotalItemCountDisabled();
}
```
22 changes: 22 additions & 0 deletions src/Traits/Configuration/PaginationConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,26 @@ public function setPerPageFieldAttributes(array $attributes = []): self

return $this;
}

public function setShouldRetrieveTotalItemCountStatus(bool $status): self
{
$this->shouldRetrieveTotalItemCount = $status;

return $this;

}

public function setShouldRetrieveTotalItemCountEnabled(): self
{
$this->setShouldRetrieveTotalItemCountStatus(true);

return $this;
}

public function setShouldRetrieveTotalItemCountDisabled(): self
{
$this->setShouldRetrieveTotalItemCountStatus(false);

return $this;
}
}
6 changes: 6 additions & 0 deletions src/Traits/Helpers/PaginationHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,10 @@ public function getPerPageFieldAttributes(): array
{
return $this->perPageFieldAttributes;
}

#[Computed]
public function getShouldRetrieveTotalItemCount(): bool
{
return $this->shouldRetrieveTotalItemCount;
}
}
18 changes: 11 additions & 7 deletions src/Traits/WithData.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,25 @@ protected function executeQuery(): Collection|CursorPaginator|Paginator|LengthAw
$this->paginationTotalItemCount = $paginatedResults->total() ?? 0;

return $paginatedResults;
}

if ($this->isPaginationMethod('simple')) {
} elseif ($this->isPaginationMethod('simple')) {

$this->paginationTotalItemCount = $this->getBuilder()->count();
if ($this->getShouldRetrieveTotalItemCount()) {
$this->paginationTotalItemCount = $this->getBuilder()->count();

return $this->getBuilder()->simplePaginate($this->getPerPage() === -1 ? $this->paginationTotalItemCount : $this->getPerPage(), ['*'], $this->getComputedPageName());
return $this->getBuilder()->simplePaginate($this->getPerPage() === -1 ? $this->paginationTotalItemCount : $this->getPerPage(), ['*'], $this->getComputedPageName());
} else {
$this->paginationTotalItemCount = -1;

}
return $this->getBuilder()->simplePaginate($this->getPerPage() === -1 ? 10 : $this->getPerPage(), ['*'], $this->getComputedPageName());
}

if ($this->isPaginationMethod('cursor')) {
} elseif ($this->isPaginationMethod('cursor')) {

$this->paginationTotalItemCount = $this->getBuilder()->count();

return $this->getBuilder()->cursorPaginate($this->getPerPage() === -1 ? $this->paginationTotalItemCount : $this->getPerPage(), ['*'], $this->getComputedPageName());
} else {
throw new DataTableConfigurationException('Pagination method must be either simple, standard or cursor');
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/Traits/WithPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ trait WithPagination

protected array $perPageFieldAttributes = ['default-styling' => true, 'default-colors' => true, 'class' => ''];

protected bool $shouldRetrieveTotalItemCount = true;

public function mountWithPagination(): void
{
$sessionPerPage = session()->get($this->getPerPagePaginationSessionKey(), $this->getPerPageAccepted()[0] ?? 10);
Expand Down
30 changes: 30 additions & 0 deletions tests/Traits/Helpers/PaginationHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,34 @@ public function test_can_get_pagination_field_attributes(): void
$this->assertSame(['default-styling' => false, 'default-colors' => true, 'class' => 'bg-blue-500 dark:bg-red-500'], $this->basicTable->getPerPageFieldAttributes());

}

public function test_can_toggle_total_item_count_retrieval(): void
{

$this->assertTrue($this->basicTable->getShouldRetrieveTotalItemCount());

$this->basicTable->setShouldRetrieveTotalItemCountDisabled();

$this->assertFalse($this->basicTable->getShouldRetrieveTotalItemCount());

$this->basicTable->setShouldRetrieveTotalItemCountEnabled();

$this->assertTrue($this->basicTable->getShouldRetrieveTotalItemCount());

}

public function test_can_toggle_total_item_count_retrieval_via_status(): void
{

$this->assertTrue($this->basicTable->getShouldRetrieveTotalItemCount());

$this->basicTable->setShouldRetrieveTotalItemCountStatus(false);

$this->assertFalse($this->basicTable->getShouldRetrieveTotalItemCount());

$this->basicTable->setShouldRetrieveTotalItemCountStatus(true);

$this->assertTrue($this->basicTable->getShouldRetrieveTotalItemCount());

}
}

0 comments on commit 3633743

Please sign in to comment.