Set column action on condition

I have a table of products. Now if the product is of type 'bundled product' I want to bind an action that when clicked on the product name (the TextColumn) a modal opens displaying the contents of the product bundle. But if the product is not of type 'bundled' then you shouldn't be able to click the product name since there are no bundled products to display in the modal. So the action should not be attached in that case.
Tables\Columns\TextColumn::make( 'name' )
->sortable()
->searchable()
->action(
Tables\Actions\Action::make( 'showBundledProductsInModal' )
->action( fn() => null )
->modalHeading( 'Products in this bundle' )
->modalActions( [] )
->modalContent( fn( $record ) => view( 'filament.resources.products.table.bundled-products', [ 'record' => $record ] ) )
// I've tried visible, hidden...
//->visible( fn(): bool => ! empty( $this->record->product_type ) && (int) $this->record->product_type === ProductTypeEnum::BUNDLED->value )
),
Tables\Columns\TextColumn::make( 'name' )
->sortable()
->searchable()
->action(
Tables\Actions\Action::make( 'showBundledProductsInModal' )
->action( fn() => null )
->modalHeading( 'Products in this bundle' )
->modalActions( [] )
->modalContent( fn( $record ) => view( 'filament.resources.products.table.bundled-products', [ 'record' => $record ] ) )
// I've tried visible, hidden...
//->visible( fn(): bool => ! empty( $this->record->product_type ) && (int) $this->record->product_type === ProductTypeEnum::BUNDLED->value )
),
I've tried visible, hidden() on the action but they don't seem to work. Not when using a closure and not when passing in a hardcoded true of false value. The product name is always clickable and the action is executed.  Is it even possible to have the action conditionally set? How would I do that? Thanks for any help and pointers.
10 Replies
Dan Harrin
Dan Harrin17mo ago
->visible(fn($record): bool => $record->product_type === ProductTypeEnum::BUNDLED->value) ?
jelmerkeij
jelmerkeij17mo ago
I've tried that, but the action is still there. As in, the column value is still clickable. Only the modal won won't show up
Dan Harrin
Dan Harrin17mo ago
how about ->action(fn ($record): bool => ($record->product_type === ProductTypeEnum::BUNDLED->value) ? Action::make(...)... : null
jelmerkeij
jelmerkeij16mo ago
This doesn't work either. The action gets attached for either all rows in the column or for no rows. I don't think it's possible to conditionally set the action for a row?
Dan Harrin
Dan Harrin16mo ago
it is possible but you need to debug it ive set you on the right track there i cant debug for you
jelmerkeij
jelmerkeij16mo ago
I know and I certainly appreciate your help. I've tried your solution with
->action(fn ($record): bool => ($record->product_type === ProductTypeEnum::BUNDLED->value) ? Action::make(...)... : null
->action(fn ($record): bool => ($record->product_type === ProductTypeEnum::BUNDLED->value) ? Action::make(...)... : null
But If I set that on my TextColumn and randomly return true of false - a different value per row - as a test, all rows follow the value of the first row. So if for example the first row returns false all rows will not have the action and if it returns true all rows will indeed have the action
Dan Harrin
Dan Harrin16mo ago
try this, but also with disableClick() on the column itself doing the inverse condition
jelmerkeij
jelmerkeij16mo ago
Or do i need to apply ->action() not on my column but on something else? ok thanks I will try it in a few minutes Thanks this works, although on the rows that do not have an action they still visually appear as a link (orange and lighter orange on hover). I think this is because all these classes appear on filament-tables-text-column : filament-tables-text-column px-4 py-3 text-primary-600 transition hover:text-primary-500 hover:underline focus:text-primary-500 focus:underline Is there any way to remove those when the row does not have the action attached?
Travis
Travis13mo ago
@jelmerkeij Hi. I'm having a very similar issue, I think, but at the early stages. I want an action if certain conditions are met, but only want to display plain old text if those conditions aren't met. I will try to replicate what it sounds like you did, but I'll still eventually get to where I want to just display plain old text if the "actionable conditions" aren't met. Were you able to find a good solution...? 🤔 Any help you can provide would be great. 🤓
Stevee
Stevee9mo ago
Hai, Im also have this similar issue did you got the solution ?
Want results from more Discord servers?
Add your server