How to trigger a custom action modal directly from a TextInputColumn updateStateUsing?
I have a TextInputColumn on a table for my resources
route_id
attribute. When this is changed to a route_id
that does not exist, I want to prompt the user with an action modal, where they can create a new route
record, which would update the route_id
column with the id of the newly created route.
I am trying to do this:
But the action modal does not get triggered at all, what am I doing wrong?Solution:Jump to solution
We cannot register an Action here, I guess. I created a header action as a workaround. Is it elegant? No, but it might work for your case.
ListPage
```php
protected function getHeaderActions(): array...
36 Replies
Im also struggling to get this working for a checkboxColumn. Were you able to figure it out @nowak ?
Not yet, no :/
I even added a log::info to the function and it looks like it doesn’t get hit at all.
Hum.. maybe registering a custom action in a widget and dispatching with arguments 🤔
You can’t ‘make’ the action on demand. It’s too late in the lifecycle. It has to be registered during component creation. Then you can dispatch a mountAction(), mountTableAction() or mountFormComponentAction() via livewire with the action name and arguments.
Will probably need a custom column that extends TextColumn to register the action during setUp()
From what I’ve been reading, it seems like ->updateStateUsing() should be the way to modify how the state is stored right?
Yes, but actions and state are 2 very different things.
I see that. In the code sample above you can see @nowak is using updateStateUsing(). I’ve also been calling it action in all of my googling today till about an hour ago when I figured out the difference.
Also, only form input columns have the concept of updating state
I’m currently trying to also get ->updateStateUsing() to work on a form input column (i’ve tried checkbox, and toggle)
Normal columns are essentially readonly. Vs something like a textInputColumn which does interact with state.
This is my particular column. ->getStateUsing() works great, but can not get ->updateStateUsing() to work to save my life. CheckboxColumn should be one of the form input columns right?
Also, where are you getting updateStateUsing() ?
These are the two lifecycle hooks for checkbox columns. https://filamentphp.com/docs/3.x/tables/columns/checkbox#lifecycle-hooks
Not sure what you mean here. Are you asking where i am finding I should use it?
I’m asking why you are using that method when it is not part of the lifecycle for that component.
It should be afterStateUpdated()
Just feels like you are trying to augment the state out of order for the lifecycle
Because im struggling to figure out and find documentation that I can understand that explains how i would translate a checkbox being checked, to a column being updated with the value of “completed”.
From digging around im seeing examples of form input columns using updateStateUsing() to get this done. I started digging into the code to figure it out and CheckboxColumn.php uses Concerns\CanUpdateState so i thought I was on the right track.
Keep in mind that form input columns are not the same thing as a form input.
I understand that. 🙂 This github issue in particular i was referencing. https://github.com/filamentphp/filament/issues/6662#issuecomment-1574574466
GitHub
updateStateUsing() makes table select go back to first value · Issu...
Package filament/filament Package Version v2.17.44 Laravel Version v10.13.0 Livewire Version No response PHP Version PHP 8.1.10 Problem description After changing the value for the select "Shi...
Column inputs are limited in what they can do compared to actual form input fields.
Ah, that’s v2. That’s where it’s coming from. Are you on filament v2?
No, im on v3.
Ok, so that method isn’t the same on v3.
I thought that could be a risk of old info, but again, with the CheckboxColumn class using CanUpdateState i assumed that was correct.
So, should my code look more like this:
Should probably be after state updated, but you still need to return the state.
So in the callback, do what you need to with the state, but return what the modified state should be for the column.
Ok, that worked.
I was confused by the documentation saying “ // Runs after the state is saved to the database.” as if it was already stored in the database.
Well it is after, but the stored state can be different than the displayed state. If that makes sense.
I do get that, but I think im still struggling with the wording.
I have it working now so im happy. Thanks for your help and patience as i wrap my brain around it. 😄
Solution
We cannot register an Action here, I guess. I created a header action as a workaround. Is it elegant? No, but it might work for your case.
ListPage
Wow! thank you! It seems like this is the only way, and this solves the issue.
ugly, but it should work ✌️
We can't have it all 🙂
agree
I did this in a form. But how do I return the form state if the user cancels the action?
For example, I have a toggle set to false, the user clicks it, changes it to true and displays the confirmation modal. If he cancels, my toggle persists as true.
In this case you probably need to revert the state to again on cancel
Could you give me an example? I tried a few ways, like changing the modalCancelAction but without success.
That's what I would have tried. Probably you can register your own action though.