我在wicket中创建了一个调色板,我可以填充调色板左侧窗口。但我想让调色板中的选项是动态的,能够做同样的事情。但问题是调色板没有填充选定的选项。 我在调色板上方有一个下拉菜单,具体取决于我想要填充调色板的选项。
例如(带有值的下拉列表(角色)): 1.scenario :下拉菜单 - > TL,经理,SE,SSE pallete - >最初没有值,在创建角色TL时,将在调色板的rite窗口中选择技能。假设TL角色是由10种技能中的4种技能创建的。 2.scenario:我想编辑角色TL,在这种情况下,当我选择角色TL时,调色板应填充到所选择的应该在右边,可用的应该在左边。 我在第二种情况下失败了。
以下是代码:
public class DropDownChoicePage extends WebPage {
private String selectedMake;
private final Map<String, List<String>> modelsMap = new HashMap<String, List<String>>();
private List<String> selected = new ArrayList<String>();
private String selectedEngine = "Google";
public DropDownChoicePage(final PageParameters parameters) {
add(new FeedbackPanel("feedback"));
modelsMap.put("AUDI", Arrays.asList(new String[] { "A4", "A6", "TT" }));
modelsMap.put("CADILLAC", Arrays.asList(new String[] { "CTS", "DTS", "ESCALADE", "SRX","DEVILLE" }));
modelsMap.put("FORD", Arrays.asList(new String[] { "CROWN", "ESCAPE", "EXPEDITION","EXPLORER", "F-150" }));
IModel<List<? extends String>> makeChoices = new AbstractReadOnlyModel<List<? extends String>>()
{
private static final long serialVersionUID = 1L;
@Override
public List<String> getObject()
{
Set<String> keys = modelsMap.keySet();
List<String> list = new ArrayList<String>(keys);
return list;
}
};
IModel<List<? extends String>> paletteChoices = new AbstractReadOnlyModel<List<? extends String>>(){
private static final long serialVersionUID = 1L;
@Override
public List<? extends String> getObject() {
List<String> models = modelsMap.get(selectedMake);
if (models == null)
{
models = Collections.emptyList();
}else if("FORD".equals(selectedMake)){
selected.add("CROWN");
selected.add("ESCAPE");
}
return models;
}
};
final DropDownChoice<String> makes = new DropDownChoice<String>("makes",new PropertyModel<String>(this, "selectedMake"), makeChoices);
IChoiceRenderer<String> renderer = new ChoiceRenderer<String>();
final Palette<String> palette = new Palette<String>("palette",
new ListModel<String>(selected),
paletteChoices,
renderer, 10, true);
palette.setOutputMarkupId(true);
makes.add(new AjaxFormComponentUpdatingBehavior("onchange")
{
private static final long serialVersionUID = 1L;
@Override
protected void onUpdate(AjaxRequestTarget target)
{
selected = new ArrayList<String>();
target.addComponent(palette);
System.out.println("AjaxFormComponentUpdatingBehavior(\"onchange\")");
System.out.println("Selected Make:"+selectedMake);
}
});
Form<?> form = new Form<Void>("form") {
private static final long serialVersionUID = 1L;
@Override
protected void onSubmit() {
info("Selected search engine : " + selectedEngine);
selected = new ArrayList<String>();
}
};
add(form);
form.add(palette);
form.add(makes);
}
}
请你帮我解决第二种情况,即在调色板的左右窗口填充下拉变化事件的可用和选定值。
感谢, 萨兰。
答案 0 :(得分:1)
我认为您的代码中存在两个主要问题,这些问题会阻止第二种方案正常工作:
(1)在onchange Ajax行为中,以下代码不会更新所选选项的ListModel:
selected = new ArrayList<String>();
请改为尝试:
selected.clear();
(2)将已选择的选项添加到列表中的代码应该在Ajax行为中,即:
makes.add(new AjaxFormComponentUpdatingBehavior("onchange") {
private static final long serialVersionUID = 1L;
@Override
protected void onUpdate(AjaxRequestTarget target) {
selected.clear();
List<String> alreadySelected = getAlreadySelectedChoicesBasedOnDropdownValue();
selected.addAll(alreadySelected);
//...
}
}
getAlreadySelectedChoicesBasedOnDropdownValue
将返回已选择的新选项列表,例如:
List<String> getAlreadySelectedChoicesBasedOnDropdownValue() {
if("FORD".equals(selectedMake)){
return Arrays.asList("CROWN", "ESCAPE");
}
}