F
Filament15mo ago
Obala

how to set a value of an input field using values in a repeater

I have this input fields inside a repeater, product name, price and quantity, and I have an input field total disabled by default outside of the repeater, I want to populate the result of foreach(price * quantity) inside the total field
7 Replies
Obala
ObalaOP15mo ago
my codes look like this
Repeater::make('items')
->relationship()
->schema([
Select::make('shop_product_id')
->options(Product::query()->pluck('name', 'id'))
->label('Stock')
->native(false)
->afterStateUpdated(fn($state, Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->placeholder('Select a drink'),
TextInput::make('quantity')
->required()
->numeric(),
TextInput::make('unit_price')
->label('Unit Price')
->disabled()dfd
->dehydrated()
->numeric()
->required()
->columnSpan([
'md' => 3,
]),
])->orderable()
->defaultItems(1)
->required(),

TextInput::make('total')
->disabled()
->required()
->numeric(),
]);
Repeater::make('items')
->relationship()
->schema([
Select::make('shop_product_id')
->options(Product::query()->pluck('name', 'id'))
->label('Stock')
->native(false)
->afterStateUpdated(fn($state, Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->placeholder('Select a drink'),
TextInput::make('quantity')
->required()
->numeric(),
TextInput::make('unit_price')
->label('Unit Price')
->disabled()dfd
->dehydrated()
->numeric()
->required()
->columnSpan([
'md' => 3,
]),
])->orderable()
->defaultItems(1)
->required(),

TextInput::make('total')
->disabled()
->required()
->numeric(),
]);
toeknee
toeknee15mo ago
function the login in the afterStateUpdated do: $set('../total', $value); But... using disabled may not allow for the value to be updated in the DB, so ensure you set a mutate before save which sets thte total value if it's a total charigng price for example.
Obala
ObalaOP15mo ago
the problem is i want it to be shown in to the user while adding up the orders, though i don't want them to be able to change the total, i saw somewhere the use of placeholder but not sure if that could work
Obala
ObalaOP15mo ago
GitHub
Set value of a field that is outside the repeater field, from a fie...
I'm trying to update the value of a field that is outside a repeater from a field that is inside the repeater, but the calculated value is doubling. For example, if I type 2 in the quantity fie...
Obala
ObalaOP15mo ago
where i tried implementing something like this
return $form
->schema([
Repeater::make('items')
->relationship()
->schema([
Select::make('shop_product_id')
->options(Product::query()->pluck('name', 'id'))
->label('Stock')
->native(false)
->afterStateUpdated(fn($state, Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->placeholder('Select a drink'),
TextInput::make('quantity')
->required()
->afterStateUpdated(fn ($state, Set $set, Get $get) => $set('../../total', Product::find($get('shop_product_id')?->price * $state)?? 0))
->numeric(),
TextInput::make('unit_price')
->label('Unit Price')
->disabled()
->dehydrated()
->numeric()
->required()
->columnSpan([
'md' => 3,
]),
])->orderable()
->defaultItems(1)
->required(),

TextInput::make('total')
->disabled()
->required()
->numeric(),
]);
return $form
->schema([
Repeater::make('items')
->relationship()
->schema([
Select::make('shop_product_id')
->options(Product::query()->pluck('name', 'id'))
->label('Stock')
->native(false)
->afterStateUpdated(fn($state, Set $set) => $set('unit_price', Product::find($state)?->price ?? 0))
->placeholder('Select a drink'),
TextInput::make('quantity')
->required()
->afterStateUpdated(fn ($state, Set $set, Get $get) => $set('../../total', Product::find($get('shop_product_id')?->price * $state)?? 0))
->numeric(),
TextInput::make('unit_price')
->label('Unit Price')
->disabled()
->dehydrated()
->numeric()
->required()
->columnSpan([
'md' => 3,
]),
])->orderable()
->defaultItems(1)
->required(),

TextInput::make('total')
->disabled()
->required()
->numeric(),
]);
it works with this but the problem when more that a single product is order for
TextInput::make('quantity')
->required()
->afterStateUpdated(fn ($state, Get $get, Set $set) => empty($get('../../total')) ? $set('../../total', $get('unit_price') * $state) : ($get('unit_price') * $state) + $get('../../total'))
->numeric(),
TextInput::make('quantity')
->required()
->afterStateUpdated(fn ($state, Get $get, Set $set) => empty($get('../../total')) ? $set('../../total', $get('unit_price') * $state) : ($get('unit_price') * $state) + $get('../../total'))
->numeric(),
toeknee
toeknee15mo ago
You need to then get the items and build a loop and count the unit_price X quantity
TextInput::make('quantity')
->required()
->afterStateUpdated(function ($state, Get $get, Set $set) {
$allValues = $get('../../items');
$total = 0;
foreach($allValues as $row) {
$total += $row['unit_price'] * $row['quanity'];
}
$set('../../total', $total);
}
->numeric(),
TextInput::make('quantity')
->required()
->afterStateUpdated(function ($state, Get $get, Set $set) {
$allValues = $get('../../items');
$total = 0;
foreach($allValues as $row) {
$total += $row['unit_price'] * $row['quanity'];
}
$set('../../total', $total);
}
->numeric(),
as a rough example
Obala
ObalaOP15mo ago
Thank you, let me test it out
Want results from more Discord servers?
Add your server