Skip to content

Commit

Permalink
Merge pull request #1619 from rappasoft/develop
Browse files Browse the repository at this point in the history
v3.1.8 - Release
  • Loading branch information
lrljoe authored Dec 29, 2023
2 parents 269c86b + a0d8411 commit 669269f
Show file tree
Hide file tree
Showing 9 changed files with 339 additions and 61 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to `laravel-livewire-tables` will be documented in this file

## UNRELEASED
### New Features
- Add capability to show/hide Pagination Details (Rows x of y) by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1618
- Add perPage to queryString by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1618

## [v3.1.7] - 2023-12-29
### Bug Fixes
- Fix published view path by @lrljoe in https://github.com/rappasoft/laravel-livewire-tables/pull/1615
Expand Down
20 changes: 20 additions & 0 deletions docs/pagination/available-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,3 +197,23 @@ Returns the number of rows that are currently displayed. This should be used in
```php
$this->getPerPageDisplayedItemCount();
```

## setDisplayPaginationDetailsEnabled
Enables display of Pagination Details (e.g. Displaying Rows x of y) - default behaviour

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

## setDisplayPaginationDetailsDisabled
Disables display of Pagination Details (e.g. Displaying Rows x of y)

```php
public function configure(): void
{
$this->setDisplayPaginationDetailsDisabled();
}
```
82 changes: 46 additions & 36 deletions resources/views/components/pagination.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@
<div>
@if ($component->paginationIsEnabled() && $component->isPaginationMethod('standard') && $rows->lastPage() > 1)
<p class="paged-pagination-results text-sm text-gray-700 leading-5 dark:text-white">
<span>@lang('Showing')</span>
<span class="font-medium">{{ $rows->firstItem() }}</span>
<span>@lang('to')</span>
<span class="font-medium">{{ $rows->lastItem() }}</span>
<span>@lang('of')</span>
<span class="font-medium"><span x-text="paginationTotalItemCount"></span></span>
<span>@lang('results')</span>
@if($component->showPaginationDetails())
<span>@lang('Showing')</span>
<span class="font-medium">{{ $rows->firstItem() }}</span>
<span>@lang('to')</span>
<span class="font-medium">{{ $rows->lastItem() }}</span>
<span>@lang('of')</span>
<span class="font-medium"><span x-text="paginationTotalItemCount"></span></span>
<span>@lang('results')</span>
@endif
</p>
@elseif ($component->paginationIsEnabled() && $component->isPaginationMethod('simple'))
<p class="paged-pagination-results text-sm text-gray-700 leading-5 dark:text-white">
<span>@lang('Showing')</span>
<span class="font-medium">{{ $rows->firstItem() }}</span>
<span>@lang('to')</span>
<span class="font-medium">{{ $rows->lastItem() }}</span>
@if($component->showPaginationDetails())
<span>@lang('Showing')</span>
<span class="font-medium">{{ $rows->firstItem() }}</span>
<span>@lang('to')</span>
<span class="font-medium">{{ $rows->lastItem() }}</span>
@endif
</p>
@elseif ($component->paginationIsEnabled() && $component->isPaginationMethod('cursor'))
@else
Expand Down Expand Up @@ -53,13 +57,15 @@
</div>

<div class="col-12 col-md-6 text-center text-md-right text-muted">
<span>@lang('Showing')</span>
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
<span>@lang('to')</span>
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
<span>@lang('of')</span>
<strong><span x-text="paginationTotalItemCount"></span></strong>
<span>@lang('results')</span>
@if($component->showPaginationDetails())
<span>@lang('Showing')</span>
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
<span>@lang('to')</span>
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
<span>@lang('of')</span>
<strong><span x-text="paginationTotalItemCount"></span></strong>
<span>@lang('results')</span>
@endif
</div>
</div>
@elseif ($component->paginationIsEnabled() && $component->isPaginationMethod('simple'))
Expand All @@ -69,10 +75,12 @@
</div>

<div class="col-12 col-md-6 text-center text-md-right text-muted">
<span>@lang('Showing')</span>
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
<span>@lang('to')</span>
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
@if($component->showPaginationDetails())
<span>@lang('Showing')</span>
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
<span>@lang('to')</span>
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
@endif
</div>
</div>
@elseif ($component->paginationIsEnabled() && $component->isPaginationMethod('cursor'))
Expand Down Expand Up @@ -100,28 +108,30 @@
<div class="col-12 col-md-6 overflow-auto">
{{ $rows->links('livewire-tables::specific.bootstrap-4.pagination') }}
</div>

<div class="col-12 col-md-6 text-center text-md-end text-muted">
<span>@lang('Showing')</span>
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
<span>@lang('to')</span>
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
<span>@lang('of')</span>
<strong><span x-text="paginationTotalItemCount"></span></strong>
<span>@lang('results')</span>
@if($component->showPaginationDetails())
<span>@lang('Showing')</span>
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
<span>@lang('to')</span>
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
<span>@lang('of')</span>
<strong><span x-text="paginationTotalItemCount"></span></strong>
<span>@lang('results')</span>
@endif
</div>
</div>
@elseif ($component->paginationIsEnabled() && $component->isPaginationMethod('simple'))
<div class="row mt-3">
<div class="col-12 col-md-6 overflow-auto">
{{ $rows->links('livewire-tables::specific.bootstrap-4.simple-pagination') }}
</div>

<div class="col-12 col-md-6 text-center text-md-end text-muted">
<span>@lang('Showing')</span>
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
<span>@lang('to')</span>
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
@if($component->showPaginationDetails())
<span>@lang('Showing')</span>
<strong>{{ $rows->count() ? $rows->firstItem() : 0 }}</strong>
<span>@lang('to')</span>
<strong>{{ $rows->count() ? $rows->lastItem() : 0 }}</strong>
@endif
</div>
</div>
@elseif ($component->paginationIsEnabled() && $component->isPaginationMethod('cursor'))
Expand All @@ -145,4 +155,4 @@

@if ($component->hasConfigurableAreaFor('after-pagination'))
@include($component->getConfigurableAreaFor('after-pagination'), $component->getParametersForConfigurableArea('after-pagination'))
@endif
@endif
21 changes: 21 additions & 0 deletions src/Traits/Configuration/PaginationConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,25 @@ public function setPaginationMethod(string $paginationMethod): self

return $this;
}

public function setDisplayPaginationDetails(bool $status): self
{
$this->shouldShowPaginationDetails = $status;

return $this;
}

public function setDisplayPaginationDetailsEnabled(): self
{
$this->setDisplayPaginationDetails(true);

return $this;
}

public function setDisplayPaginationDetailsDisabled(): self
{
$this->setDisplayPaginationDetails(false);

return $this;
}
}
32 changes: 32 additions & 0 deletions src/Traits/Helpers/PaginationHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,36 @@ public function getPerPageDisplayedItemCount(): int
{
return $this->paginationCurrentCount;
}

public function showPaginationDetails(): bool
{
return $this->shouldShowPaginationDetails === true;
}

// TODO: Test
public function setupPagination(): void
{
if ($this->paginationIsDisabled()) {
return;
}

if (in_array(session($this->getPerPagePaginationSessionKey(), $this->getPerPage()), $this->getPerPageAccepted(), true)) {
$this->setPerPage(session($this->getPerPagePaginationSessionKey(), $this->getPerPage()));
} else {
$this->setPerPage($this->getPerPageAccepted()[0] ?? 10);
}
}

/**
* Reset the page using the custom page name
*/
public function resetComputedPage(): void
{
$this->resetPage($this->getComputedPageName());
}

private function getPerPagePaginationSessionKey(): string
{
return $this->tableName.'-perPage';
}
}
43 changes: 19 additions & 24 deletions src/Traits/WithPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,42 @@ trait WithPagination

public array $numberOfPaginatorsRendered = [];

// TODO: Test
public function setupPagination(): void
{
if ($this->paginationIsDisabled()) {
return;
}
protected bool $shouldShowPaginationDetails = true;

if (in_array(session($this->getPerPagePaginationSessionKey(), $this->getPerPage()), $this->getPerPageAccepted(), true)) {
$this->setPerPage(session($this->getPerPagePaginationSessionKey(), $this->getPerPage()));
} else {
$this->setPerPage($this->getPerPageAccepted()[0] ?? 10);
public function mountWithPagination(): void
{
$sessionPerPage = session()->get($this->getPerPagePaginationSessionKey(), $this->getPerPageAccepted()[0] ?? 10);
if (! in_array((int) $sessionPerPage, $this->getPerPageAccepted(), false)) {
$sessionPerPage = $this->getPerPageAccepted()[0] ?? 10;
}
$this->setPerPage($sessionPerPage);
}

// TODO: Test
public function updatedPerPage($value): void
{
if (! in_array($value, $this->getPerPageAccepted(), false)) {
$value = $this->setPerPage($this->getPerPageAccepted()[0] ?? 10);
if (! in_array((int) $value, $this->getPerPageAccepted(), false)) {
$value = $this->getPerPageAccepted()[0] ?? 10;
}

if (in_array(session($this->getPerPagePaginationSessionKey(), $this->getPerPage()), $this->getPerPageAccepted(), true)) {
if (in_array(session($this->getPerPagePaginationSessionKey(), (int) $value), $this->getPerPageAccepted(), true)) {
session()->put($this->getPerPagePaginationSessionKey(), (int) $value);
} else {
session()->put($this->getPerPagePaginationSessionKey(), $this->getPerPageAccepted()[0] ?? 10);
}

$this->resetComputedPage();
$this->setPerPage($value);
}

/**
* Reset the page using the custom page name
*/
public function resetComputedPage(): void
protected function queryStringWithPagination(): array
{
$this->resetPage($this->getComputedPageName());
}

private function getPerPagePaginationSessionKey(): string
{
return $this->tableName.'-perPage';
if ($this->queryStringIsEnabled()) {
return [
'perPage' => ['except' => null, 'history' => false, 'keep' => false, 'as' => $this->getQueryStringAlias().'perPage'],
];
}

return [];
}

public function renderingWithPagination(): void
Expand Down
22 changes: 22 additions & 0 deletions tests/Http/Livewire/PetsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@ class PetsTable extends DataTableComponent
{
public $model = Pet::class;

public string $paginationTest = 'standard';

public function enableDetailedPagination(string $type = 'standard')
{
$this->setPerPageAccepted([1, 3, 5, 10, 15, 25, 50])->setPerPage(3);
$this->setPaginationMethod($type);
$this->setDisplayPaginationDetailsEnabled();

}

public function disableDetailedPagination(string $type = 'standard')
{
$this->setPerPageAccepted([1, 3, 5, 10, 15, 25, 50])->setPerPage(3);
$this->setPaginationMethod($type);
$this->setDisplayPaginationDetailsDisabled();
}

public function setPaginationTest(string $type)
{
$this->paginationTest = $type;
}

public function configure(): void
{
$this->setPrimaryKey('id');
Expand Down
27 changes: 27 additions & 0 deletions tests/Traits/Helpers/PaginationHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,31 @@ public function can_check_per_page_displayed_items(): void
$this->assertSame([1, 2, 3, 4, 5], $this->basicTable->getPerPageDisplayedItemIds());

}

/** @test */
public function can_enable_detailed_pagination(): void
{

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

$this->basicTable->setDisplayPaginationDetailsDisabled();

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

$this->basicTable->setDisplayPaginationDetailsEnabled();

$this->assertTrue($this->basicTable->showPaginationDetails());
}

/** @test */
public function can_disable_detailed_pagination(): void
{

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

$this->basicTable->setDisplayPaginationDetailsDisabled();

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

}
}
Loading

0 comments on commit 669269f

Please sign in to comment.