FilamentF
Filament3y ago
Wbzy

How to listen to dispatch events in form?

so i have a button for populating the repeater, basically the button will open a modal and inside a modal there is a livewire filament table. in the bulk action i want to dispatch selected data back to the form.

This is my button
Forms\Components\Actions\Action::make('add')
    ->hiddenLabel()
    ->color('success')
    ->tooltip('Add Invoice')
    ->icon('heroicon-o-plus-circle')
    ->modalHeading('Choose Invoice')
    ->modalSubmitAction(false)
    ->modalContent(view('filament.modal.invoice-table'))
    ->action(function (Forms\Get $get, Forms\Set $set, Component $livewire) {
        // I want to get the selected record from modal and set the repeater here
    }),


This is my bulk action
->bulkActions([
    BulkAction::make('select')
        ->action(fn (Collection $records) => $this->dispatch('selectedData', selectedData: $records)),
]);
image.png
image.png
Solution
in the table bulk action i use 2 dispatch
one for passing the data, and the other for closing the modal.

$livewire->dispatch('attach-record', data: $record->select(['id'])->first());
$livewire->dispatch('close-modal', id: $this->componentId.'-form-component-action');


and listen it in the create file

#[On('attach-record')]
public function useRecord($data)
{
    $this->data['id'] = $data['id'];
}
Was this page helpful?