Rails Against The Machine

Just a mind dump. Why are you even reading this?

Thursday, 5 June 2008

 

Cascading drop downs

Java script code using prototype

window.onload = init;

function init(){
//
if (field = $('parent_dropdown_id')) {
field.observe('change', function(e) {
//make a request and get the json output
new Ajax.Request('url_to_get_data_from', {
method:'get',
parameters: field.serialize(true),
onSuccess: function(transport){
var json = transport.responseText.evalJSON();
some_namespace.display(json);
}
});
});
}
}

var some_namespace= {
display: function (data){
var field = $('child_dropdown_id');
opt= field.options;
opt.length=0; //You delete by resizing the array
for(var i=0;i<data.length;i++){
opt[opt.length] = new Option(data[i][0],data[i][1]);
}
}
}



On the server you just return a json opject to populate the drop down

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2007   August 2007   September 2007   December 2007   January 2008   February 2008   March 2008   April 2008   June 2008   July 2008   August 2008   October 2008   November 2008   January 2009  

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]