FilamentF
Filament2y ago
Hugo

Closure on column action doesn't work

Hi, there,

I'm trying to put an action on a column through a closure. I don't want the action to open if there is no $record->url, however, as soon as I pass a closure the action doesn't fire at all and the modal never opens.

I've noticed that as soon as I pass a closure in the action, the click event is no longer “mountTableAction” but “callTableColumnAction.

If anyone has any ideas 😄

Tables\Columns\IconColumn::make('url')
                    ->icon('heroicon-o-document')
                    ->placeholder('No media')
                    ->action(function ($record) {
                        if ($record->url) {
                            return Tables\Actions\Action::make()
                                ->modal()
                                ->modalContent(new HtmlString('<iframe src="' . $record->url . '" class="w-full h-full"></iframe>'))
                                ->modalHeading($record->url);
                        } else {
                            return null;
                        }
                    })
Solution
I think the problem here is that the action isn't getting registered. Try this:
->action(
    Tables\Actions\Action::make('show_page_in_modal')
        ->disabled(fn ($record): bool => ! $record->url)
        ->modalContent(fn ($record) => new HtmlString('<iframe src="' . $record->url . '" class="w-full h-full"></iframe>'))
        ->modalHeading(fn ($record) => $record->url)
        ->action(fn () => null)
),
Was this page helpful?