F
Filament16mo ago
Abi

When is afterStateUpdated triggered for a repeater?

I am trying to perform some calculations when a new row is added or deleted. I see that the afterStateUpdated is triggered when the row is added and details are added to the row, but can't seem to get the afterStateUpdated to work when a row is deleted.. Can you please advice if this is a bug or if I am missing something? This is for a standalone Form builder. Here is the code I have
Repeater::make('add_new_kids')
->schema([
// Schema Fields go here
])->columnSpanFull()->columns(4)
->live()
->afterStateUpdated(function (Get $get) {
$this->quantity = count($get('existing_kids') ?? []) + count($get('add_new_kids') ?? []);
$this->subTotal = $this->quantity * $this->price;
}),
Repeater::make('add_new_kids')
->schema([
// Schema Fields go here
])->columnSpanFull()->columns(4)
->live()
->afterStateUpdated(function (Get $get) {
$this->quantity = count($get('existing_kids') ?? []) + count($get('add_new_kids') ?? []);
$this->subTotal = $this->quantity * $this->price;
}),
5 Replies
Patrick Boivin
Patrick Boivin16mo ago
How is $this->subTotal used in the rest of the component?
Abi
AbiOP16mo ago
it isn't used anywhere except for the blade file {{$subTotal}}
Patrick Boivin
Patrick Boivin16mo ago
I see... I'm not sure if this is the best option but maybe you could move this outside of the field, in a regular method on your component?
public function getSubtotal()
{
$quantity = count($this->data['existing_kids'] ?? []) + count($this->data['add_new_kids'] ?? []);

return $quantity * $this->price;
}
public function getSubtotal()
{
$quantity = count($this->data['existing_kids'] ?? []) + count($this->data['add_new_kids'] ?? []);

return $quantity * $this->price;
}
Then in your Blade view:
{{ $getSubtotal() }}
{{ $getSubtotal() }}
But to your original question, it could be a bug with afterStateUpdated()... it sounds like this should fire even when items are deleted.
Abi
AbiOP16mo ago
ok, let me try that
awcodes
awcodes16mo ago
Th is could still be due to outstanding bugs with livewire’s entangle. It still needs some work but not much Filament can do about that.
Want results from more Discord servers?
Add your server