F
Filamentβ€’16mo ago
paul_89142

Call Component function from Action button

is it possible to have an Action that when clicked calls a method on the component passing in the row's record (or just id of that row's record)? I cant find an example and currently when i click it reloads the whole page. context... I have a seperate audio-player component that I want to pass a new src route to based on the row record. I have a working Table component to which i want to add an action that changes a public variable (on that tabel component) something like:
...
$parent = $this;
...

Action::make('play')->icon('heroicon-o-play')->action(fn (Media $record) => $parent->setSrc($record)),
...
...
$parent = $this;
...

Action::make('play')->icon('heroicon-o-play')->action(fn (Media $record) => $parent->setSrc($record)),
...
fyi: earlier in the code i define $parent
public function table(TableComponent $table): TableComponent
{
$parent = $this;
...
public function table(TableComponent $table): TableComponent
{
$parent = $this;
...
the issue i have is that clicking the play button triggers a reload of the page, so even though i set the public livewire component variable $src it reloads and gets set back to '#' . thanks. Paul.
6 Replies
krekas
krekasβ€’16mo ago
Is this in the panel or livewire component?
paul_89142
paul_89142OPβ€’16mo ago
custom livewire component. Not in the filament panel / admin system πŸ™‚
krekas
krekasβ€’16mo ago
setSrc() is found on the same component?
paul_89142
paul_89142OPβ€’16mo ago
yeah.. using the $parent = $this; and then calling $parent->setSrc() does now seem to be working, just feels very hacky - lol
krekas
krekasβ€’16mo ago
i don't get it why are you assinging $this to $parent? just call $this->setSrc()
paul_89142
paul_89142OPβ€’16mo ago
There is a scope issue where you cant access $this from the anonymous function closure being passed into ->action(). Using arrow functions might work around that as it would normally allow access to parent variables but $this might be slightly different in this context (i havent tested it tbf let me have a quick go now) hmm.. ok cool can confirm by using arrow functions
->action(fn (Media $record) => $this->setSrc($record))
->action(fn (Media $record) => $this->setSrc($record))
does work previously i was doing more in that method so arrow function wasn't viable but have now simplified it so that does work πŸ˜„ awesome.
Want results from more Discord servers?
Add your server