开发者

Java Wicket AJAX refresh paginated DataView

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

    }


}


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜