F
Filamentβ€’2mo ago
w5m

Pass form data (`$state`) to `ChartWidget` in resource Create page

I have embedded a ChartWidget in a resource Create page (of type CreateRecord) via the getFooterWidgets() method. Is there a way for the ChartWidget to access the data in the form fields (e.g. via a $state variable or similar) before the form is submitted (i.e. there is no record in the database table yet)? What I'm trying to do is populate a chart after clicking a "Calculate" button (i.e. Action) on the form and would appreciate any thoughts as to how to approach this. Many thanks.
Solution:
where are you using this action? I think you will need to inject $livewire.. ```php ->action(function($livewire) { ...
Jump to solution
6 Replies
LeandroFerreira
LeandroFerreiraβ€’2mo ago
use livewire events YourWidget
use Livewire\Attributes\On;

#[On('updateChartData')]
public function chartData(array $data): void
{
//$data..
}
use Livewire\Attributes\On;

#[On('updateChartData')]
public function chartData(array $data): void
{
//$data..
}
Your action
Action::make('calculate')
->dispatch('updateChartData', ['data' => $this->data])
Action::make('calculate')
->dispatch('updateChartData', ['data' => $this->data])
w5m
w5mβ€’2mo ago
Thanks for your response @Leandro Ferreira I get the following error "Using $this when not in object context", so would I need to pass a closure in as the 2nd argument to the dispatch() method?
Solution
LeandroFerreira
LeandroFerreiraβ€’2mo ago
where are you using this action? I think you will need to inject $livewire..
->action(function($livewire) {
$livewire->dispatch('updateChartData', data: $livewire->data);
})
->action(function($livewire) {
$livewire->dispatch('updateChartData', data: $livewire->data);
})
w5m
w5mβ€’2mo ago
The action is defined in the footerActions of a form Section component.
LeandroFerreira
LeandroFerreiraβ€’2mo ago
worked?
w5m
w5mβ€’2mo ago
That's worked nicely - many thanks Leandro πŸ‘
Want results from more Discord servers?
Add your server