ColdFusion Multi Selectbox
test.cfc
component {
/* getAll */
remote County[] function getAll(state_id)
{
var collection = [];
var obj = '';
var qry = '';
var i = 0;
/* get all records from database */
var q= new query();
q.setdatasource('SSWINTD');
q.setsql('
SELECT county_id,countyname
FROM SSWINTD.COUNTY
WHERE STATE_ID = :state_id
');
q.addParam(name="state_id", value=state_id,
cfsqltype="cf_sql_integer");
qry=q.execute().getresult();
/* load value objects */
for(i=1;i<="#qry.recordcount#";i++)
{
obj = createObject('component', 'County').init();
obj.setCounty_Id(qry.County_Id[i]);
obj.setCountyname(qry.Countyname[i]);
arrayAppend(collection, obj);
}
/* return success */
return collection;
}
}
multSelect.cfm
<html>
<head>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
$(document).ready(function() {
//first, detect when initial DD changes
$("#states").change(function() {
//get what they selected
var selected = $("option:selected",this).val();
//no matter what, clear the other DD
$("#county").children().remove().end().append("<option
value=\"\">Select a County</option>");
//now load in new options if I picked a state
if(selected == "") return;
$.getJSON("test.cfc?method=getAll&returnformat=json",{"state_id":selected},
function(res,code) {
var newoptions = "";
for(var i=0; i<res.length; i++) {
alert(res[i].countyname);
//In our result, ID is what we will use for the value, and
NAME for the label
newoptions += "<option value=\"" + res[i].county_id +
"\">" + res[i].countyname+ "</option>";
}
$("#cities").children().end().append(newoptions);
});
});
})
</script>
</head>
<body>
<form>
<label for="states">States</label>
<select name="state_id" id="states">
<option value="">Select a State</option>
<cfoutput query="stateList">
<option value="#state_id#">#state#</option>
</cfoutput>
</select>
<label for="county">County</label>
<select name="county" id="county">
<option value="">Select a County</option>
</select>
</form>
</body>
</html>
I am not getting this to fill in the county dropdown. I was hoping someone
would know what mistake I have made.
No comments:
Post a Comment