How do I close Action modal using $wire when process is done
Hello,
I am using a custom Livewire component within ->modalContent for Table Action and it works great.
I need to know how to close the modal when the process is finished please?
I have attached the screenshot of the code I am using inside Livewire component view.
Thank you
36 Replies
You need to dispatch an event:
Hi @Dennis Koch I am not able to find the id to pass into this CustomEvent. Please let me know how do I get it?
Thank you so much.
I guess it's
[ACTION_NAME]-action
of your Action. But I guess it should appear in the HTML, too.Yes it appears in the html somewhere and I was able to find the close button and trigger click event. Is it not possible to retrieve the id of the modal so that I can close it by using dispatch('close-modal',id) please ?
[ACTION_NAME]-action
didn't work?I am sorry, let me try this.
I will check it 10 to 15 minutes, thank you so much.
@Dennis Koch sorry for late reply.
This is what I see on the close button $dispatch('close-modal', { id: '8dt9gfVpiFZrelMR6e9r-table-action' })
Is it changing between requests? Not sure how we generate that ID
Yes it changes between requests.
it is probably created using the wire-ui-elements
Ah, looking at the code it's the Livewire ID. I think you can retrieve that by using
$wire._id
let me try that.
not related, but why not modal form?
https://filamentphp.com/docs/3.x/actions/modals#modal-forms
@Dennis Koch $wire._id didn't work.
As far as I understand he is using a modal form and tries to close it from JS?
@Leandro Ferreira I am actually generating the Stripe card token before submitting the form and if I use modal form it sumbits right away. I need to run a few different tasks with it so Livewire component with the help of Alpine JS is working pretty good. The only issue is the modal not closing itself after the save
I also tried to set isOpen to false it closes the modal but doesn't refresh the parent.
Don't know the exact var. Unfortunetaly it's not documented. You could try
$wire.closeModal()
which takes an additional server requesti don't mind the additional server request though.
Or
@this.closeModal()
in your case maybe?Unable to call component method. Public method [closeModal] not found on component
Sorry, it's
closeActionModal()
. I already closed my editor 😅Unable to call component method. Public method [closeActionModal] not found on component
I think it looks for the function inside Livewire component which isn't available.
It's part of
InteractsWithActions
. But it'sprotected
🤦🏼♂️
As a workaround you could add your own method and call $this->dispatch('close-modal', id: "{$this->getId()}-action");
I understand, let me try to add own method.
i got a few ideas with your suggestions, I hope to figure it out.
do you see it inspection the browser?
$dispatch('close-modal', { id: '8dt9gfVpiFZrelMR6e9r-table-action' })
Yes I do
is
8dt9gfVpiFZrelMR6e9r-table-action
also the wire:key
, right?I think so, let me check
from the modal, I mean
it is wire:id
can you see
<div x-ref="modalContainer"...
?Yes I can see x-ref="modalContainer"
same wire:key?
yes
QlqEOA3nQOyBd5bKLIgp.table.actions.add.modal
hum.. let me check
Thank you
Solution
add this
and then, try to dispatch it
$dispatch('close-modal', {id: modalId})
Perfect
Thank you so much.
I got it working before with setting isOpen to false which closed the modal but it wasn't refreshing the parent. This does exactly like I needed.
Thank you so much.