How to populate select list options without pre-selecting them?

Hello, I am using hintAction withi the Select option to populate the options in my Select (multiple,searchable) input and it loads the lists but auto selects all the options as well. Please let me know how can I just load the options to the list without selecting any of them? Thank you
Solution:
But if I only use the $get('opporunity') it takes forever to load as it goes through all rows.
You should limit the results and use getSearchResultsUsing() https://filamentphp.com/docs/3.x/forms/fields/select#returning-custom-search-results...
Jump to solution
10 Replies
Dennis Koch
Dennis Koch5mo ago
It would help if you shared your code
mohdaftab
mohdaftabOP5mo ago
Please wait. Select::make("recipients") ->hintAction( Action::make('fetchRecipients') ->icon('heroicon-m-arrow-path') ->action(function (Get $get, Set $set, $state, $component) { $opportunity = Opportunity::find($get('opportunity')); if (null !== $opportunity) { $bc = BuyerContact::excludePipelineBuyerNetwork($opportunity)->take(10)->get(); $options = []; foreach ($bc as $b) { $options[$b->id] = $b->first_name . " " . $b->last_name; } $component->state($options); } }) ) ->searchable() ->multiple() ->preload(), I tried $set('recipients',$options); as well Thank you so much.
Dennis Koch
Dennis Koch5mo ago
You are setting the state not the available options: $component->state($options); You could store the options to the cache or a static property on the page and then use that inside ->options() What's the reason you are using an hintAction for this?
mohdaftab
mohdaftabOP5mo ago
it is based on the Select::make("opportunity") option if we change it and click on fetch recipients button then I need to clear the recipients and load the new options. it works well with ->live() but we are trying not to use live as there is a lot of data which we only need when requested using the button.
Dennis Koch
Dennis Koch5mo ago
Why don't you just return results based on $get('opportunity')? The select is searchable already
mohdaftab
mohdaftabOP5mo ago
because there are around 3 select inputs that will have different data based on the opportunity selected. we need to preload the results as the client wants to click on the drop down to see which options to multi select. there are around 50k+ results with a lot of subqueries but I just used a simple one in the example. @Dennis Koch I have convinced my client to use this with searchable option just like you advised. But if I only use the $get('opporunity') it takes forever to load as it goes through all rows. Is it possible to make this only search when we type and with limit instead of loading options Select::make("recipients") ->options(function (Get $get) { $opportunity = Opportunity::find($get('opportunity')); if (null !== $opportunity) { $bc = BuyerContact::excludePipelineBuyerNetwork($opportunity)->get(); $options = []; foreach ($bc as $b) { $options[$b->id] = $b->first_name . " " . $b->last_name; } return $options; } }) ->searchable() ->multiple() ->preload(), I also tried this using the code below, but it changes the label to id when I change the keyword which doesn't have the previously selected record. Select::make("recipients") // ->options(function (Get $get) { // }) ->getSearchResultsUsing(function (Get $get, $search) { $opportunity = Opportunity::find($get('opportunity')); if (null !== $opportunity) { $bc = BuyerContact::excludePipelineBuyerNetwork($opportunity) ->where('first_name', 'like', '%' . $search . '%') ->orWhere('last_name', 'like', '%' . $search . '%') ->get(); $options = []; foreach ($bc as $b) { $options[$b->id] = $b->first_name . " " . $b->last_name; } return $options; } }) ->searchable() ->multiple() ->preload(),
mohdaftab
mohdaftabOP5mo ago
like this.
No description
Solution
Dennis Koch
Dennis Koch5mo ago
But if I only use the $get('opporunity') it takes forever to load as it goes through all rows.
You should limit the results and use getSearchResultsUsing() https://filamentphp.com/docs/3.x/forms/fields/select#returning-custom-search-results
Dennis Koch
Dennis Koch5mo ago
I also tried this using the code below, but it changes the label to id when I change the keyword which doesn't have the previously selected record.
You should implement: getOptionLabelUsing Btw. please format your code to make it easier to read as mentioned in our #✅┊rules
mohdaftab
mohdaftabOP5mo ago
oh, I understand why we need to use getOptionLabelUsing. That is perfect, thank you. I will format the code properly from now on. it worked perfectly, thank you so much @Dennis Koch
Want results from more Discord servers?
Add your server