p:dataTable 选择在对 LazyDataModel 进行分页后丢失 [英] p:dataTable selections are lost after paginating a LazyDataModel

查看:21
本文介绍了p:dataTable 选择在对 LazyDataModel 进行分页后丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,在我选择了第一页上的几个项目后,如果我分页到另一个页面并返回,则不会显示我的初始选择.我尝试实现 SelectableDataModel 以及使用 rowKey 属性,但问题仍然存在.

My problem is that after I've selected a few items on the 1st page, if I paginate to another page and come back, my initial selections are not shown. I've tried to implement the SelectableDataModel as well as using the rowKey attribute but the problem persists.

这是我的测试 bean:

This is my test bean:

@ManagedBean
@ViewScoped
public class MrBean {
    private List<Item> chosenItems;
    private LazyDataModel lazyModel;

    @PostConstruct
    public void prepareTest() {
        this.lazyModel = new LazyItemDataModel();
    }

    public void countItems() {
        System.out.println("TEST 3: chosenItems's size: " + chosenItems.size());
    }

    private class LazyItemDataModel extends LazyDataModel<Item> implements SelectableDataModel<Item> {
        @Override
        public Item getRowData(String rowKey) {
            System.out.println("TEST 1: getRowData");
            Iterator<Item> iter = ((List<Item>) this.getWrappedData()).iterator();
            while (iter.hasNext()) {
                Item item = iter.next();
                if (item.getId().equals(rowKey)) {
                    return item;
                }
            }

            return null;
        }

        @Override
        public Object getRowKey(Item item) {
            return item.getId();
        }

        @Override
        public List<Item> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) {
            System.out.println("TEST 2: load");
            // Code to retrieve items from database
        }
    }

    // Getters and Setters
}

这是我的测试页:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>Test page</title>
    </h:head>
    <h:body>
        <h:form>
            <p:dataTable id="itemTable" var="item" value="#{mrBean.items}" rows="5" 
                         paginator="true" selection="#{mrBean.chosenItems}" lazy="true" >

                <p:ajax event="rowSelectCheckbox" listener="mrBean.countItems" />                    

                <p:column selectionMode="multiple" />

                <p:column headerText="ID">
                    <h:outputText value="#{item.id}" /> 
                </p:column>

                <p:column headerText="Name">
                    <h:outputText value="#{item.name}" /> 
                </p:column>

            </p:dataTable>
        </h:form>
    </h:body>
</html>

如果您能告诉我我在这里做错了什么,我将不胜感激.

I'd be very grateful if you could show me what I've done wrong here.

更新:在我在上面的代码中添加更多的System.out.println("TEST")之后,我观察到了以下几点:

UPDATE: After I added more System.out.println("TEST") to the above code, I observed the following things:

  1. 在控制台上,每次我分页时,TEST 1: getRowData 总是在 TEST 2: load 之前打印.因此,我相信方法 #LazyDataModel.getWrappedData() 可能会从旧页面返回数据.起初,我认为这个方法的目标是检索选定的行以在表格上突出显示.但是,如果在 load 之前调用此方法,它就无法完成这项工作吗?
  2. 在第一页上选择第 1 个 2 项后,在控制台上,我看到 TEST 3: selectedItems's size: 2.如果我分页到第 2 页,然后返回到第 1 页,则如上所述,选择将丢失.但是,如果我继续选择另一个项目,在控制台上,我看到了TEST 3: selectedItems's size: 3.显然,chosenItems 列表仍然保留了我的旧选择,但它们并未呈现在表格中.
  1. On the console, every time I paginate, TEST 1: getRowData is always printed before TEST 2: load. As a consequence, I believe the method #LazyDataModel.getWrappedData() may return data from the old page. At first, I thought this method's goal was to retrieve the selected rows to highlight on the table. However, if this method is called before load, there's no way it can do the job right?
  2. After I selected the 1st 2 items on the 1st page, on the console, I saw TEST 3: chosenItems's size: 2. If I paginate to the 2nd page and then back to the 1st page, the selections are lost as mentioned. However, if I continued to select another item, on the console, I saw TEST 3: chosenItems's size: 3. Obviously, the chosenItems list still kept my old selections but they're not rendered on the table.

推荐答案

在webPage中添加一个页面切换事件:

In webPage just add a event for when page switch:

<p:ajax event="page" listener="#{listingBean.updateSelected()}" />

在listingBean中,只保存选中的:

In the listingBean, just save the selected:

private List<Entity> selectedInstances;
private List<Entity> selectedInstancesSaved;

public List<Entity> getSelectedInstances()
{
    return selectedInstancesSaved;
}

public void setSelectedInstances(List<Entity> selectedInstances)
{
    this.selectedInstances = selectedInstances;
}

public void updateSelected()
{
    if (selectedInstances != null && !selectedInstances.isEmpty()) {
        for (Entity inst : lazyModel.getDatasource()) {
            if (selectedInstances.contains(inst)) {
                selectedInstancesSaved.add( inst);
            } else {
                selectedInstancesSaved.remove( inst);
            }
        }
    }
}

这篇关于p:dataTable 选择在对 LazyDataModel 进行分页后丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆