F
Filament2mo ago
Guido

Conditionally hide SelectColumn based on email domain

I'm working on a Laravel project using FilamentPHP and need some help conditionally hiding a column in a table.
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('email')
->searchable(),
Tables\Columns\SelectColumn::make('department_id')
->label('Department')
->options(Department::all()->pluck('name', 'id')),
]);
}
public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('name')
->searchable(),
Tables\Columns\TextColumn::make('email')
->searchable(),
Tables\Columns\SelectColumn::make('department_id')
->label('Department')
->options(Department::all()->pluck('name', 'id')),
]);
}
The department_id column should only display when the user’s email address does end with @mycompany.com. If it doesn't, I'd like this column to be hidden for that specific row. Is there a way to conditionally hide a column based on a record's attribute like this in FilamentPHP?
6 Replies
LeandroFerreira
LeandroFerreira2mo ago
You can disable the select according to the condition
->disabled(fn (YourModel $record) => !str_ends_with($record->email, '@mycompany.com'))
->disabled(fn (YourModel $record) => !str_ends_with($record->email, '@mycompany.com'))
Guido
GuidoOP2mo ago
Hi @LeandroFerreira Thanks for the suggestion! However, I’m actually looking to completely hide the column rather than just disable it. Appreciate the help!
LeandroFerreira
LeandroFerreira2mo ago
visible won't work because you can't determine the column visibility by rows..
devpoolxx
devpoolxx2mo ago
GitHub
Visible Closure Table Column - Parameter Not Filled · filamentphp f...
Package Table builder Package Version v3.x.x How can we help you? I want to create a condition where columns can appear or hide according to the data row they pass. Usually we just enter a callback...
Guido
GuidoOP2mo ago
@LeandroFerreira I see, thanks! Any ideas on a workaround to just leave the column empty for certain rows?
LeandroFerreira
LeandroFerreira2mo ago
maybe ->extraAttributes(fn (YourModel $record) => ['class' => str_ends_with($record->email, '@mycompany.com') ? '' : 'hidden']) use lifecycle hooks to verify the values...
Want results from more Discord servers?
Add your server