如何使用所选对象维护 Wicket 中的第二个项目列表 'ListMultipleChoice'

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

我有 10 个国家/地区的第一个列表,当我单击一个国家/地区时,另一个列表将由单击该国家/地区的州填充...我 select 例如 4 个州...我'将在国家/地区和 select 另一个国家/地区的列表中,这里出现了问题... select 国家的离子丢失了...。

更新国家/地区列表后,有什么办法可以保留这个 selection 吗? .

这是我的代码:

  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);

.

代码是 运行,不要放在示例中,但我正在过滤国家 selected 国家,这个 100%,因为我发布了问题发生在我有州列表选择另一个国家,然后我 selected 失去 selection.

这是我的 2 美分:我所做的就是将选定的选项存储到某个变量中,然后在国家选择更改时为州重置这些选项。

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;
    }
}

标记:

<?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>