【问题标题】:Reading correct number of rows to display for a primefaces datatable读取要显示的primefaces数据表的正确行数
【发布时间】:2015-12-08 11:37:55
【问题描述】:

我有一个这样的 primafaces 数据表

 <pf:dataTable id="#{controller.tableComponentId}" 
     rows="#{controller.rowsPerPage}"
     rowsPerPageTemplate="#{controller.rowsPerPageTemplate}"

    <pf:ajax event="page" listener="#{controller.onPageChange}"/>
  />

我的问题是当用户更改要显示的行数时。 page 事件被触发,但使用旧的 rows 值。因此,如果行的初始值为 10,那么用户将其更改为 25。我仍然读取值 10,然后 JSF 调用 rowsPerPage 设置器。

我知道这个线程PrimeFaces dataTable: how to catch rows-per-page event? 基本上是同样的问题。我尝试了这里提到的解决方案,但它对我不起作用。我也使用分页,但为了简单起见,我没有把它放在我的代码中。

我也尝试使用 process="@form" 并读取请求参数映射,但是在 ajax 请求中没有发送“dt_rppDD”值。任何其他建议如何做到这一点?

【问题讨论】:

  • rowsPerPage 设置器在 RENDER_RESPONSE 上触发,onPageChange(PageEvent event) 被触发。如果你需要新的行值,你可以得到它Faces.getRequestParameter(((UIComponent)event.getSource()).getClientId().concat("_rows"))

标签: jsf primefaces datatable


【解决方案1】:

在页面事件监听器中获取行

页面:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
        PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui">

<h:head>
    <title>Datatable Page</title>
</h:head>

<h:body>
    <h:form>
        <p:dataTable id="#{datatablePage.tableComponentId}" rows="#{datatablePage.rows}" paginator="true"
                     rowsPerPageTemplate="#{datatablePage.rowsPerPageTemplate}" value="#{datatablePage.list}" var="row">
            <p:ajax event="page" listener="#{datatablePage.onPageChange}"/>
            <p:column>
                <h:outputText value="#{row}"/>
            </p:column>
        </p:dataTable>
    </h:form>
</h:body>
</html>

支持 bean:

package org.antonu17;

import org.omnifaces.cdi.ViewScoped;
import org.omnifaces.util.Faces;
import org.primefaces.event.data.PageEvent;

import javax.faces.component.UIComponent;
import javax.inject.Named;
import java.io.Serializable;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Named("datatablePage")
@ViewScoped
public class DatatablePage implements Serializable {
    private String tableComponentId = "table";
    private Integer rows = 20;
    private String rowsPerPageTemplate = "20,30,40,50";

    private List<Integer> list = IntStream.range(1,200).boxed().collect(Collectors.toList());

    public void onPageChange(PageEvent event) {
        System.out.println(Faces.getRequestParameter(((UIComponent)event.getSource()).getClientId().concat("_rows")));
    }

    public String getTableComponentId() {
        return tableComponentId;
    }

    public void setTableComponentId(String tableComponentId) {
        this.tableComponentId = tableComponentId;
    }

    public Integer getRows() {
        return rows;
    }

    public void setRows(Integer rows) {
        this.rows = rows;
    }

    public String getRowsPerPageTemplate() {
        return rowsPerPageTemplate;
    }

    public void setRowsPerPageTemplate(String rowsPerPageTemplate) {
        this.rowsPerPageTemplate = rowsPerPageTemplate;
    }

    public List<Integer> getList() {
        return list;
    }
}

【讨论】:

  • 我接受您的回答,因为它是原始问题的解决方案。谢谢
【解决方案2】:

我找到了解决问题的方法,但这与我提出的问题无关。当我使用我自己的实现 PaginatorElementRenderer 时,JSF 没有使用我的类,而是使用默认类。

此链接中的问题是解释我的问题和解决方案。

How to customize Pagination in Primefaces Data Table

由于我使用的是比 5.1 更旧的版本,我不得不使用反射来访问地图并设置我自己的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-18
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2015-12-23
    • 1970-01-01
    • 2015-07-25
    • 2018-07-03
    相关资源
    最近更新 更多