F
Filament16mo ago
Ninja++

Show price when item selected.

Hey guys can anyone recommend an approach i should take. I want to show the cost field value for an item when i select that item in a select option next to it. So when you select a item you see its cost pop up next to it. Here is the current relevant step in the from wizard ```
16 Replies
Ninja++
Ninja++OP16mo ago
@josef tagging you as requested thank you for offering to help.
josef
josef16mo ago
So what exactly doesn't work? Does it update the cost according to the selected item? And which one is the select that should update the cost?
krekas
krekas16mo ago
$set and your fields aren't live()
Ninja++
Ninja++OP16mo ago
The final formula i am trying to achieve is volume + (selected materials) pricePermm3 * by quantity to get the final price. You mean i should add an addition
->live(),
->live(),
to this for example?
Select::make('pricePermm3')
->label('Price Per mm3')
->options(Materials::all()->pluck('name', 'id'))
->default(1)
->disabled()
->required()
->prefixIcon('heroicon-m-beaker')
->columnSpan(1)
->live(onBlur: true),
Select::make('pricePermm3')
->label('Price Per mm3')
->options(Materials::all()->pluck('name', 'id'))
->default(1)
->disabled()
->required()
->prefixIcon('heroicon-m-beaker')
->columnSpan(1)
->live(onBlur: true),
krekas
krekas16mo ago
onblur means when users removes focus from input check docs about reactivity
Ninja++
Ninja++OP16mo ago
Right now i can just pull the volume and perform math operators on it and have that value be shown in a disabled textinput. Yes that was my strategy to know when they had finished entering their values and i could then perform the operations using
->afterStateUpdated
->afterStateUpdated
josef
josef16mo ago
Depends. using onBlur is totally fine if you want the calculation to only happen after the user shifts focus away from the field I'm still not sure on what exactly does not work as you want it to You got select fields that are reactive and the field where you display the calculation result
Ninja++
Ninja++OP16mo ago
Right now what i think the first problem is i am unable to get the pricePermm3 value from the selected material when it is chosen from the dropdown. I would like to save that as a variable so i can use it in the overall price calculation
josef
josef16mo ago
If you calculation depends on multiple values, I'd move the calculation itself to the field that displays it, getting the requird values from the other fields using $get, then, when one of the fields changes, the cost field updates itself just have live() (with or without onBlur) on the required fields, and move all the calculation into the cost field probably using somethign like
formatStateUsing(static function (Get $get) {
$volume = $get('volume');
$pricePermm = $get(...);
$result = // calculation
return $result;
}
formatStateUsing(static function (Get $get) {
$volume = $get('volume');
$pricePermm = $get(...);
$result = // calculation
return $result;
}
Ninja++
Ninja++OP16mo ago
Thank you so much @josef I will experiment and post any additional challenges in this thread. I will have to also figure out how to query the pricePermm depending on what material is selected. Just this and my file downloading issue and i can tidy everything up for the submission. Then i can actually experment without this crazy time presure and actually have some fun with it. is the intention that i use like this?
TextInput::make('cost')
->label('Cost to print these items')
->default(1)
->disabled()
->numeric()
->required()
->prefixIcon('heroicon-m-currency-pound')
->columnSpan(1)
->formatStateUsing(static function (Get $get) {
$volume = $get('volume');
$pricePermm = $get(...);
$result = // calculation
return $result;
}),
TextInput::make('cost')
->label('Cost to print these items')
->default(1)
->disabled()
->numeric()
->required()
->prefixIcon('heroicon-m-currency-pound')
->columnSpan(1)
->formatStateUsing(static function (Get $get) {
$volume = $get('volume');
$pricePermm = $get(...);
$result = // calculation
return $result;
}),
Thijmen
Thijmen16mo ago
Just use visible() on the textinput
Ninja++
Ninja++OP16mo ago
I don't understand
Thijmen
Thijmen16mo ago
TextInput::make('new_costprice')
->label('Nieuwe kostprijs (€)')
->debounce()


->disabled(fn (callable $get) => !$get('change_cost_price')),
Checkbox::make('change_cost_price')
->label('Kostprijs wijzigen')
->reactive()
->disabled(fn (PriceChange $record) => $record->status != 1)
->inline(false),
TextInput::make('new_costprice')
->label('Nieuwe kostprijs (€)')
->debounce()


->disabled(fn (callable $get) => !$get('change_cost_price')),
Checkbox::make('change_cost_price')
->label('Kostprijs wijzigen')
->reactive()
->disabled(fn (PriceChange $record) => $record->status != 1)
->inline(false),
This is for v2 So replace reactive with live You can swap the disabled with visible if you like to hide it
Ninja++
Ninja++OP16mo ago
I am not trying to hide it im just trying to have it update as you change your selections for material, there is also a priority checkbox that is on another part of the form that i will need to add a static amount too this for also Or am i misunderstanding you?
josef
josef16mo ago
Na, you got it. Ignore that, if you don't want to hide the field yet. Just get it working first
Ninja++
Ninja++OP16mo ago
i would rather them see it so they can see the impact of their choice on the price.
Want results from more Discord servers?
Add your server