addOptions problem in DWR
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.
December 21, 2007 Posted by Paras | Ajax using DWR | addOptions for array of objects, addOptions not working in DWR, addOptions problem in DWR Ajax, Ajax, DWR, DWR API | 5 Comments
About Me
Hello. Thanks for visiting my blog. I am Paras Jain. I am a Riveter – team member of Rivet Logic Corporation in Reston, Virginia, USA. I am working as Senior Software Engineer. Currently most of my work involves implementing solutions involving Liferay and Alfresco. I have experience in Grails, Spring, Struts, Hibernate, JSF, JBoss Seam and many others. I am also technical owner of few websites developed in WordPress, Drupal and Prestashop. I am a Sun Certified Java Professional. You can reach me at my email id http://scr.im/parasjain01 or http://scr.im/parasjain
Categories
-
Blog Stats
- 206,545 hits
Category Cloud
-
Recent Comments
- Moayad Abu Jaber on org.hibernate.QueryParameterException: could not locate named parameter
- hadi on How to configure JCaptcha with your Spring App
- shasisekar on Hibernate Error – Identifier of an instance altered from 1 to 1
- Paras on org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing:
- Waqas Ahmed on org.hibernate.TransientObjectException: object references an unsaved transient instance – save the transient instance before flushing: