diff --git a/CHANGELOG.md b/CHANGELOG.md index 05cdd9865..6ede90f3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ All notable changes to `laravel-livewire-tables` will be documented in this file - Set Row for Filter (setFilterSlidedownRow(()) - Set ColSpan for Filter (setFilterSlidedownColspan()) - Workflow (run-phpstan) - Introducing PHP-Stan at Level 3 to ensure code-quality persists - https://github.com/rappasoft/laravel-livewire-tables/pull/1133 +- Added getPerPageDisplayedItemIds() and getPerPageDisplayedItemCount() along with public variables to allow retrieval of currently paginated PrimayKey and Item Count https://github.com/rappasoft/laravel-livewire-tables/pull/1136 ### Changed diff --git a/docs/pagination/available-methods.md b/docs/pagination/available-methods.md index 76e300154..380d92f0e 100644 --- a/docs/pagination/available-methods.md +++ b/docs/pagination/available-methods.md @@ -172,3 +172,19 @@ public function configure(): void $this->setPaginationMethod('simple'); } ``` + +## getPerPageDisplayedItemIds + +Returns the Primary Key for the currently visible rows in an array. This should be used in a blade to ensure accuracy. + +```php + $this->getPerPageDisplayedItemIds(); +``` + +## getPerPageDisplayedItemCount + +Returns the number of rows that are currently displayed. This should be used in a blade to ensure accuracy. + +```php + $this->getPerPageDisplayedItemCount(); +``` diff --git a/src/Traits/Helpers/PaginationHelpers.php b/src/Traits/Helpers/PaginationHelpers.php index d9e3403ff..5357982d1 100644 --- a/src/Traits/Helpers/PaginationHelpers.php +++ b/src/Traits/Helpers/PaginationHelpers.php @@ -143,4 +143,20 @@ public function isPaginationMethod(string $paginationMethod): bool { return $this->paginationMethod === $paginationMethod; } + + /** + * @return array + */ + public function getPerPageDisplayedItemIds(): array + { + return $this->paginationCurrentItems; + } + + /** + * @return int + */ + public function getPerPageDisplayedItemCount(): int + { + return $this->paginationCurrentCount; + } } diff --git a/src/Traits/WithData.php b/src/Traits/WithData.php index d68625fdb..99fa45dfc 100644 --- a/src/Traits/WithData.php +++ b/src/Traits/WithData.php @@ -15,7 +15,12 @@ public function getRows() { $this->baseQuery(); - return $this->executeQuery(); + $executedQuery = $this->executeQuery(); + + $this->paginationCurrentItems = $executedQuery->pluck($this->getPrimaryKey())->toArray() ?? []; + $this->paginationCurrentCount = $executedQuery->count() ?? 0; + + return $executedQuery; } protected function baseQuery(): Builder diff --git a/src/Traits/WithPagination.php b/src/Traits/WithPagination.php index 74cfe3e18..f601d9995 100644 --- a/src/Traits/WithPagination.php +++ b/src/Traits/WithPagination.php @@ -20,6 +20,8 @@ trait WithPagination public bool $paginationVisibilityStatus = true; public bool $perPageVisibilityStatus = true; public string $paginationMethod = 'standard'; + public array $paginationCurrentItems = []; + public int $paginationCurrentCount = 0; // TODO: Test public function setupPagination(): void diff --git a/tests/Traits/Visuals/PaginationVisualsTest.php b/tests/Traits/Visuals/PaginationVisualsTest.php index 8fc9cdaa2..48f0ef98b 100644 --- a/tests/Traits/Visuals/PaginationVisualsTest.php +++ b/tests/Traits/Visuals/PaginationVisualsTest.php @@ -149,4 +149,18 @@ public function per_page_dropdown_only_renders_with_accepted_values(): void Livewire::test(PetsTable::class) ->call('setPerPage', 15); } + + /** @test */ + public function can_get_currently_displayed_ids(): void + { + Livewire::test(PetsTable::class)->assertSet('paginationCurrentItems', [1,2,3,4,5]) + ->assertNotSet('paginationCurrentItems', [1,2,3,4,5,6,7,8,9]); + } + + /** @test */ + public function can_get_currently_displayed_count(): void + { + Livewire::test(PetsTable::class)->assertSet('paginationCurrentCount', 5) + ->assertNotSet('paginationCurrentCount', 125); + } }