Getting properties of <p:dataScroller> in backing bean

How can I get the following properties of a <p:dataScroller> in my backing bean?

  1. page index (on which page in a pagination I am on)
  2. page count
  3. page index
  4. first row index
  5. last row index

I tried component binding, but everything just returns null.

Upvotes: 0

Views: 923

Answers (1)

Mindwin Remember Monica
Mindwin Remember Monica

Reputation: 1539

the <p:dataScroller> has a ton of issues, but if you want these properties, you have to get them indirectly, because if you don't use the lazyDataModel, the dataScroller fetches all the data at once and goes away do its stuff, never calling back the managedBean.

Use a lazy load model. You will have to do a quick implementation of org.primefaces.model.LazyDataModel but it is just around 20 lines of code. Create a method on the managedBean that returns the subList requested; inside that managed bean you will fetch the values you want.

import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

class MsgLogLazyModel extends LazyDataModel<YourPojo> {
    private static final long serialVersionUID = 1L;
    private ManagedBean mb;

    public MsgLogLazyModel(ManagedBean mb) {
        this.mb = mb;
    }

    @Override
    public List<YourPojo> load(int first, int pageSize, String sortField,
            SortOrder sortOrder,
            Map<String, Object> filters) {
        return mb.loadItens(first, pageSize, sortOrder, filters);
    }   
}

YourPojo should be the object you are passing into the dataScroller for display.

Assuming the items you have to display are stored in a MB property

private List<YourPojo> myList;

Now on the mb.loadItens() you will capture:

  1. page index (on which page in a pagination I am on)

int pageIndex = first / pageSize;

  1. page count

// you have to round up this because the last page will have less than pageSize itens
int pageCount = (int) Math.ceil(myList.size() / new Double(pageSize));

  1. page index

You are repeating yourself.

  1. first row index

I will assume you want the first row of the chunk returned. That is the first parameter. Since the dataScroller does not have pagination...

  1. last row index

// maybe the list has ended. so we take the safe path...
int lastRow = Math.min(myList.size(), first + pageSize)

Upvotes: 0

Related Questions