Reputation: 1221
I am using the select2 jQuery dropdown box plugin (latest version), and have an issue where I'm allowing the user to enter an Employee ID to return the names of matching employee(s). This works.
In all searches, I return a value (the "status" of the ajax call, and the employee array if found). I suspect this may be confusing select2 into always thinking there are results.
When there are no actual employees returned, how can I explicitly tell select2 that no results were found so it displays that message?
Currently, I get a nasty jQuery error when no results are found:
jquery-3.6.0.min.js:2 jQuery.Deferred exception: Cannot read properties of undefined (reading 'length') TypeError: Cannot read properties of undefined (reading 'length')
at e (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js:2:39169)
at Object.<anonymous> (https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js:2:38243)
at e (https://code.jquery.com/jquery-3.6.0.min.js:2:30038)
at t (https://code.jquery.com/jquery-3.6.0.min.js:2:30340) undefined
Upvotes: 0
Views: 289
Reputation: 1221
Was going to delete as I found the answer shortly after, but figured this might help future coders out.
Simply return an explicit "results" value with an empty json object in the format select2 expects like below:
return { results: [] };
Upvotes: 1