Wicket CheckBoxMultipleChoice不会更新

时间:2011-06-17 21:58:17

标签: java ajax wicket

我的wicket面板需要至少两次点击才能使用我的按钮的onSubmit方法更新任一CheckBoxMulitpleChoice组件。如何在第一次点击时更新这些组件?

我使用两个CheckBoxMultipleChoice组件来编译要通知的用户列表。第一个是根据DropDownChoice团队选择中可用的用户填充的。然后,用户可以从该团队中选择用户以添加到第二CheckBoxMultipleChoice,其显示要通知的所有用户并允许用户移除用户。

我尝试使用Palette组件,但是使用的是wicket 1.3.1(我遇到了迁移到1.4的麻烦,但这是另一篇文章)并且还没有成功控制UI。我也尝试将组件放在表单中,但这并没有改变功能。它需要至少2次单击才能从复选框中添加或删除条目。似乎getValue()直到执行了按钮行为后才更新。

        // Team selection for notification =================================
        final DropDownChoice teamNotificationChoice = new DropDownChoice("teamNotification", teamList, new IChoiceRenderer() {
            public Object getDisplayValue(Object o) {
                return ((Team) o).getName();
            }
            public String getIdValue(Object o, int i) {
                return Long.toString(((Team) o).getId());
            }
        });
        notifySelectionList.add(teamNotificationChoice);

        // teamUser selection list for notification ========================
        List<ItemUser> choices = UserUtils.convertToItemUserListFromUsers(getJtrac().findUsersForSpace(space.getId()));
        teamUsers = new CheckBoxMultipleChoice("teamUsers", choices, new IChoiceRenderer() {
            public Object getDisplayValue(Object o) {
                return ((ItemUser) o).getUser().getName();
            }
            public String getIdValue(Object o, int i) {
                return ((ItemUser) o).getUser().getLoginName();
            }
        });
        notifySelectionList.add(teamUsers);

        // Add selected teamUsers button ===================================
        Button button = new Button("addUsersToList") {
            @Override
            public void onSubmit(){
            }
        };
        button.add(new AjaxFormComponentUpdatingBehavior("onClick") {
            protected void onUpdate(AjaxRequestTarget target) {
                List choices = teamUsers.getChoices();
                String value = teamUsers.getValue();
                for (int index = 0; index < choices.size(); index++) {
                    final ItemUser choice = (ItemUser) choices.get(index);
                    if(isSelected(choice, index, value)&!userSelection.contains(choice)) {
                        userSelection.add(choice);
                    }
                }
                SortUtils.sortItemUsers(userSelection);
                itemUsers.setChoices(userSelection);
                target.addComponent(itemUsers);
            }
        });
        notifySelectionList.add(button);
        notifySelectionList.setOutputMarkupId(true);

        // notify list ===================================================
        itemUsers = new CheckBoxMultipleChoice("itemUsers", userSelection, new IChoiceRenderer() {
            public Object getDisplayValue(Object o) {
                return ((ItemUser) o).getUser().getName();
            }
            public String getIdValue(Object o, int i) {
                return ((ItemUser) o).getUser().getLoginName();
            }
        });
        itemUsers.setMaxRows(10);
        itemUsers.setOutputMarkupId(true);
        notifyList.setOutputMarkupId(true);
        notifyList.add(itemUsers);

        // Remove selected teamUsers button ===================================
        Button removeButton = new Button("removeUsersFromList") {
            @Override
            public void onSubmit(){
            }
        };
        removeButton.add(new AjaxFormComponentUpdatingBehavior("onClick") {
            protected void onUpdate(AjaxRequestTarget target) {
                List choices = itemUsers.getChoices();
                String value = itemUsers.getValue();
                for (int index = 0; index < choices.size(); index++) {
                    final ItemUser choice = (ItemUser) choices.get(index);
                    if(isSelected(choice, index, value)) {
                        userSelection.remove(choice);
                    }
                }
                itemUsers.setChoices(userSelection);
                target.addComponent(itemUsers);
            }
        });
        notifyList.add(removeButton);

提前感谢您提供任何建议......

1 个答案:

答案 0 :(得分:1)

好的,我不知道是否有人关心,因为我没有得到任何反馈,我想这不是一个常见的问题,但我会把我的解决方案放在这里,以防它可以帮助某人。

我可以通过删除AjaxFormComponentUpdatingBehavior()并使用按钮的onSubmit()方法来解决问题。我还在按钮上setDefaultFormProcessing(false),以便只更新CheckboxMultipleChoice面板。现在看来如下:

        // Team selection for notification =================================
        final DropDownChoice teamNotificationChoice = new DropDownChoice("teamNotification", teamList, new IChoiceRenderer() {
            public Object getDisplayValue(Object o) {
                return ((Team) o).getName();
            }
            public String getIdValue(Object o, int i) {
                return Long.toString(((Team) o).getId());
            }
        });
        notifySelectionList.add(teamNotificationChoice);

        // teamUser selection list for notification ========================
        List<ItemUser> choices = UserUtils.convertToItemUserListFromUsers(getJtrac().findUsersForSpace(space.getId()));
        teamUsers = new JtracCheckBoxMultipleChoice("teamUsers", choices, new IChoiceRenderer() {
            public Object getDisplayValue(Object o) {
                return ((ItemUser) o).getUser().getName();
            }
            public String getIdValue(Object o, int i) {
                return ((ItemUser) o).getUser().getLoginName();
            }
        });
        notifySelectionList.add(teamUsers);

        // Add selected teamUsers button ===================================
        Button button = new Button("addUsersToList") {
            @Override
            public void onSubmit(){
                List choices = teamUsers.getChoices();
                String value = teamUsers.getValue();
                for (int index = 0; index < choices.size(); index++) {
                    final ItemUser choice = (ItemUser) choices.get(index);
                    if(isSelected(choice, index, value)&!userSelection.contains(choice)) {
                        userSelection.add(choice);
                    }
                }
                SortUtils.sortItemUsers(userSelection);
                itemUsers.setChoices(userSelection);
                notifyList.add(itemUsers);
                teamUsers.updateModel();
            }
        };
        button.setDefaultFormProcessing(false);
        notifySelectionList.add(button);
        notifySelectionList.setOutputMarkupId(true);

        // notify list ===================================================
        itemUsers = new JtracCheckBoxMultipleChoice("itemUsers", userSelection, new IChoiceRenderer() {
            public Object getDisplayValue(Object o) {
                return ((ItemUser) o).getUser().getName();
            }
            public String getIdValue(Object o, int i) {
                return ((ItemUser) o).getUser().getLoginName();
            }
        });
        itemUsers.setMaxRows(10);
        itemUsers.setOutputMarkupId(true);
        notifyList.setOutputMarkupId(true);
        notifyList.add(itemUsers);

        // Remove selected teamUsers button ===================================
        Button removeButton = new Button("removeUsersFromList") {
            @Override
            public void onSubmit(){
                List choices = itemUsers.getChoices();
                String value = itemUsers.getValue();
                if(value!=""){
                    String[] valueList = itemUsers.getValue().split(";");
                    List<User> userList = new ArrayList<User>();
                    for (String s:valueList){
                        userList.add(getJtrac().loadUser(s));
                    }
                    List<ItemUser> itemUserList = UserUtils.convertToItemUserListFromUsers(userList);
                    for (ItemUser iu:itemUserList) {
                        userSelection.remove(iu);
                    }
                    itemUsers.setChoices(userSelection);
                    notifyList.add(itemUsers);
                }
            }
        };
        removeButton.setDefaultFormProcessing(false);
        notifyList.add(removeButton);