You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为公共页面的Table Builder启用表格过滤查询字符串

在Filament公共页面Table Builder中启用URL查询字符串过滤

要在公共页面的Filament Table Builder中实现过滤条件同步到URL查询字符串的功能,只需在Table配置中添加特定方法即可,具体操作如下:

  • 启用查询字符串存储:在Table定义里调用persistFiltersInQueryString()方法,Filament会自动把过滤、搜索等条件附加到URL的查询参数中。
  • 可选:禁用Session存储:如果不想让过滤条件同时存在于Session中,可以额外调用persistFiltersInSession(false),确保过滤状态仅通过URL传递。

代码示例

假设你在公共页面的Table定义如下,添加上述配置后:

use Filament\Tables\Table;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Filters\SelectFilter;

class PublicPostsPage extends \Filament\Pages\Page
{
    protected static string $view = 'filament.pages.public-posts';

    public function table(Table $table): Table
    {
        return $table
            ->query(\App\Models\Post::query())
            // 核心配置:开启URL查询字符串保存过滤条件
            ->persistFiltersInQueryString()
            // 可选:仅用URL存储,禁用Session
            ->persistFiltersInSession(false)
            ->columns([
                TextColumn::make('title')->label('文章标题'),
                TextColumn::make('category.name')->label('分类'),
                TextColumn::make('created_at')->label('发布时间')->date(),
            ])
            ->filters([
                SelectFilter::make('category')
                    ->relationship('category', 'name')
                    ->label('分类筛选'),
            ])
            ->searchInput()
            ->searchPlaceholder('搜索文章标题');
    }
}

效果说明

配置完成后,当你使用过滤器筛选分类、或在搜索框输入关键词时,URL会自动生成对应的查询参数,例如:
/public-posts?search=教程&filter[category]=2

将这个URL分享给他人后,对方打开页面时会自动应用相同的搜索和筛选条件,无需手动设置。

内容的提问来源于stack exchange,提问作者SNaRe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 20:42:37