我有两个DropDownChoice组件,分别为category1和category2。我想在更改类别1的选定值时更改类别2的列表。但是,在调用category1.getModelObject()方法时,category1的更改后的值始终保持初始值。
private void addCategoryChoice(Form form) {
List<Category1> category1List = category1Impl.listProduct();
ChoiceRenderer renderer1=new ChoiceRenderer<Category1>() {
@Override
public Object getDisplayValue(Category1 value) {
return value.getName();
}
};
DropDownChoice<Category1> category1 = new DropDownChoice<Category1>("category1",
new Model<Category1>(category1List.get(1)) , category1List,renderer1);
category2List = category2Imple.listByCategory1Id(category1.getModelObject().getId());
ChoiceRenderer renderer2=new ChoiceRenderer<Category2>() {
@Override
public Object getDisplayValue(Category2 value) {
return value.getName();
}
};
DropDownChoice<Category2> category2 = new DropDownChoice("category2",category2List,renderer2);
form.add(category2);
category1.add(new AjaxEventBehavior("change") {
@Override
protected void onEvent(AjaxRequestTarget target) {
category2List.clear();
int id=category1.getModelObject().getId();
category2List.addAll(category2Imple.listByCategory1Id(Integer.valueOf(id)));
category2.setChoices(category2List);
}
});
form.add(category1);
}
AjaxEventBehavior不会更新组件模型。您应该改用OnChangeAjaxBehavior。