Dynamically Populating Select Menus Client-Side - Code
(Page 2 of 4 )
Before jumping into code, I'll explain to you by which mechanisms we can accomplish said task. Basically we're going to maintain a JavaScript array of all possible items that could be populated into the second menu. For each item in the array, there will also be indicated the item in the first menu with which it is associated. So then when the user selects a new item in the first list, we can take the new selected value, iterate through the array to find only those items associated with the value, and load them into the second menu. That means that until something is selected in the first menu, there will be nothing in the second.
While this could be a somewhat simple idea and tutorial, I'm afraid I have to throw in a little something to de-simplify it all. Suppose you wish to implement this little gadget in an 'edit' screen. As in, you as an administrator want to edit the information about a particular laptop, and have this piece of JavaScript installed on the page. Now the laptop already belongs to a certain category and sub-category, so we'll have to have some sort of handler to pre-populate the menus based on the information that's already there. Lost? I really hope not, because we're about to start coding.
Now we can do this all with one array and one function. In this example I'll show you how to create the array dynamically on the server, but first we need to see what it should look like statically. All will feed off a form that will look something like this:
<form name="form1">
<select name="types" onChange="fillItems(0)">
<option value="32">Laptops</option>
<option value="21">PDAs</option>
</select>
<select name="items"></select>
</form>
The array we will need will be multi-dimensional. In the first dimension, each item will contain an array with three indexes. The first will be the ID of the main category that it belongs to; in the example it will be either laptops (ID: 32) or PDAs (ID: 27). The second index will contain the actual unique ID of the item. The third will just be the description. In the end it should look like this:
arItems = [
[ 32, 234, 'Toshiba SuperLaptop'],
[ 32, 156, 'Compaq WonderMachine'],
[ 32, 333, 'Sony ThinBook'],
[ 27, 656, 'IBM c6'],
[ 27, 467, 'Palm HandThing']
]
So that's the end product, now I'll show you how to generate it. I'm assuming that you'll retrieve a result set from a database with ASP.
<script language="JavaScript">
var arItems = new Array()
arItems = [
<%
strSQL = "SELECT type_id, id, description FROM items"
objRS.Open strSQL, strConn
arItems = objRS.GetRows()
objRS.Close()
Now we dump the contents of the server-side array into the identically-named client-side array.
for i = 0 to uBound( atItems, 2 )
response.Write( "[" & atItems( 0, i ) & "," & atItems( 1, i ) & _
",'" & atItems( 2, i ) & "']" )
if i < uBound( atItems, 2 ) then response.Write("," & vbCrLf )
next
%>
]
With this mix of ASP and JavaScript, we should have the array we need. Feel free to use PHP, or whichever technology you prefer. Now let's get into the function.
Next: Function >>
More JavaScript Articles
More By Justin Cook