Reputation: 477
I'm working on dojo and Struts2, facing one issue. Dynamically making ajax call and getting response in JSON format and contain many key value pairs, want to set this response to Strut2 select
Could you please help on this issue, how can i fix this issue?
Struts select Tag:
<s:select id="empindex" list="#{EmployList}" name="Employ" listKey="key" listValue = "label" emptyOption="true" value="%{destination.key.toString()}" />
Ajax:
employs.ajax.fireJavaScriptRequest("getEmploys.action", {}, function(details) {
dojo.byId("empindex").innerHTML=details;
});
JSON Response:
{"Employs":[{"label":"Test1","key":123},{"label":"Test2","key":345},{"label":"Test3","key":567}],"identifier":"key"}
Upvotes: 0
Views: 977
Reputation: 2856
Answer Edited
In your jsp file you can use jquery
to populate combo box.
Code Updated
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/json3/3.3.2/json3.min.js"></script>
<script type="text/javascript">
loadEmplDropDown();
function loadEmplDropDown() {
$.getJSON("getEmploys.action", function(details) {
//convert json in html and set html
var options = '<option value="' + "--Select--" + '">'
+ "--Select--" + '</option>';
for ( var i = 0; i < details.employs.length; i++) {
//alert(details.employs[i].key);
options += '<option value="' + details.employs[i].key + '">'
+ details.employs[i].label + '</option>';
}
$("select#empindex").html(options);//or dojo.byId("empindex").innerHTML=options;
});
}
</script>
<s:select id="empindex" list="#{EmployList}" name="Employ" listKey="key" listValue = "label" emptyOption="true" value="%{destination.key.toString()}" />
Note:
1. Try with Capital E in employs because in your json it is Employs
instead of employes
for ( var i = 0; i < details.Employs.length; i++) {
//alert(details.Employs[i].items.key);
options += '<option value="' + details.Employs[i].key + '">'
+ details.Employs[i].label + '</option>';
}
Output:
Upvotes: 1