Marshall
Marshall

Reputation: 1195

JSF2 lazy loading - how to get next page programmatically?

I've implemented DataTable - Lazy Loading sample. Similar to this one, but I load data from database - it's not important though.

So I have:

public class LazyCarDataModel extends LazyDataModel<Car> { 

    ...
}

and

public class TableBean {  

    private LazyDataModel<Car> lazyModel;  

    ...
}

Somewhere, very deep in the procject I need to know what the current page of this Car list looks like. So I do:

List<T> currentCarList = (List<T>)getTestTableBean().getLazyModel().getWrappedData();

and I have. Last loaded cars (currently visiable in the browser). But, I also need to see, what WILL BE next and previos page. I mean before user clicks "Next page" there will be a warning massage "attention! there will be Ferrari on the next page!" :D

So, finally my question: How to get next and previos page when using lazy loading?

Upvotes: 1

Views: 899

Answers (1)

BalusC
BalusC

Reputation: 1108802

You can just keep track of it yourself in the LazyDataModel#load() method.

private List<Car> prev;
private List<Car> next;

@Override
public List<Car> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) {
    this.setRowCount(service.count());
    this.prev = service.list(first - pageSize, pageSize);
    this.next = service.list(first + pageSize, pageSize);
    return service.list(first, pageSize);
}    

public List<Car> getPrev() {
    return prev;
}

public List<Car> getNext() {
    return next;
}

Wherever you need it, just do

List<Car> prev = tableBean.getLazyCarDataModel().getPrev();
// ...

Upvotes: 1

Related Questions