0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Farhan stands with Palestine
  • 13,890
  • 13
  • 58
  • 105
  • Is it about plain JSF? It is provided by one of its component libraries PrimeFaces out of the box, [``](http://www.primefaces.org/showcase/ui/data/datascroller/basic.xhtml). It can also, if necessary, be loaded lazily. – Tiny Apr 26 '15 at 04:30
  • The datascroller IS a component and does not exist in plain jsf and if you do not want to use a component library, you have to develop you own component with e.g. plain html, a jquery-ui compojent and integrate that with the jsf lifecycle. In the end ending up with a component 'library' that you have to maintain. – Kukeltje Apr 26 '15 at 10:54
  • 2
    You want to clarify *which* datascroller you're working with. Richfaces and Primefaces provide similarly-named components – kolossus Apr 26 '15 at 16:16
  • @kolossus: primefaces dataScroller component. – Farhan stands with Palestine Apr 29 '15 at 04:47
  • The scroller component is backed by `org.primefaces.component.datascroller.DataScroller`, so you'll start by binding your datascroller element to an instance in the backing bean. A number of the things that you're looking for are not available out of the box, so you'll need to get creative – kolossus May 01 '15 at 17:49

1 Answers1

0

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)

Mindwin Remember Monica
  • 1,469
  • 2
  • 20
  • 35