Update Select options after event
I have a Select field with options from a database. Now I want to update the list of options after a Livewire event was dispatched.
Is this possible?
20 Replies
@Leandro Ferreira yeah, but my form has a lot of Select fields with own options. The options can be edited inside a modal. So, I'm dispatching an event from the modal to the form, but I don't know how to 'refresh' the select component. I tried
$this->dispatch('$refresh');
from the modal, but no luckcould you share some code you are trying to do?
Ok, below is a simplified example:
$fieldsFromDb
is a Collection with fields from the database.
getOptions()
is a method in the FormField model that returns the options for that specific field.
This all works fine, but I don't know how to refresh the list of options.Something like this
@Leandro Ferreira thanks, but it doesn't work. I added a
dd()
to the listener callback but nothing happens
I'm also trying to find documentation about this feature, but it's not documentedCould you share how are you doing this?
And from the modal where the options are managed:
$this->dispatchFormEvent('updateOptions');
I also implemented HasForms
and added use InteractsWithForms;
to the modal.Where are you using this?
$this->dispatchFormEvent('updateOptions')
In the modal component, after saving the options and before closing the modal
The modal is a separate (non-Filament) Livewire component
take a look.. if you create a livewire component as this section
https://filamentphp.com/docs/3.x/forms/adding-a-form-to-a-livewire-component
And add this:
The event will be dispatched when you submit the form
@Leandro Ferreira yes this is basically what I'm doing, only my form is in another component. Maybe that's the reason it's not working?
hum.. maybe
@Leandro Ferreira one thing I forgot to mention is that the Select field is a multiple select
->multiple()
And when I remove that, the options are being refreshed using Livewire's $refresh
event. Any idea why it's not working with ->multiple()
?
The solution with dispatchFormEvent()
still won't work btw.because multiple uses
choices.js
I thinkIs that a bug or expected behavior?
expected behavior, it is why I recommended this example
https://discord.com/channels/883083792112300104/1226893491229950034/1226899263502548993
Ok, thanks!