简体   繁体   English

如何计算 p:dataTable Primefaces 中的行数?

[英]How to count the number of rows in p:dataTable Primefaces?

I am new to Primefaces.我是 Primefaces 的新手。

I am developing a project using Primefaces 5.1 and jsf 2.2.我正在使用 Primefaces 5.1 和 jsf 2.2 开发一个项目。

I do not know how to count the number of rows in p:dataTable ?我不知道如何计算p:dataTable的行数?

I need this for set Piechart based on rows count.我需要这个来根据行数设置 Piechart。

Any idea?任何的想法?

What yo have to do is to set a widgetVar to your p:datatable你需要做的是设置一个widgetVar到你的p:datatable

<p:dataTable value="#{tableBean.myTable}" widgetVar="myTable">...</p:dataTable>

then you can use it to easily access your table from Javascript然后您可以使用它从 Javascript 轻松访问您的表

To count the number of rows on your table计算表格中的行数

PF('myTable').tbody[0].children.length

or if you are using a paginator或者如果您使用的是分页器

PF('myTable').cfg.paginator.rowCount

If number of rows means, how many datasets are returned from the backing bean to my view, then you should count the number of rows in your backing bean by simply adding another getter like如果number of rows意味着从支持 bean 返回多少数据集到我的视图,那么您应该通过简单地添加另一个 getter 来计算支持 bean 中的行数

public int getNumberOfRows() {
  return this.myDataTableList.size();
}

If you mean by number of rows , how much data is currently visible within my p:datatable and you've used a paginator , then the answer is the same number you chose to initialize your datatable (except the last page maybe).如果您的意思number of rows当前在我的p:datatable可见多少数据并且您使用了paginator ,那么答案是您选择初始化数据表的相同数字(可能除了最后一页)。

If you want to access the number of rows on the client side, you have to use something like this:如果要访问客户端的行数,则必须使用以下内容:

$("#YOUR_DATATABLE_ID").find(".ui-datatable-tablewrapper").find(".ui-datatable-data").find(".ui-widget-content").length

But this will only display the rows currently visible (if you are using a paginator).但这只会显示当前可见的行(如果您使用的是分页器)。 If you want to get all rows and to make it much easier for you, make the number of rows accessible in your backing bean as seen above.如果您想获取所有行并让您更轻松,请在您的支持 bean 中设置可访问的行数,如上所示。

To get the number of rows for a datatable, simply use the "size()" built in Java method要获取数据表的行数,只需使用 Java 中内置的“size()”方法

<p:dataTable value="#{tableBean.myTable}">...</p:dataTable>

<h:outputText value="#{tableBean.myTable.size()}"/>

In case anyone still needs this feature just for display purposes .万一有人仍然需要此功能仅用于显示目的 From Version 5 upwards you have the so called {CurrentPageReport} element in the paginator template.从版本 5 起,您在分页器模板中拥有所谓的 {CurrentPageReport} 元素。 It allows customization via.它允许通过自定义。 Search for "currentPageReportTemplate" in the Primefaces documentation, it is kinda hidden :) Primefaces 5.1 Docu在 Primefaces 文档中搜索“currentPageReportTemplate”,它有点隐藏 :) Primefaces 5.1 Docu

With {totalRecords} you can output the number of records in the table.使用 {totalRecords} 您可以输出表中的记录数。 It also works with active filters.它也适用于有源滤波器。 Here is an example:下面是一个例子:

<p:dataTable var="entry" value="#{mbvHandler.entries}"
      widgetVar="carsTable" id="carsTable"
      scrollable="true" scrollWidth="100%"
      filteredValue="#{mbvHandler.filteredEntries}"
      rows="25" paginator="true" resizableColumns="true"
      paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown} {Exporters}"
      currentPageReportTemplate="{currentPage} of {totalPages} ({totalRecords})"
      rowsPerPageTemplate="10,15,25,50">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM