Connecting Tech Pros Worldwide Forums | Help | Site Map

Filtered Drop Down

Newbie
 
Join Date: Jul 2007
Posts: 1
#1   Jul 7 '07
Have struggled a lot to get a filtered drop down in the normal html and that too editable ... i have used table like auto-completion etc ... This is the simplest solution for a filtered drop down

[HTML]
<SELECT NAME="drop" id='drop' onkeyup="editOption(arguments[0], this)" style='width:100px;cursor:text' mode="1">
<option value="0">Default</option>

</SELECT>
[/HTML]

Mode 0:
Filters the list till a valid match is found, if not found then will behave as a text box.
Mode 1: Editable text box and filters the list as the user types. Difference from mode 0 is that at every point user inputs will be shown rather than filtering.
Mode 2: User has to select from list available, Invalid entry would show the first list element as selected. User inputs is to filter to available match.
Mode -1 (Default): Invalid entry would show empty drop down.

And Have this script function in your page should do it.

Expand|Select|Wrap|Line Numbers
  1. function editOption(nse, obj)
  2. {
  3.     var key = nse?nse.keyCode:event.keyCode;
  4.     if(key>=33 && key<=40)
  5.     {
  6.         return true;
  7.     }
  8.     var text = obj.getAttribute("filter");
  9.     if(typeof(text)=="undefined" || text==null)
  10.     {
  11.         text = "";
  12.     }
  13.     if(key==8 && text.length>0)
  14.     {
  15.         text = text.substring(0, text.length-1);
  16.     }
  17.     else if(key>31)
  18.     {
  19.         text += String.fromCharCode(key);
  20.     }
  21.     else
  22.     {
  23.         return true;
  24.     }
  25.     text = text.toLowerCase();
  26.     var plist = obj.getAttribute("list");
  27.     var list = [];
  28.     if(typeof(plist)=="undefined" || plist==null)
  29.     {
  30.         plist = "";
  31.         for(var i=0, ilen=obj.options.length; i<ilen; i++)
  32.         {
  33.             list[i] = [obj.options[i].text, obj.options[i].value];
  34.             plist += obj.options[i].text+":"+obj.options[i].value+",";
  35.         }
  36.         obj.setAttribute("list", plist);
  37.     }
  38.     else
  39.     {
  40.         var tlist = plist.split(',');
  41.         for(var i=0, ilen=tlist.length; i<ilen; i++)
  42.         {
  43.             list[i] = tlist[i].split(':');
  44.         }
  45.     }
  46.     for(var i=obj.options.length-1; i>=0; i--)
  47.     {
  48.         obj.options[i] = null;
  49.     }
  50.     var mode = obj.getAttribute("mode");
  51.     if(typeof(mode)=="undefined" || mode==null)
  52.     {
  53.         mode = -1;
  54.     }
  55.     var optn;
  56.     if(mode==1)
  57.     {
  58.         optn = document.createElement("OPTION");
  59.         optn.text = text;
  60.         optn.value = text;
  61.         optn.selected = true;
  62.         obj.options.add(optn);
  63.         obj.selectedIndex = 0;
  64.     }
  65.     for(var i=0, ilen=list.length; i<ilen; i++)
  66.     {
  67.         if(list[i][0].toLowerCase().indexOf(text)==0)
  68.         {
  69.             optn = document.createElement("OPTION");
  70.             optn.text = list[i][0];
  71.             optn.value = list[i][1];
  72.             if(mode==1)
  73.             {
  74.                 optn.selected = false;
  75.             }
  76.             obj.options.add(optn);
  77.         }
  78.     }
  79.     if(obj.options.length==0)
  80.     {
  81.         if(mode==0)
  82.         {
  83.             optn = document.createElement("OPTION");
  84.             optn.text = text;
  85.             optn.value = text;
  86.             obj.options.add(optn);
  87.         }
  88.         else if(mode==2)
  89.         {
  90.             for(var i=0, ilen=list.length; i<ilen; i++)
  91.             {
  92.                 optn = document.createElement("OPTION");
  93.                 optn.text = list[i][0];
  94.                 optn.value = list[i][1];
  95.                 obj.options.add(optn);
  96.             }
  97.             obj.selectedIndex = 0;
  98.             text = "";
  99.         }
  100.     }
  101.     obj.setAttribute("filter", text);
  102.     return false;
  103. }
  104.  



acoder's Avatar
Site Moderator
 
Join Date: Nov 2006
Location: UK
Posts: 14,581
#2   Jul 9 '07

re: Filtered Drop Down


An explanation of the code would be useful.
Reply