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 »
Leave a Reply Cancel reply
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:
Hello
I have the same problem using addOptions. I have a java class containing a function that returns array of strings. I have no getter and setter methods. Is it possible for me to separate from this array the values and the texts that I will use in the select field? Do you have some java examples? In your example you initialized valueText to ‘stateId’ and nameText to ‘stateName’ and then you use it in addOptions. Why ‘stateId’ and ‘stateName’? Do they have to be the same as the java variables?
Thank you very much.
Regards,
Tobbi
Hi,
Yes we have to use exactly the same name as the Java variables. Then only DWR would be able to know the getter and setter methods. For instance, in my example DWR will call getStateName() and setStateName(..) for the Name of the drop downs.
The getter and setter methods are required only for the Array of objects. For array of Strings you have to use.
For example,
your method which returns string value is getStringArray()class ExampleClass. And the id of the dropdown in JSP is ‘dropDownId’. Your code will be something like this
objOfExampleClass.getStringArray(setDropDownFunction);
function setDropDownFunction(data){
dwr.util.addOptions(‘dropDownId’, data);
}
I have never tried this in my code. You try this and let me know the results
Thanks for this! I have been trying to get a DWR example to work for hours, your example finally got everything working for me.
Thanks Paras, for explaining this very carefully.
Excellent post. I had exactly the same problem as you – didn’t know about adding the entry in the dwr.xml. The joys of picking a technology up by tweaking other’s existing code