filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
17.65k stars 2.76k forks source link

Filters and pagination don't seem to play well together #721

Closed blite closed 2 years ago

blite commented 2 years ago

Package

filament/tables

Package Version

v2.4.38

Laravel Version

v8.73.2

Livewire Version

v2.7.2

PHP Version

8.0.5

Bug description

It seems that you can't use pagination while filtering, clicking a pagination link seems to clear the filter.

Additionally it seems you can only use one filter at a time.

I also don't really understand why the pagination updates the url, but the per page options do not. I think the filters and search should also update the url.

It also appears that searching also resets the filters, but searching does work correctly with pagination.

Steps to reproduce

No response

Relevant log output

No response

danharrin commented 2 years ago

Can I see some code please?

blite commented 2 years ago
<?php

namespace App\Http\Livewire;

use App\Models\Post;
use App\Models\Category;

use Filament\Tables;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;

use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Builder;
use Livewire\Component;

class ListPost extends Component implements Tables\Contracts\HasTable
{
    use Tables\Concerns\InteractsWithTable;

    protected function getTableQuery(): Builder
    {
        return Post::query();
    }

    protected function getTableColumns(): array
    {
        return [
            Tables\Columns\TextColumn::make('title')->sortable()->searchable()->url(
                fn (Post $record): string => route('post.show', ['guide' => $record->slug])
            ),
            Tables\Columns\TextColumn::make('subtitle')->sortable()->searchable()->url(
                fn (Post $record): string => route('post.show', ['guide' => $record->slug])
            ),
            Tables\Columns\TextColumn::make('category.title'),
        ];
    }

    protected function getTableFilters(): array
    {
        return [
            Filter::make('is_active')->label('Active')
                ->query(fn (Builder $query): Builder => $query->where('is_active', true)),
            SelectFilter::make('category')
                ->options(Category::all()->pluck('title', 'id')->toArray())->column('category_id')
        ];
    }

    protected function getTableActions(): array
    {
        return [];
    }

    protected function getTableBulkActions(): array
    {
        return [];
    }

    // protected function getTableRecordsPerPageSelectOptions(): array
    // {
    //     return [10, 25, 50, 100];
    // }

    protected function isTablePaginationEnabled(): bool
    {
        return true;
    }

    public function render(): View
    {
        return view('livewire/list-post');
    }
}
danharrin commented 2 years ago

Thanks! This was a breaking change caused by #702.

danharrin commented 2 years ago

The bugs are fixed in the latest update.

As for the request about adding other parameters to the query string - this would be a breaking change for people with multiple tables on the same page, so IMO it should be opt-in. At the moment, since $tableRecordsPerPage, $tableSearchQuery etc are all Livewire properties, you can just use Livewire's query string features to bind these properties to the URL.