mirror of
https://github.com/filamentphp/filament.git
synced 2026-09-24 15:42:09 +08:00
Merge pull request #10794 from filamentphp/feature/filter-layout
feature: Filter layout
This commit is contained in:
@@ -144,3 +144,37 @@ public function table(Table $table): Table
|
||||
->hiddenFilterIndicators();
|
||||
}
|
||||
```
|
||||
|
||||
### Customizing the filter form schema
|
||||
|
||||
You may customize the [form schema](../../forms/layout) of the entire filter form at once, in order to rearrange filters into your desired layout, and use any of the [layout components](../../forms/layout) available to forms. To do this, use the `filterFormSchema()` method, passing a closure function that receives the array of defined `$filters` that you can insert:
|
||||
|
||||
```php
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Tables\Filters\Filter;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
public function table(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->filters([
|
||||
Filter::make('is_featured'),
|
||||
Filter::make('published_at'),
|
||||
Filter::make('author'),
|
||||
])
|
||||
->filtersFormColumns(2)
|
||||
->filtersFormSchema(fn (array $filters): array => [
|
||||
Section::make('Visibility')
|
||||
->description('These filters affect the visibility of the records in the table.')
|
||||
->schema([
|
||||
$filters['is_featured'],
|
||||
$filters['published_at'],
|
||||
])
|
||||
->columns(2)
|
||||
->columnSpanFull(),
|
||||
$filters['author'],
|
||||
]);
|
||||
}
|
||||
```
|
||||
|
||||
In this example, we have put two of the filters inside a [section](../../forms/layout/section) component, and used the `columns()` method to specify that the section should have two columns. We have also used the `columnSpanFull()` method to specify that the section should span the full width of the filter form, which is also 2 columns wide. We have inserted each filter into the form schema by using the filter's name as the key in the `$filters` array.
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
namespace Filament\Tables\Concerns;
|
||||
|
||||
use Filament\Forms;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Tables\Filters\BaseFilter;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@@ -29,7 +28,7 @@ trait HasFilters
|
||||
}
|
||||
|
||||
return $this->makeForm()
|
||||
->schema($this->getTableFiltersFormSchema())
|
||||
->schema($this->getTable()->getFiltersFormSchema())
|
||||
->columns($this->getTable()->getFiltersFormColumns())
|
||||
->model($this->getTable()->getModel())
|
||||
->statePath($this->getTable()->hasDeferredFilters() ? 'tableDeferredFilters' : 'tableFilters')
|
||||
@@ -160,25 +159,6 @@ trait HasFilters
|
||||
return $name::getDefaultName();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Forms\Components\Group>
|
||||
*/
|
||||
public function getTableFiltersFormSchema(): array
|
||||
{
|
||||
$schema = [];
|
||||
|
||||
foreach ($this->getTable()->getFilters() as $filter) {
|
||||
$schema[$filter->getName()] = Forms\Components\Group::make()
|
||||
->schema($filter->getFormSchema())
|
||||
->statePath($filter->getName())
|
||||
->columnSpan($filter->getColumnSpan())
|
||||
->columnStart($filter->getColumnStart())
|
||||
->columns($filter->getColumns());
|
||||
}
|
||||
|
||||
return $schema;
|
||||
}
|
||||
|
||||
public function getTableFiltersSessionKey(): string
|
||||
{
|
||||
$table = class_basename($this::class);
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Filament\Tables\Table\Concerns;
|
||||
|
||||
use Closure;
|
||||
use Filament\Forms\Components\Group;
|
||||
use Filament\Forms\Form;
|
||||
use Filament\Support\Enums\ActionSize;
|
||||
use Filament\Support\Enums\MaxWidth;
|
||||
@@ -18,6 +19,8 @@ trait HasFilters
|
||||
*/
|
||||
protected array $filters = [];
|
||||
|
||||
protected ?Closure $filtersFormSchema = null;
|
||||
|
||||
/**
|
||||
* @var int | array<string, int | null> | Closure
|
||||
*/
|
||||
@@ -160,6 +163,32 @@ trait HasFilters
|
||||
return $this->getLivewire()->getTableFiltersForm();
|
||||
}
|
||||
|
||||
public function filtersFormSchema(?Closure $schema): static
|
||||
{
|
||||
$this->filtersFormSchema = $schema;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, Group>
|
||||
*/
|
||||
public function getFiltersFormSchema(): array
|
||||
{
|
||||
$filters = [];
|
||||
|
||||
foreach ($this->getFilters() as $filterName => $filter) {
|
||||
$filters[$filterName] = Group::make()
|
||||
->schema($filter->getFormSchema())
|
||||
->statePath($filterName)
|
||||
->columnSpan($filter->getColumnSpan())
|
||||
->columnStart($filter->getColumnStart())
|
||||
->columns($filter->getColumns());
|
||||
}
|
||||
|
||||
return $this->evaluate($this->filtersFormSchema, ['filters' => $filters]) ?? $filters;
|
||||
}
|
||||
|
||||
public function getFiltersTriggerAction(): Action
|
||||
{
|
||||
$action = Action::make('openFilters')
|
||||
|
||||
Reference in New Issue
Block a user