rappasoft / laravel-livewire-tables

A dynamic table component for Laravel Livewire
https://rappasoft.com/docs/laravel-livewire-tables/v2/introduction
MIT License
1.8k stars 340 forks source link

[Bug]: Changing 'Per Page' Dropdown on Non-First Page Shows 0 Results #1946

Closed red-cruz closed 2 months ago

red-cruz commented 2 months ago

What happened?

The issue arises when changing the "Per Page" dropdown value after navigating to a different page. Specifically:

  1. I navigated to page 2 of my data table.
  2. I changed the "Per Page" dropdown value to something other than the default (10).
  3. The table displayed 0 results when it should have shown the correct set of paginated data.

How to reproduce the bug

  1. Navigate to page 2 of the data table.

  2. Change the "Per Page" dropdown value from 10 to 25.

  3. The table displays 0 results.

Package Version

3.4.20

PHP Version

8.3.x

Laravel Version

11.9

Alpine Version

No response

Theme

Bootstrap 4.x

Notes

My Component


namespace App\Livewire\Datatables\Admin;

use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\Views\Columns\ButtonGroupColumn;
use Rappasoft\LaravelLivewireTables\Views\Columns\ImageColumn;
use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
use Rappasoft\LaravelLivewireTables\Views\Filters\DateRangeFilter;

class UsersTable extends DataTableComponent
{
  protected $model = User::class;

  public function configure(): void
  {
    $this->setPrimaryKey('id');
    $this->setSingleSortingDisabled();
    $this->setLoadingPlaceholderStatus(true);
  }

  public function columns(): array
  {
    return [
      Column::make('id')->hideIf(true)->eagerLoadRelations(),
      ImageColumn::make('Profile')->location(fn($row) => $row->profile_url)->attributes(
        fn($row) => [
          'class' => 'rounded-circle border border-primary',
          'style' => 'object-fit: cover; width: 32px; height: 32px;',
          'loading' => 'lazy',
        ]
      ),
      Column::make('Role', 'role')->sortable()->searchable()->excludeFromColumnSelect(),
      Column::make('Username', 'username')->sortable()->searchable()->excludeFromColumnSelect(),
      Column::make('Email', 'email')->sortable()->searchable(),
      Column::make('Joined At', 'created_at')->sortable()->searchable(),
      ButtonGroupColumn::make('Actions')->buttons([
        LinkColumn::make('Visit') // make() has no effect in this case but needs to be set anyway
          ->title(fn() => '<i class="fa-solid fa-user-pen"></i>')
          ->location(fn($row) => route('profile', $row))
          ->attributes(function ($row) {
            return [
              'class' => 'btn btn-sm btn-primary mb-1',
              'wire:navigate' => true,
            ];
          })
          ->html(),
      ]),
    ];
  }

  public function filters(): array
  {
    return [
      DateRangeFilter::make('Join date')
        ->config([
          'allowInput' => true,
          'altFormat' => 'F j, Y',
          'placeholder' => 'Enter Date Range',
        ])
        ->filter(function (Builder $builder, array $dateRange) {
          $builder->whereDate('users.created_at', '>=', $dateRange['minDate'])->whereDate('users.created_at', '<=', $dateRange['maxDate']);
        }),
    ];
  }
}

Error Message

No response

lrljoe commented 2 months ago

Thanks for raising!

Looks like the function that resets the "current page" when "per page" is changed was removed at some point.

Fix is ready to go, and I will merge it in for next release (will be later this week)

If you would like to test it (alongside other fixes & features), then it is in the "development" branch now.