Sreeram
Sreeram

Reputation: 3258

datatable pagination when using viewscoped bean

I have been using JSF 1.2 in my application. most of my application pages consists of h:datatable. I came across this wonderful article which explain everything about datatables. As shown in the above article, i implemented the datatable pagination by binding my table to HtmlDataTable and using a session scoped bean.

Now i am moving to JSF 2.0 version. I wanted to convert my sessionscoped beans to viewscoped as most of my application pages are independent from one another.

i came across this article which explains about the Viewscoped beans. It tells that we cannot use binding attribute of the datatable. and also it uses the Datamodel.

I am now struck on how to implement datatable pagination with the Datamodel and viewscoped bean.

I am having the following methods for pagination

public String pageFirst() {
        dataTable.setFirst(0);
        return "";
    }

    public String pagePrevious() {
        dataTable.setFirst(dataTable.getFirst() - dataTable.getRows());
        return "";
    }

    public String pageNext() {
        dataTable.setFirst(dataTable.getFirst() + dataTable.getRows());
        return "";
    }

    public String pageLast() {
        try {
        int count = dataTable.getRowCount();
        int rows = dataTable.getRows();
        LOGGER.info("rowcount:" + count);
        LOGGER.info("rows:" + rows);
        dataTable.setFirst(count - ((count % rows != 0) ? count % rows : rows));
        }catch(ArithmeticException e){
            LOGGER.info("no rows to display: ",e);
        }
        return "";
    }

And in the view i am using them like this

<h:commandButton value="#{msgs.footerbutton1}"
                 action="#{bean.pageFirst}"
                 disabled="#{bean.dataTable.first == 0}" />
<h:commandButton value="#{msgs.footerbutton2}"
             action="#{bean.pagePrevious}"
             disabled="#{bean.dataTable.first == 0}" />
<h:commandButton value="#{msgs.footerbutton3}"
             action="#{bean.pageNext}"
             disabled="#{bean.dataTable.first + bean.dataTable.rows
                             >= bean.dataTable.rowCount}" />
<h:commandButton value="#{msgs.footerbutton4}"
              action="#{bean.pageLast}"
              disabled="#{bean.dataTable.first + bean.dataTable.rows
                           >= bean.dataTable.rowCount}" />

Please help.

Upvotes: 0

Views: 4540

Answers (2)

BalusC
BalusC

Reputation: 1109002

Replace dataTable.setFirst(...) and dataTable.getRows() by private int first and private int rows properties which are bound as

<h:dataTable ... first="#{bean.first}" rows="#{bean.rows}">

Upvotes: 1

Guilherme Torres Castro
Guilherme Torres Castro

Reputation: 15350

I think, you should use primefaces datable they have a really nice out of box datatable with a lot of freatures including lazy loading Or you can use richfaces or anyother component suite. Of course you can build your onw jsf pagination, but i think you will spent a lot of time build something that are arelady done.

Upvotes: 0

Related Questions