Select options get irrelevant results in the drop down list

Hello, When using Select::make() with ->options() and ->searchable(), the search works instantly but sometimes shows irrelevant results because filtering happens client-side. When switching to ->getSearchResultsUsing(), the results are accurate (server-side filtering) but the search becomes noticeably slower. In my case, the dataset is very small (about 50 records per tenant), so I’d expect client-side searching to be both fast and accurate. Is this expected behavior, or should ->searchable() filter results the same way as ->getSearchResultsUsing() even when using ->options()? Please let me know how to fix this, Thank you Here is the code I am using:
Select::make("driver_id")
->label("Drivers")
->options(function () {
return Filament::getTenant()->drivers()->orderBy('name')->pluck("name", "id")->toArray();
})
->searchable(),
Select::make("driver_id")
->label("Drivers")
->options(function () {
return Filament::getTenant()->drivers()->orderBy('name')->pluck("name", "id")->toArray();
})
->searchable(),
65 Replies
mohdaftab
mohdaftabOP2mo ago
bump
toeknee
toeknee2mo ago
It doesn't really matter clientside or not, what's different about the name? what if you set driver_id to be a relationship and adjust the relationship option? what about ->preload() too if you only hvae 50 items just load them all.
mohdaftab
mohdaftabOP2mo ago
@toeknee the issue is for example I have 10 names, this drop-down is being used in the filters so I can't use the relationship here. "John Smith" "Meghan Stacey" "Nicole" "David Smith" and so on when I search -> john it displays john smith on top but it shows the others which are totally irrelevant with the search term. If we look at how select2 works it only shows the matching option. Client is comparing it with select2 and wants me to fix it because they usually copy paste names to search in that list and just press enter to select, because they have really busy business hours and he want it to work really fast. I would really be grateful if someone could help me fix this issue, thank you so much.
mohdaftab
mohdaftabOP2mo ago
Here is the example with the issue
No description
mohdaftab
mohdaftabOP2mo ago
bump
toeknee
toeknee2mo ago
Ahh it's a filter, that is a strange search look at the query with debugbar or telescope
mohdaftab
mohdaftabOP2mo ago
@toeknee the results are preloaded and I am not using getSearchResults method so the searches are simply client side, there are no queries, do you think debugbar or telescope will read that ?
toeknee
toeknee2mo ago
preloaded will still show you the queries on that page you just need to open the request on that page
mohdaftab
mohdaftabOP2mo ago
the query is running fine and has the data in the drop down which are supposed to be there, but the issue is when I am searching it should only display the matching results as you can see in the screenshot above, it shows some unmatched results too.
toeknee
toeknee2mo ago
So if you are searching you need to see what the query is doing in the search for it to return non-matched results. That should be shown in debugbar or telescope.
mohdaftab
mohdaftabOP5w ago
Ok thank you so much, I will try again. @toeknee there were no queries except for the dropdown list to be preloaded once then there are no more queries for the drop down. bump
DSM
DSM5w ago
Select::make("driver_id") ->label("Drivers") ->options(Filament::getTenant()->drivers()->orderBy('name')-pluck("name", "id")) ->searchable(), Try this if it is working
mohdaftab
mohdaftabOP5w ago
Hello @DSM, thank you for the reply. It is same response with a few irrelevant options.
DSM
DSM5w ago
if you are using this in filters use SelectFilter instead of Select with relationship, relationship in filter will work with SelectFilter properly like SelectFilter::make('driver_id') ->relationship('drivers', 'name') ->searchable() ->preload() ->multiple()
mohdaftab
mohdaftabOP5w ago
Hi that sounds good, let me check. @DSM they are actually supposed to be form component because table filter can't be used inside Filter::form()
awcodes
awcodes5w ago
Where are you using the select? And is driver actually a relationship on the resource’s model?
mohdaftab
mohdaftabOP5w ago
Yes @awcodes it is a DriverPaymentResource, please check the screenshot. I am using filter form
No description
No description
mohdaftab
mohdaftabOP5w ago
Btw I checked the search with Checkboxlist and it works correctly matching Jo to Johny Wick and Josephine properly but when I search on Select drop down it shows johny wick along with other irrelevant options unless I type the name in full driver_payments belongs to drivers yes
awcodes
awcodes5w ago
Hmm, I wonder if there is a problem with trying to use a single Filter object as a form of multiple filters? Ie is it creating a query conflict at a lower level?
mohdaftab
mohdaftabOP5w ago
well I can see the same issue in the single form as well it is same with Filter or a normal form.
mohdaftab
mohdaftabOP5w ago
this is also a relationship in the booking form
No description
awcodes
awcodes5w ago
Can you try DSM’s solution with a Select instead of a SelectFilter?
mohdaftab
mohdaftabOP5w ago
ok let me try that now
awcodes
awcodes5w ago
Ie, if it’s a relationship you should define it as such instead of using ->options()
mohdaftab
mohdaftabOP5w ago
Well it shouldn't be using relationship though because this is a table filter which has multiple records. Filament\Support\Services\RelationshipJoiner::prepareQueryForNoConstraints(): Argument #1 ($relationship) must be of type Illuminate\Database\Eloquent\Relations\Relation, null given, called in
awcodes
awcodes5w ago
But you’re trying to filter on a relationship.
mohdaftab
mohdaftabOP5w ago
that Edit modal has relationship it can work on that modal, but not where I actually want to fix this. also the relationship will make queries to the database which kind of works anyway by using getSearchResultUsing but my client wants to only filter the driver names from the preloaded items so it is fast and works client side Checkboxlist searches without querying same should be the behavior of Select as well to match exactly what is being searched
awcodes
awcodes5w ago
Then don’t use the ‘driver_id’ as the make of the select. Make it something that doesn’t exist and define all the search and labels manually.
mohdaftab
mohdaftabOP5w ago
that could work let me try.
awcodes
awcodes5w ago
Just think you are trying to use convention and override it at the same time. But the convention should work. The only thing that would slow it down would be a slow connection or a unoptimized db.
mohdaftab
mohdaftabOP5w ago
driver_id is just being used by the filters though, there is no database connection
awcodes
awcodes5w ago
But driver is a relationship? Correct?
mohdaftab
mohdaftabOP5w ago
Yes it is but it doesn't work with relatoinship, let me show the error.
awcodes
awcodes5w ago
If it doesn’t work with the relationship, then my guess is that the relationship definition is off somewhere.
mohdaftab
mohdaftabOP5w ago
the driver is the relationship it brings the results which we desire when we use the relationship but it actually queries the database which actually takes some time to search and that is what my client doesn't want he wants to compare it with how the select2 library works. yes the relationship definition was wrongly used it was supposed to be driver instead of drivers but it brings the results now after querying the database.
awcodes
awcodes5w ago
Ok, but that’s not feasible. It should query the db. If you have 10k drivers loading all that into js memory is going to create more problems. So, at that point any slowness is at the network latency level or unoptimized db tables.
mohdaftab
mohdaftabOP5w ago
we are not going to have more than 10 or 15 drivers
awcodes
awcodes5w ago
If you hard code the ->options() as a test, does the searchable() work as expected?
mohdaftab
mohdaftabOP5w ago
this works fine with CheckboxList, but not with Select
No description
No description
awcodes
awcodes5w ago
Great, but we’re worried about selects at the moment. CheckboxList doesn’t use select.js. I’m trying to isolate the problem.
mohdaftab
mohdaftabOP5w ago
I also tried the search with quotes and it worked exactly how I want it to work without quotes
mohdaftab
mohdaftabOP5w ago
No description
awcodes
awcodes5w ago
Ok, just trying to reason if it’s an issue in select.js. If it is then filament can’t solve that.
mohdaftab
mohdaftabOP5w ago
Yes, I hope it can be fixed, my client keeps asking me why I haven't fixed it yet and it is difficult to explain to him about the issue. May I know the source for select.js ? is it select2 or some other library please?
awcodes
awcodes5w ago
My bad, it’s choices.js not select.js
mohdaftab
mohdaftabOP5w ago
Ok thank you
mohdaftab
mohdaftabOP5w ago
it is actually the issue with the source
No description
mohdaftab
mohdaftabOP5w ago
doing matches like fulltext
No description
mohdaftab
mohdaftabOP5w ago
GitHub
Search results (filter) are incorrect · Issue #1165 · Choices-js/...
Describe the bug I know that this library is not maintained, but hopefully someone knows what is going on here. Take a look at this: I have Choices setup pretty simply: const choices = new Choices(...
mohdaftab
mohdaftabOP5w ago
found the issue, hopefully this will solve if we can somehow have an option to change the threshold to 0 @awcodes I changed the threshold manually in select.js to 0 and it works like a charm, please let me know how can I prevent overwriting the select.js file with future updates unless there is an important update ?
awcodes
awcodes5w ago
Glad you found it. From the filament pov it would require a PR to allow setting the threshold and passing that to the choices.js alpine initialization. Should be doable.
mohdaftab
mohdaftabOP5w ago
I am sorry but I am not much familiar with github and PR stuff, does it require posting a repository or the project files? or is it just a request?
awcodes
awcodes5w ago
There should be a contributing guide on the github repo.
mohdaftab
mohdaftabOP5w ago
Great, thank you so much for your help, I will try it.
awcodes
awcodes5w ago
But you could also submit it as an issue with a reproduction repo. But that will take longer than submitting an actual fix.
mohdaftab
mohdaftabOP5w ago
The contributing part actually has to have a solution to be merged right ?
awcodes
awcodes5w ago
Yes. That’s what PRs are. They are solutions in the code to existing problems that if merged in will fix the issues.
mohdaftab
mohdaftabOP5w ago
I wish I knew how to dynamically add the threshold value to select.js to have the solution for PR I can simply edit the file and post it but that is not a solution
awcodes
awcodes5w ago
Basically, you fork the existing code on the branch you need, in this case 3.x. You make the changes that solve the issue, then make a PR from your fork. If you’re not comfortable doing a PR then I would recommend going down the issue reporting path. It’ll just probably take longer to get resolved.
mohdaftab
mohdaftabOP5w ago
I am just confused doing the PR with edited select.js file or to have an option with config for Select component to set the threshold ? I just want to clarify this then I will definitely post the PR
awcodes
awcodes5w ago
I’m not sure how to explain the steps in text. But you’d need a method on the select php component, then pass that call to the select alpine component and use it in the initialization of choices js. It’s really not as complicated as it sounds, I just can’t vocalize it. Source dive the code and see how other settings are being passed.
mohdaftab
mohdaftabOP5w ago
I will try it, thank you so much for your time, I really appreciate it. @awcodes I actually created a select-override.js file and extended the Select component to load select-override.js instead of select.js and it worked great.
awcodes
awcodes5w ago
Awesome. If it works then I’m happy for you. Good job.
mohdaftab
mohdaftabOP5w ago
Thank you so much for the guidance.
awcodes
awcodes5w ago
No worries.

Did you find this page helpful?