gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:13:59 24 4
gpt4 key购买 nike

我有第一个包含 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%,正如我发布的那样,当我在州列表中选择另一个国家/地区时,就会出现问题,然后是州我选择了放弃选择。

最佳答案

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

关于java - 如何使用选定的对象维护 Wicket 中的第二个项目列表 'ListMultipleChoice',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32050488/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com