How can I dispatch a livewire event from a table?

The table function is static, thus I cant call $this->dispatch('something').
14 Replies
awcodes
awcodes12mo ago
The table method is a set up method. Where are you trying to call the dispatch? A dispatch needs an action. Just calling the table method isn’t going to dispatch anything.
DarkKnight
DarkKnightOP12mo ago
hello
->actions([
Tables\Actions\DeleteAction::make(),
Action::make('Test')
// ...
->action(function (Model $record, array $data) {
$this->dispatch('custom-event');
}),
->actions([
Tables\Actions\DeleteAction::make(),
Action::make('Test')
// ...
->action(function (Model $record, array $data) {
$this->dispatch('custom-event');
}),
Using $this when not in object context Which makes sense, but i dont know how else to do it
justlasse
justlasse12mo ago
You can access livewire in the closure and use it to dispatch with. Component $component and then $component->dispatch
DarkKnight
DarkKnightOP12mo ago
Got it
->action(function (Model $record, array $data, \Livewire\Component $livewire) {
$livewire->dispatch('custom-event');
})
->action(function (Model $record, array $data, \Livewire\Component $livewire) {
$livewire->dispatch('custom-event');
})
justlasse
justlasse12mo ago
I needed to access a method and found that component->getContainer()->getLivewire() actually is a thing 🙂 The dependency is filament livewire not livewire I believe I learned from a YouTube if ever in doubt you can dd(get_defined_vars())
DarkKnight
DarkKnightOP12mo ago
yeah. Filament has livewire as a dependency, since its built on top of it wow this is cool I always use dd() anyways, but I wasnt aware of this
justlasse
justlasse12mo ago
Here’s the docs mentioning the injected classes
DarkKnight
DarkKnightOP12mo ago
yess
justlasse
justlasse12mo ago
Seems it actually was the regular livewire class 😂 I didn’t even look I just used the dep injection in vscode to import the class
DarkKnight
DarkKnightOP12mo ago
Yeah, I literally just remembered it existed. Your thoughts are always clearer after a gooood 10h sleep 😂
justlasse
justlasse12mo ago
I’m curious did that work for you?
Igor
Igor12mo ago
Also you can use ->to() with dispatch to define which component will receive the event.
Want results from more Discord servers?
Add your server