firefly
firefly

Reputation: 1

Wicket Dropdownchoice can not change the selected value

I have two DropDownChoice component of category1 and category2. I want change the list of category2 when changing the selected value of category1. But the changed value of category1 always keep the init value when the method of category1.getModelObject() invoked.

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

Upvotes: 0

Views: 86

Answers (1)

Andrea Del Bene
Andrea Del Bene

Reputation: 2511

AjaxEventBehavior doesn't update component model. You should use OnChangeAjaxBehavior instead.

Upvotes: 2

Related Questions