The sum of dehydrated disabled and field in the repeater was late in realtime, and I got wrong total
Repeater::make('test')
->columns(3)
->defaultItems(3)
->reactive()
->afterStateUpdated(function ($set, $get) {
$sumOfPrice1 = collect($get('test'))->pluck('price1')->sum();
$sumOfPrice2 = collect($get('test'))->pluck('price2')->sum();
$set('sum_of_price1', $sumOfPrice1);
$set('sum_of_price2', $sumOfPrice2);
})
->schema([
TextInput::make('price1')
->integer()
->reactive()
->afterStateUpdated(fn ($set, $state) => $set('price2', $state))
->required(),
TextInput::make('price2')
->disabled()
->dehydrated(),
]),
TextInput::make('sum_of_price1')
->disabled()
->dehydrated(),
TextInput::make('sum_of_price2')
->disabled()
->dehydrated(),
In above repeater, The sum of price2 is not correct in realtime. Please, How to solve?
I want the value of Sum of Price1 field by suming the value from price1 in the repeater,
And also, I want the value of Sum of Price2 field by suming the value from price2 in the repeater,
Note: the price2 field value was not type, state clone from price1 field
2 Replies
In the picture, The sum of price1 Field in the repeater shown in the TexInput(Sum Of Prcie1) was 100 is right, the price2 field is get a value state from price1
The worng things is the sum of price2 field show it in the TextInput(sum of price2), I think I was late output when I type, If I type second price1 field, the sum of price2 field was set 100, but total sum of price2 field should be first item of price1 field + second item of price2 field. it still late to get total value
Please, How to solve???:fi:
Instead of
$set()
ing a sum, it would be a better approach to use ->formatStateUsing()
on the inputs that show the sum and use a callback to calculate it from the repeater data.
I could imagine that $set('price2')
runs after the repeater callback is fired.