Have you ever wondered why the addOptions is not working in DWR even after reading the specs carefully. It happened to me recently. First, of all, the API or specs or documentation for DWR(addOptions Documentation) are not detailed and sufficient. Especially, for the new and impatient learners like me, who don’t want to go through each and every page of the spec before using the technology. Second, if you are impatient and new learner then probably you might have missed some of the part. Problem: I was trying to populate the Select box from a list of Objects using the addOptions method of DWR. I have followed the documentation fully but my drop down was not being populated. (It was my second day with DWR so I didn’t know some of the complexities which ultimately solved the problem)
Solution:- I will take one example to explain this Let your select field something like this
<select id=”statesList” />
And the class whose list of Objects you are trying to add is something like this
package testPackage.dwr;
public class State{
private String stateId;
private String stateName;
…………
//getter and setter methods
…………
}
and you want to populate this using DWR after some user action. Make sure that your callback function looks like this
var setValuesInDWR = function(data){
var fieldId = ’statesList’;
var valueText = ’stateId’;
var nameText = ’stateName’;
dwr.util.removeAllOptions(fieldId);// step 1
dwr.util.addOptions(fieldId, data, valueText, nameText);//step 2
};
All the above steps are important
Step 1 - Actually removes the already existing values from your select list. If you don’t do this. the addOptions method will simply append the list.
Step 2 - This will actually add the values to the drop-down list.
I forgot/missed/didn’t know that you have to add the following line in dwr.xml
<convert converter=”bean” match=”testPackage.dwr.State”/>
Reason is, unless you don’t explicitly specify in your dwr.xml the user defined beans will not be converted in DWR. If your bean consists of supported datatypes which DWR can automatically convert then you can just add this declaration. After following all these steps carefully the problem of Dropdown not getting populated in DWR should be solved.