在Wicket数据视图中使用PropertyModel从DropDownChoice获取数据

时间:2018-08-03 14:10:41

标签: java wicket dataview

我有以下检票口数据视图:

enter image description here
我有5个用户(分析师)和5个过滤器(进入数据库)。对于每种用户组合(过滤器),我都有一个count方法(此处未列出),该方法为我提供了一个值-显示在第3列中。

我的html看起来像这样:

    .......
    <table class="dataview">
        <tr wicket:id="users">
            <td wicket:id="userName">[User Name]</td>
            <td><select wicket:id="defaultFilter">[Default Filter]</select></td>
            <td wicket:id="count">[Assigned Messages To User]</td>
        </tr>
    </table>

我的Java代码如下:

public class UsersListPanel extends BasePanel {

    private UserRefreshingView userRefreshingView;

    UsersListPanel(String wicketId) {
        super(wicketId);
        setOutputMarkupId(true);
        setUserRefreshingView(new UserRefreshingView("users"));
        add(getUserRefreshingView());
    }

      class UserRefreshingView extends RefreshingView<SUser> {

        private AdvancedMessageFilter selectedFilt;

        UserRefreshingView(String id) {
            super(id);
        }

        AdvancedMessageFilter getSelectedFilt() {
            return selectedFilt;
        }

        void setSelectedFilt(AdvancedMessageFilter selectedFilt) {
            this.selectedFilt = selectedFilt;
        }

        @Override
        protected Iterator<IModel<SUser>> getItemModels() {
            List<SUser> assignableUsers = getUsers(getRiskType());
            return new ModelIteratorAdapter<SUser>(assignableUsers.iterator()) {
                protected IModel<SUser> model(SUser object) {
                    return new Model<>(object);
                }
            };
        }

        private class FilterChangedBehaviour extends AjaxFormComponentUpdatingBehavior {

            private int index;
            private String user;
            private AdvancedMessageFilter messageFilter;

            FilterChangedBehaviour(int index, String user, AdvancedMessageFilter messageFilter) {
                super("change");
                setIndex(index);
                setUser(user);
                setMessageFilter(messageFilter);
            }

            public int getIndex() { return index; }

            public void setIndex(int index) { this.index = index; }

            public String getUser() { return user; }

            public void setUser(String user) { this.user = user; }

            public AdvancedMessageFilter getMessageFilter() { return messageFilter; }

            public void setMessageFilter(AdvancedMessageFilter messageFilter) { this.messageFilter = messageFilter; }

            protected void onUpdate(AjaxRequestTarget target) {
                System.out.println(index+user+messageFilter.getName());
            }
        }

        protected void populateItem(final Item<SUser> assignableUsers) {
            final SUser user = assignableUsers.getModelObject();
            List<AdvancedMessageFilter> messageFilterList = 
                    getFiltersForUser(getPreferencesDao(), user.getUserId());
            assignableUsers.add(new Label("userName", user.getId()));
            int noOfMessages = 0;
            AdvancedMessageFilter defaultFilter = null;
            if(messageFilterList.size() > 0) {
                noOfMessages = count(user.getUserId(),  messageFilterList.get(0).getChoices());
                defaultFilter = messageFilterList.get(0);
                setSelectedFilt(defaultFilter);
            }
            List<AdvancedMessageFilter> allFilterList = getFiltersForUser(getPreferencesDao(), null);

            Component filter = new DropDownChoice<>("defaultFilter",
                    new PropertyModel<>(this, "selectedFilt"), allFilterList)
                    .setChoiceRenderer(new ChoiceRenderer<AdvancedMessageFilter>() {
                        public String getDisplayValue(AdvancedMessageFilter filt) {
                            return filt.getName();
                        }
                        public String getIdValue(AdvancedMessageFilter filt, int idx) {
                            return filt.getName();
                        }
                    }).setOutputMarkupId(true)
                        .add(new FilterChangedBehaviour(assignableUsers.getIndex(), user.getId(), getSelectedFilt()));

            assignableUsers.add(filter);
            assignableUsers.add(new Label("count", String.valueOf(noOfMessages)));
        }
    }
}

我有一个方法getFiltersForUser,它用第二个参数作为null来调用,它为我提供了所有过滤器,当用用户参数调用时,将为用户返回分配的过滤器。
问题是getSelectedFilt()始终指向Filter1。因此,此PropertyModel<>(this, "selectedFilt")进入数据视图无法正常工作。

数据视图应如下所示: enter image description here
为了使它看起来像这样,我应该将new PropertyModel<>(this, "selectedFilt"),替换为Model.of(defaultFilter),,这样我的下拉菜单将如下所示:

Component filter = new DropDownChoice<>("defaultFilter", Model.of(defaultFilter), allFilterList)
                    .setChoiceRenderer(new ChoiceRenderer<AdvancedMessageFilter>() {
                        public String getDisplayValue(AdvancedMessageFilter filt) {
                            return filt.getName();
                        }
                        public String getIdValue(AdvancedMessageFilter filt, int idx) {
                            return filt.getName();
                        }
                    }).setOutputMarkupId(true)
                        .add(new FilterChangedBehaviour(assignableUsers.getIndex(), user.getId(), getSelectedFilt()));

但是这样做,我不知道何时将值更改为下拉列表,选择的值是什么。
谢谢您帮助我解决这个问题!

0 个答案:

没有答案