Asked by:
What is the default search behavior of an asp.net MVC SelectListItem control on keypress?

Question
-
User-1955344339 posted
I have a dropdownlist control created using the asp.net MVC SelectListItem base control having the following list items (in the following order):
AMERICA AFGHANISTAN ALGERIA
With the focus on the control, when I press the key 'A', the control selects AFGHANISTAN. I am assuming that it is selected because it comes first in alphabetical order. Is this assumption true? If yes, is there any way to change this behavior and select the item on the basis of the display order and not alphabetical order i.e. AMERICA?
Using the following code inside my wrapper control class:
selectList = new SelectList(tableData, valueExpression, descExpression, selectedValue);
Expectation is that the control should select the first matching item on the basis of the display order and not alphabetical order.
Any documentation around the filtering functionality of SelectListItem would be helpful.
Wednesday, July 10, 2019 9:18 AM
All replies
-
User-1038772411 posted
Hi, deehar
You can refer below code may be you will get your solution with this.
$("#ID").keydown(function(e){ // the keydown even on ul var chr =String.fromCharCode( e.which ); $(this).find("li").each(function(){ // if the key down is not in UL then make sure the "this" should refer to UL var name = $(this).text(); if( name.toLowerCase().indexOf(chr.toLowerCase()) === 0 ) { $(this).focus(); // this should focus you return false; } });
Thanks.
Wednesday, July 10, 2019 10:36 AM -
User753101303 posted
Hi,
This is actually a browser behavior. I would expect most browser to select the next entry in display order whose first letter maches the one you pressed.
You have a lot of plugins turning your standard select into something with more capabilitie such as filering ui
Wednesday, July 10, 2019 11:00 AM -
User-1955344339 posted
Actually the next entry in alphabetical order is being selected by default. Need the control to select it based on display order and not alphabetical order.
Do you have examples of the plugins that you are talking about?
Thursday, July 11, 2019 11:55 AM -
User753101303 posted
For example see https://select2.org/searching You then have an explicit search box and the drop down is filtered as you type.
Thursday, July 11, 2019 12:58 PM