How to populate a list box using a REST service ?
Answer:
The Java-script & Dojo code are shown below, where:
- REST service URL is /resources/industry
- We will send a GET request to retrieve a JSON object
- The JSON object carries industries related information and it contains an ID and name
- For each option tag: the ID content is assigned to the option value while the name content is assigned to the innerHTML
var xhrArgsIndustry = {
url : "/resources/industry",
preventCache : false,
handleAs : "json",
handle : function(response, ioargs) {
var message="";
switch (ioargs.xhr.status) {
case 200:
console.debug("server response: 200");
var id1=dojo.byId('industry');
dojo.forEach(response,function(item){
dojo.create("option",{value:item.id,innerHTML:item.name},id1,"last");
})
break;
default:
console.debug("server response: "+ioargs.xhr.status);
}
}
};
var reply1 = dojo.xhrGet(xhrArgsIndustry);
The HTML code will be as shown below:
<label for="industry"><strong>Industry</strong></label> <br>
<select dojoType="dijit.form.MultiSelect" id="industry" name="industry" size="10">
</select>
No comments:
Post a Comment