0

我有延迟加载的问题。没有延迟加载它工作正常,但延迟加载后我没有得到任何数据。我认为我的懒惰数据模型创建存在问题。

编辑:我发现惰性模型的加载方法没有调用。

这是我的数据表;

<p:dataTable id="companiesDataTable"
                         var="company" value="#{myController.lazyCompanyDataModel}"
                         rowKey="#{company.uuid}"
                         lazy="true"
                         sortMode="multiple" rows="5" paginator="true">

我的弹簧控制器;

private LazyDataModel<Company> lazyCompanyDataModel;
private List<Company> companyList;

@Override
public void init() {
    lazyCompanyDataModel = new LazyCompanyDataModel(companyList);

    selectedCompany = new Company();     

}

我的数据模型;

public class LazyCompanyDataModel extends LazyDataModel<Company> {  


GenericService genericService;

private List<Company> datasource;
private int pageSize;
private int rowIndex;
private int rowCount;

public LazyCompanyDataModel(List<Company> datasource) {  
    this.datasource = datasource;        
}

@Override
public List<Company> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
    datasource = genericService.getByTemplate(new Company(), first, pageSize,new Order(null,Order.convertSortOrder(sortField)) );
    setRowCount((int) genericService.getCountByTemplate(new Company()));
    return datasource;
}
 @Override
public boolean isRowAvailable() {
    if(datasource == null)
        return false;
    int index = rowIndex % pageSize ;
    return index >= 0 && index < datasource.size();
}

@Override
public Object getRowKey(Company company) {
    return company.toString();
}

@Override
public Company getRowData() {
    if(datasource == null)
        return null;
    int index =  rowIndex % pageSize;
    if(index > datasource.size()){
        return null;
    }
    return datasource.get(index);
}

@Override
public Company getRowData(String rowKey) {
    if(datasource == null)
        return null;
   for(Company company : datasource) {
       if(company.toString().equals(rowKey))
       return company;
   }
   return null;
}

@Override
public void setPageSize(int pageSize) {
    this.pageSize = pageSize;
}

@Override
public int getPageSize() {
    return pageSize;
}

@Override
public int getRowIndex() {
    return this.rowIndex;
}

@Override
public void setRowIndex(int rowIndex) {
    this.rowIndex = rowIndex;
}

@Override
public void setRowCount(int rowCount) {
    this.rowCount = rowCount;
}

@Override
public int getRowCount() {
    return this.rowCount;
}

@Override
public void setWrappedData(Object list) {
    this.datasource = (List<Company>) list;
}

@Override
public Object getWrappedData() {
    return datasource;
}

}

4

1 回答 1

2

您必须重写 LazyDataModel 的 getRowCount 方法,以便表格现在可以显示多少行和多少页。不要在 load 方法中设置 rowCount。

于 2013-05-01T11:36:13.107 回答