0

I have a problem with lazy loading. Without lazyloading it is working fine but after lazy loading i get no data. I think there is a problem with my lazy data model creation.

edit: What I found is lazy model's load method is not calling.

Here is my datatable;

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

My Spring Controller;

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

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

    selectedCompany = new Company();     

}

My Data Model;

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 Answer 1

2

You have to override getRowCount method of LazyDataModel so that the table can now how many rows to show and how many pages. don't set rowCount inside load method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.