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

时间:2015-08-17 12:31:13

标签: java list wicket

我有10个国家/地区的第一个列表,当我点击某个国家/地区时,另一个列表会被点击该国家/地区的状态填充...我选择4个州例如...我将会在国家列表并选择另一个国家,这里出现问题......国家的选择丢失......

更新国家/地区列表时,有没有办法保留此选项?

这是我的代码:

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

代码正在运行,请不要放在示例中但是我过滤了所选国家/地区,这100%,因为我发布的问题发生在我与州名单时选择另一个国家,然后我选择的州失去了选择权。

1 个答案:

答案 0 :(得分:1)

这是我的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>