I have run into this same issue in some of my programs. My solution may not be terribly elegant, but it works like a charm.
Using a car example, say we have Ford and Toyota vehicles. I run a query that returns all vehicles complete with make and model. I then loop through it, creating a list of arrays with the info I want.
- <cfloop query="qryGetModels">
-
<cfset formSelectData = ListAppend(formSelectData, "new Array('#make#','#model#','#modelID#')")>
-
</cfloop>
To dynamically change the values in a list of models based on whether they select Ford or Toyota, I generate some JavaScript on page load that creates a javascript array of arrays with the information from my query and a function that is called by an onChange event associated with the makes select HTML element (<select name="make" onchange="showModels()">)
- vehicles = new Array(<cfoutput>#formSelectData#</cfoutput>);
-
-
function showModels() {
-
var makes = document.forms[0].make;
-
var models = document.forms[0].model;
-
models.options.length = 0;
-
for (i = 0; i < vehicles.length; i++) {
-
if (vehicles[i][0] == makes.options[makes.selectedIndex].value) {
-
models.options[models.options.length] = new Option(vehicles[i][1],vehicles[i][2]);
-
}
-
}
-
}
Now when a user clicks on "Ford" or "Toyota" in the makes select, the models select cleared and then populated with all Ford or Toyota models.
Here is an example of what my javascript "vehicles" variable/array looks like:
- vehicles = new Array(new Array('Ford','Taurus','1'), new Array(new Array('Ford','Explorer','2'), new Array(new Array('Toyota','Avalon','3'), new Array(new Array('Toyota','Yaris','4'));
Like I say, it might not be elegant, but it works!