473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,387 developers and data experts.

Filtered Drop Down

1
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.  
Jul 7 '07 #1
1 5920
acoder
16,027 Expert Mod 8TB
An explanation of the code would be useful.
Jul 9 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Seth | last post by:
How can I hide an entire row of a table if one of the cells in that row is equal to a value drop down box value. My goal is to have a filtered table so that each cell in each row determines if...
0
by: Mike O. | last post by:
MS Access 2003 "filter by form" has drop down lists that allow the user to select values for each field to filter by. However, once some values are selected,the remaining dropdown lists remain the...
3
by: dbaxter7 | last post by:
OK, here's my dilemma. I am trying to create a system to track cases and clients. There are instances when cases have multiple clients, and there are instances where clients have multiple cases. ...
3
by: Don Wash | last post by:
Hi There! I have a Server-side Drop-down box in ASP.NET (VB) page. What do I do to widen the Drop down box's Pull-Down list's width? I'm not talking about the Drop-down box's width but the box...
2
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will...
4
by: TycoonUK | last post by:
Hi, As I do not have IE7 on my computer, I was wondering if there is a fault in my CSS Menu when using IE7. Please can someone look at my site - http://www.worldofmonopoly.co.uk and tell me...
21
by: JHNielson | last post by:
I have a table that I would like to be able for the users to search/filter. I have multiple fields to search: I built a form, and I would like them to be able to choose form a set of...
14
kcdoell
by: kcdoell | last post by:
Hello: I have a form (Default view =single form) with a subform (Default view =continuous forms) embedded into it. In the form I have three controls that display the Division, Working Region &...
3
by: mfdesigns | last post by:
http://www.etcontrol.com/autosuggest/autosuggest.php I am trying to add a drop down to filter the results. Is there a way to do that? Or just point me in the right direction. Thanks!!! <?php...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.