Set State of Select Field After Dynamically Populating Via Dependent Field
Hey all,
I've been trying to solve this for two days and I'm sure it's something simple that I'm missing but I keep hitting a wall.
Using the code below, I'm populating the "service_call_id" field using an eloquent query based on the "customer_id" field.
However, once the "service_call_id" options have been populated, I'd like to automatically set the state of the "service_call_id" field to the very first result returned from the query. I've tried using default, selectablePlaceholder(false), and manually manipulating the state but I can't seem to find a way to hook into an event that happens after the options are populated. Any help would be GREATLY appreciated:
Using the code below, I'm populating the "service_call_id" field using an eloquent query based on the "customer_id" field.
However, once the "service_call_id" options have been populated, I'd like to automatically set the state of the "service_call_id" field to the very first result returned from the query. I've tried using default, selectablePlaceholder(false), and manually manipulating the state but I can't seem to find a way to hook into an event that happens after the options are populated. Any help would be GREATLY appreciated:
16 Replies
You can do similar to the following:
Hey, thanks, I just tried that but unfortunately it doesn't work.
So it seems that if I do the following, the service_call_id state gets updated but the select field doesn't reflect the state change on the front end. It still says "Select an option". Is there a way to trigger the form field to update to reflect the state of that field?
Because you use
->key('service_call_select')
, I guess you need to use that key instead of $set('service_call_id', array_key_first($options));
@bahamagician I've been experimenting with a similar example and made it work from another angle: instead of afterStateUpdated() on the parent, you JUST set the values in the options() of the child. Also added selectablePlaceholder() with callback function condition.
Look how it worked for me, transform it to your case:
So this was similar to what I was trying to say with afterStateUpdated seems a better place to handle the setting to me.
I wonder if your approach makes it faster however
I found this interesting on your Filament Daily and I tried to implement it. However, in edit mode, the value showed on the parent select is its id, not the name.
Good point, you'll need to condition if $record->city_id then pluck where id = city_id
The version on Filament Daily is not perfect, I found bugs there and that's why I returned to this topic to shoot the updated version. So above is the code I came up with, you're saying it doesn't work? Worked well for me just an hour ago, showed parent normally.
Yeah, it doesn't. Exactly the same code written above
Of course I think so
Mmmmkay will double-check when back to computer, thanks
Pleasure is mine
Could you try this please?
This did it, thanks! Is it okay to leave the random function in there for the wire:key? Is that going to cause any interference with other components?
This trick has been working for me when I need to make updates, like this https://github.com/livewire/livewire/discussions/1895#discussioncomment-139040
However, I haven’t tried it in this specific context. If anyone has a better approach, I’d be interested to hear it as well ✌️
GitHub
livewire nesting component only works once · livewire livewire · Di...
Im using livewire component add product to shopping cart and change their quantities, there are three components. product-quantity used as child components in table of cart component, when add prod...
Using the trick you mentioned I found also the only way to make a date picker field reactively update. This was a long time ago though so I’m not sure if the date picker can currently be set reactively,