F
Filament3mo ago
Mikail

native(false) won't work on depended dropdown, any idea?

->filters([ SelectFilter::make('school_class_and_section') ->form([ Select::make('school_class_id') ->label('Class') ->relationship('school_classes', 'name') ->native(false) ->live(), Select::make('section_id') ->label('Section') // ->native(false) NOTE: Not loading the dropdown unless native is true because sections dropdown depends on classes
->placeholder(fn (Get $get): string => empty($get('school_class_id')) ? 'First select a class' : 'Select an option') ->relationship( 'sections', 'name', fn (Builder $query, Get $get) => $query ->where('sections.school_class_id', $get('school_class_id')) ), ]) Select::make('section_id') won't load if native() is set to false. I think because of arrow fn. The Remove item feature in native(false) is very important in this regard. Any idea how to go around this?
15 Replies
Mikail
Mikail3mo ago
any idea still?
Majid Al Zariey
Majid Al Zariey3mo ago
Unfortunately, PlaceHolder will only be calculated and initialized once in every (not native) select. As the component is wire:ignored. It would need a bit of restructuring for the following code to support your case. I think you can use the hint label as an alternative.
Majid Al Zariey
Majid Al Zariey3mo ago
No description
No description
Mikail
Mikail3mo ago
Thanks for this input. However, using hint does same as placeholder in the sense that it doesn't remove the 'section' item if you want to once you've already select one. If you decide to unselect the 'section' it just change the query to null and filters no result. I want to be able to remove a 'section' item even after one item is selected, just like you can remove (X) a 'class' item. I hope you get my point
Majid Al Zariey
Majid Al Zariey3mo ago
I believe the query of the section are dependent on the class, right Are you saying you want to clear the section once the class changes? and I meant you could still use the (not native select), with the hint or helper, you are only notifying the user to select a class first for the options of the section to show. I'm I right?
Mikail
Mikail3mo ago
I simply want to be able to remove section item just like you can remove a class item by pressing 'X' on class select option. Kindly check the attachment you sent
Majid Al Zariey
Majid Al Zariey3mo ago
That's why I said to use (hint/helper) instead of placeholder with (native(false)) . if it matches your need
Mikail
Mikail3mo ago
native(false) won't work as long as I use Get $get closure.
Majid Al Zariey
Majid Al Zariey3mo ago
Use your code like this
SelectFilter::make('school_class_and_section')
->form([
Select::make('school_class_id')
->label('Class')
->relationship('school_classes', 'name')
->native(false)
->live(),

Select::make('section_id')
->label('Section')
->native(false)
->hint(fn (Get $get): string => empty($get('school_class_id')) ? 'First select a class' : 'Select an option')
->relationship(
'sections',
'name',
fn (Builder $query, Get $get) => $query
->where('sections.school_class_id', $get('school_class_id'))
),
SelectFilter::make('school_class_and_section')
->form([
Select::make('school_class_id')
->label('Class')
->relationship('school_classes', 'name')
->native(false)
->live(),

Select::make('section_id')
->label('Section')
->native(false)
->hint(fn (Get $get): string => empty($get('school_class_id')) ? 'First select a class' : 'Select an option')
->relationship(
'sections',
'name',
fn (Builder $query, Get $get) => $query
->where('sections.school_class_id', $get('school_class_id'))
),
Mikail
Mikail3mo ago
for some reason, native(false) refuses to load option once there's a $get closure.
Mikail
Mikail3mo ago
CAn you try it from your end?
Majid Al Zariey
Majid Al Zariey3mo ago
Let me try and create a schema for this
Mikail
Mikail3mo ago
Alright
Majid Al Zariey
Majid Al Zariey3mo ago
I believe there is something wrong with the relationship use the following
->options(fn($get) => Section::where('school_class_id', (int)$get('school_class_id'))->pluck('name', 'id')),
->options(fn($get) => Section::where('school_class_id', (int)$get('school_class_id'))->pluck('name', 'id')),
instead of relationship
Mikail
Mikail3mo ago
Oh nice one. it indeed loaded the option with native(off). Only issue is that placeholder condition didn't work but using hint worked. This is still better and useable. I believe it has to do with $get utility. the relationship in the first Select do work fine without issues.