Ronald Calazans
Ronald Calazans

Reputation: 71

How to maintain the second list of items 'ListMultipleChoice' in Wicket with the selected objects

I have the first list of 10 countries, when I click on a country another list is populated by the states of that country clicked... I select a 4 states for example ... I'll be on the list of countries and select another country, and here occurs the problem ... the selection of states is lost ... .

Is there any way to keep this selection when the country list is updated? .

Here my code:

  private List<Country> selectedCountry;
  private List<Country> selectedState;
.  
.
  List listCountry=countryService.assembleList();

  ListMultipleChoice listMultiplaCountry = new ListMultipleChoice("multipleCountry",new PropertyModel(this, "selectedCountry"),listCountry,new ChoiceRenderer<Country>("nameCountry"));

  ListMultipleChoice listStates= = new ListMultipleChoice("selectedStates",new PropertyModel(this, "selectedState"),listAllStates,new ChoiceRenderer<States>("nameState"));
    acaoAdicionarMunicipio(listMultiplaUfs);

.

The code is running, do not put in the example but I'm filtering states the selected country, this 100%, as I posted the problem occurs when I have being with the list of states choose another country, then the states I had selected lose selection.

Upvotes: 0

Views: 346

Answers (1)

Mihir
Mihir

Reputation: 270

Here is my 2 cents: All I have done is store selected choices into some variable and then reset those choices for the state on country selection change.

public class TestPage3 extends WebPage {

    FeedbackPanel feedbackPanel;
    Set<String> selectedStates = new HashSet<String>();
    Set<String> selectedCountries = new HashSet<String>();
    Set<String> allStatesSelected = new HashSet<String>();

    public TestPage3(final PageParameters parameters) {

        feedbackPanel = new FeedbackPanel("feedback");
        feedbackPanel.setOutputMarkupId(true);
        add(feedbackPanel);

        Form<Void> form = new Form<Void>("form");
        add(form);

        addFormComponents(form);
    }

    private void addFormComponents(Form<Void> form) {
        ListMultipleChoice<String> statesSelection = addStatesMultipleChoicesBox(form);
        ListMultipleChoice<String> countriesSelection = addCountryMultipleChoiceBox(form, statesSelection);
    }

    private ListMultipleChoice<String>  addCountryMultipleChoiceBox(Form<Void> form, final ListMultipleChoice<String> statesSelection) {

        final Map<String, String> countryMap = new HashMap<String, String>();

        countryMap.put("US", "United States of America");
        countryMap.put("IN", "India");
        countryMap.put("JA", "Japan");
        countryMap.put("AA", "AA");
        countryMap.put("BB", "BB");

        final ListMultipleChoice<String> choices = new ListMultipleChoice<String>("countries", new PropertyModel<Collection<String>>(this, "selectedCountries"), new ArrayList<String>(countryMap.keySet()));
        choices.add(new AjaxFormComponentUpdatingBehavior("onchange") {
            @Override
            protected void onUpdate(AjaxRequestTarget target) {
                if (allStatesSelected != null && allStatesSelected.size() > 0){
                    statesSelection.setChoices(new ArrayList<String>(allStatesSelected));
                    target.add(statesSelection);
                }
            }
        });

        choices.setOutputMarkupId(true);
        form.add(choices);
        return choices;
    }

    private ListMultipleChoice<String> addStatesMultipleChoicesBox(Form<Void> form) {

        final Map<String, String> statesMap = new HashMap<String, String>();

        statesMap.put("AK", "Arkansas");
        statesMap.put("FL", "Florida");
        statesMap.put("IL", "Illinois");
        statesMap.put("SA", "State A");
        statesMap.put("SB", "State B");
        statesMap.put("SC", "State C");
        statesMap.put("SD", "State D");

        final ListMultipleChoice<String> choices = new ListMultipleChoice<String>("states", new PropertyModel<Collection<String>>(this, "selectedStates"), new ArrayList<String>(statesMap.keySet())){

            @Override
            protected void onModelChanged() {
                for (String selectedState : selectedStates){
                    allStatesSelected.add(selectedState);
                }
            }
        };

        choices.setOutputMarkupId(true);
        form.add(choices);

        return choices;
    }

    public Set<String> getSelectedStates() {
        return selectedCountries;
    }

    public void setSelectedStates(Set<String> selectedStates) {
        this.selectedCountries = selectedStates;
    }

    public Set<String> getSelectedCountries() {
        return selectedCountries;
    }

    public void setSelectedCountries(Set<String> selectedCountries) {
        this.selectedCountries = selectedCountries;
    }
}

Markup:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:wicket>
<head>
</head>
<body>
<div wicket:id="feedback"></div>
<form wicket:id="form">
    <select wicket:id="countries"></select>
    <select wicket:id="states"></select>
</form>
</body>
</html>

Upvotes: 1

Related Questions