使用viewscoped bean时的数据表分页

时间:2012-10-06 12:23:30

标签: jsf jsf-2 datatable pagination

我一直在我的应用程序中使用JSF 1.2。我的大多数应用程序页面都包含h:datatable。我偶然发现了this精彩的文章,解释了有关数据表的所有内容。如上文所示,我通过将我的表绑定到HtmlDataTable并使用会话范围的bean来实现数据表分页。

现在我转向JSF 2.0版本。我希望将sessionscoped bean转换为viewscoped,因为我的大多数应用程序页面彼此独立。

我遇到了this文章,其中解释了Viewscoped bean。它告诉我们不能使用数据表的binding属性。它还使用Datamodel

我现在对如何使用Datamodelviewscoped bean实现数据表分页感到震惊。

我有以下分页方法

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 "";
    }

在视图中我正在使用它们

<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}" />

请帮忙。

2 个答案:

答案 0 :(得分:1)

dataTable.setFirst(...)dataTable.getRows()替换为绑定为

private int firstprivate int rows属性
<h:dataTable ... first="#{bean.first}" rows="#{bean.rows}">

答案 1 :(得分:0)

我认为,你应该使用primefaces datable他们有一个很好的开箱即用数据表,包括lazy loading或者你可以使用richfaces或任何其他组件套件。 当然你可以建立你的onw jsf分页,但我认为你会花很多时间来构建一些完整的东西。