延迟加载数据表中没有数据

时间:2013-04-29 08:00:26

标签: jsf primefaces lazy-loading

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

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

这是我的数据表;

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

}

1 个答案:

答案 0 :(得分:1)

您必须覆盖LazyDataModel的getRowCount方法,以便该表现在可以显示多少行以及多少页。不要在load方法中设置rowCount。