Linked select lists in Spring 3
My task is to populate another select list based on what is choosen from one select list, querying populating data from database.
I th开发者_JAVA技巧ink it goes something like:
- User clicks the parent selectlist which returns the id of the selected item.
- On select list
onChange
method a query likeselect id, description from table where child_id=_id_
is executed. - Child select list is populated based on that query. Maybe the result of query is returned in a controller method?
My problem is that I don't seem to find an example that would suit Spring 3 applications. There are multiple examples with php, and I'm newbie with JQuery.
I would like to recommend you to use Google, because simple search will provide you the answer.
This code sample will show you the way how to do it:
<script type="text/javascript" charset="utf-8">
$(function(){
$("select#category").change(function(){
$.getJSON("/getSubCategories?id="+$(this).val(), function(j){
var options = '';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
}
$("select#subCategory").html(options);
})
})
})
</script>
Now the Server side:
@RequestMapping(method = RequestMethod.GET, value = "/getSubCategories")
@ResponseBody
public String handleRequest( @RequestParam("id") int id) {) {
now build your json string as optionValue and OptionDisplay.
String json = service.getSubCategories(id);
return json;
}
精彩评论