Java Wicket AJAX刷新分页DataView

发布于 2024-12-07 06:59:24 字数 2505 浏览 0 评论 0原文

我有一个页面,其中包含在页面显示时动态加载的 Hibernate 实体列表。该列表用于创建 DataView,该 DataView 用于在容器中显示列表中条目的分页列表。列表中的每个条目都有一个删除图标。当按下删除图标时,我会延迟删除该条目,重新加载实体列表(将不再包含延迟删除的条目),然后重新加载容器,但该条目仍然存在于容器中,直到我重新加载整个页面。为什么?

public class LogPage extends ProjectPage{

    @SpringBean
    private LogDao logDao;
    @SpringBean
    private LogEntryDao logEntryDao;

    private List<LogEntry> logEntryList;
    private DataView<LogEntry> dataView;
    private WebMarkupContainer logEntryListContainer;   

    public LogPage(PageParameters pp) {
        super(pp);
        Project activeProject = SciProSession.get().getActiveProject();
        Log log = null;
        if (activeProject.getLog()==null){
            log = new Log(activeProject);
            log = logDao.save(log);
        }else{
            log = activeProject.getLog();
        }
        logEntryList = logEntryDao.findAll();
        Collections.sort(logEntryList);

        logEntryListContainer = new WebMarkupContainer("logEntryListContainer");
        logEntryListContainer.setOutputMarkupId(true);

        dataView = (DataView<LogEntry>) new DataView<LogEntry>("logEntryDataView", new ListDataProvider(logEntryList)) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final Item<LogEntry> item) {
                final LogEntry logEntry = item.getModelObject();

                item.add(new Label("contents", logEntry.getContents()));
                item.add(new Label("creator", logEntry.getCreator().toString()));

                AjaxActionIcon deleteIcon = new AjaxActionIcon("deleteIcon", ImageIcon.ICON_DELETE){
                    private static final long serialVersionUID = 1L;
                    @Override
                    protected void onClick(AjaxRequestTarget target) {
                        LogEntry toBeRemoved = logEntryDao.reLoad(logEntry);
                        toBeRemoved.setDeleted(true);
                        logEntryDao.save(toBeRemoved);
                        logEntryList = logEntryDao.findAll();
                        target.addComponent(logEntryListContainer);

                    }
                };
                item.add(deleteIcon);               
            }
        };

        dataView.setItemsPerPage(10);
        logEntryListContainer.add(dataView);
        logEntryListContainer.add(new PagingNavigator("navigator", dataView));
        add(logEntryListContainer);

    }


}

I have a page that has a list of Hibernate entities that is loaded dynamically on display of the page. The list is used to create a DataView, which is used to display a paginated list of the entries in the list in a container. Each entry in the list has a delete icon on it. When the delete icon is pressed, I lazy delete the entry, reload the list with entities (which will no longer contain the lazy deleted entry), and reload the container, but the entry is still there in the container until I reload the whole page. Why?

public class LogPage extends ProjectPage{

    @SpringBean
    private LogDao logDao;
    @SpringBean
    private LogEntryDao logEntryDao;

    private List<LogEntry> logEntryList;
    private DataView<LogEntry> dataView;
    private WebMarkupContainer logEntryListContainer;   

    public LogPage(PageParameters pp) {
        super(pp);
        Project activeProject = SciProSession.get().getActiveProject();
        Log log = null;
        if (activeProject.getLog()==null){
            log = new Log(activeProject);
            log = logDao.save(log);
        }else{
            log = activeProject.getLog();
        }
        logEntryList = logEntryDao.findAll();
        Collections.sort(logEntryList);

        logEntryListContainer = new WebMarkupContainer("logEntryListContainer");
        logEntryListContainer.setOutputMarkupId(true);

        dataView = (DataView<LogEntry>) new DataView<LogEntry>("logEntryDataView", new ListDataProvider(logEntryList)) {
            private static final long serialVersionUID = 1L;

            @Override
            protected void populateItem(final Item<LogEntry> item) {
                final LogEntry logEntry = item.getModelObject();

                item.add(new Label("contents", logEntry.getContents()));
                item.add(new Label("creator", logEntry.getCreator().toString()));

                AjaxActionIcon deleteIcon = new AjaxActionIcon("deleteIcon", ImageIcon.ICON_DELETE){
                    private static final long serialVersionUID = 1L;
                    @Override
                    protected void onClick(AjaxRequestTarget target) {
                        LogEntry toBeRemoved = logEntryDao.reLoad(logEntry);
                        toBeRemoved.setDeleted(true);
                        logEntryDao.save(toBeRemoved);
                        logEntryList = logEntryDao.findAll();
                        target.addComponent(logEntryListContainer);

                    }
                };
                item.add(deleteIcon);               
            }
        };

        dataView.setItemsPerPage(10);
        logEntryListContainer.add(dataView);
        logEntryListContainer.add(new PagingNavigator("navigator", dataView));
        add(logEntryListContainer);

    }


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-12-14 06:59:24

您正在更改变量 logEntryList 指向的内容,但这不会影响新 ListDataProvider(logEntryList) 所看到的内容。

重新加载后,您可以做的是

  • logEntryList.clear().addAll(logEntryDao.findAll()) ,以便更新数据提供程序指向的变量
  • 提供您自己的 DataProvider 实现

You are changing what the variable logEntryList points to, but that does not impact what the new ListDataProvider(logEntryList) sees.

Upon reload, what you could do is

  • logEntryList.clear().addAll(logEntryDao.findAll()) so that the variable to which the data provider points is updated
  • provide your own DataProvider implementation
你列表最软的妹 2024-12-14 06:59:24

您可以在创建 DataView 时传入 logEntries 列表。此后对列表的任何更改都不会反映出来。尝试将列表包装在 PropertyModel 中并为其提供 getter。

You pass in the list of logEntries upon creation of the DataView. Any changes to the list afterwards will not be reflected. Try wrapping the list in a PropertyModel and provide a getter for it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文