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
awcodes6mo 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
DarkKnight6mo 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
justlasse6mo ago
You can access livewire in the closure and use it to dispatch with. Component $component and then $component->dispatch
DarkKnight
DarkKnight6mo 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
justlasse6mo 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
DarkKnight6mo 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
justlasse6mo ago
Here’s the docs mentioning the injected classes
DarkKnight
DarkKnight6mo ago
yess
justlasse
justlasse6mo 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
DarkKnight6mo ago
Yeah, I literally just remembered it existed. Your thoughts are always clearer after a gooood 10h sleep 😂
justlasse
justlasse6mo ago
I’m curious did that work for you?
Igor
Igor6mo ago
Also you can use ->to() with dispatch to define which component will receive the event.