I have a page containing two combo boxes. I want to make them in a way that when I change the first combo box the content of the second combo box, which is got from database, is changed. How can I do it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The usual way to perform this kind of operations is through AJAX. You can easily add an AJAX Behavior to the first
DropDownChoicein order to populate/refresh the choices of the secondDropDownChoice.Let’s say you are using
IModels to get the choices of bothDropDownChoices. TheIModelthat gets the choices for the secondDropDownChoicewould be using the firstDropDownChoice‘s ModelObject (because it is dependent).You can attach an
AjaxFormComponentUpdatingBehaviorto the firstDropDownChoice. That will output anonchangeHTML event handler on the<select>tag, such that it will update thatDropDownChoice‘s model object with the selected value, and then will call your Behavior’sonUpdate(). In theonUpdate()method, you will only have to add the secondDropDownChoiceto theAjaxRequestTargetto write it back through the ajax response with the updated options. Remember to usesetOutputMarkupId(true)on all components you’ll be adding to theAjaxRequestTarget.For instance, for 2 dependent selects with countries and cities:
In case you’re not using
IModelsfor your choices (i.e. usingListobjects in the constructors), you’d just need to get the newListin theonUpdatemethod, and set it toddcCitywithsetChoices(). You can get theComponentthe Behavior is bounded to with thegetComponent()method.In case you want to support users without javascript, you should add a submit button (maybe in a
<noscript>tag?) with default from processing disabled, and perform that same logic in the button’sonSubmit.For additional reference, see this DropDownChoice Examples Wicket wiki page, you might find the “Ajax” section interesting.