mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-28 08:04:45 +02:00
Add tag filter to lists (#1124)
This commit is contained in:
parent
c52439132d
commit
3d29243cf0
@ -7,6 +7,7 @@ use App\Filament\Components\Actions\ImportEggAction as ImportEggHeaderAction;
|
||||
use App\Filament\Components\Tables\Actions\ExportEggAction;
|
||||
use App\Filament\Components\Tables\Actions\ImportEggAction;
|
||||
use App\Filament\Components\Tables\Actions\UpdateEggAction;
|
||||
use App\Filament\Components\Tables\Filters\TagsFilter;
|
||||
use App\Models\Egg;
|
||||
use Filament\Actions\CreateAction as CreateHeaderAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
@ -77,6 +78,10 @@ class ListEggs extends ListRecords
|
||||
CreateAction::make(),
|
||||
ImportEggAction::make()
|
||||
->multiple(),
|
||||
])
|
||||
->filters([
|
||||
TagsFilter::make()
|
||||
->model(Egg::class),
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ namespace App\Filament\Admin\Resources\NodeResource\Pages;
|
||||
|
||||
use App\Filament\Admin\Resources\NodeResource;
|
||||
use App\Filament\Components\Tables\Columns\NodeHealthColumn;
|
||||
use App\Filament\Components\Tables\Filters\TagsFilter;
|
||||
use App\Models\Node;
|
||||
use Filament\Actions;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
@ -65,6 +66,10 @@ class ListNodes extends ListRecords
|
||||
->emptyStateHeading(trans('admin/node.no_nodes'))
|
||||
->emptyStateActions([
|
||||
CreateAction::make(),
|
||||
])
|
||||
->filters([
|
||||
TagsFilter::make()
|
||||
->model(Node::class),
|
||||
]);
|
||||
}
|
||||
|
||||
|
57
app/Filament/Components/Tables/Filters/TagsFilter.php
Normal file
57
app/Filament/Components/Tables/Filters/TagsFilter.php
Normal file
@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Components\Tables\Filters;
|
||||
|
||||
use Filament\Forms\Components\Field;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Tables\Filters\BaseFilter;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class TagsFilter extends BaseFilter
|
||||
{
|
||||
protected string $model;
|
||||
|
||||
public static function getDefaultName(): ?string
|
||||
{
|
||||
return 'tags';
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->query(fn (Builder $query, array $data) => $query->when($data['tag'], fn (Builder $query, $tag) => $query->whereJsonContains('tags', $tag)));
|
||||
|
||||
$this->indicateUsing(fn (array $data) => $data['tag'] ? 'Tag: ' . $data['tag'] : null);
|
||||
|
||||
$this->resetState(['tag' => null]);
|
||||
|
||||
$this->visible(fn () => $this->getTags()->count() > 0);
|
||||
}
|
||||
|
||||
private function getTags(): Collection
|
||||
{
|
||||
return $this->getModel()::query()->pluck('tags')->flatten()->unique();
|
||||
}
|
||||
|
||||
public function getFormField(): Field
|
||||
{
|
||||
return Select::make('tag')
|
||||
->preload()
|
||||
->searchable()
|
||||
->options(fn () => $this->getTags()->mapWithKeys(fn ($tag) => [$tag => $tag]));
|
||||
}
|
||||
|
||||
public function model(string $model): static
|
||||
{
|
||||
$this->model = $model;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getModel(): string
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user