首页 > 解决方案 > Wicket Dropdownchoice 不能改变选中的值

问题描述

我有 category1 和 category2 的两个 DropDownChoice 组件。我想在更改 category1 的选定值时更改 category2 的列表。但是在调用 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);
}

标签: wicketdropdownchoice

解决方案


AjaxEventBehavior 不更新组件模型。您应该改用 OnChangeAjaxBehavior。


推荐阅读