PrimeFaces中的Sum Column Datatable

时间:2013-05-29 12:31:14

标签: jsf primefaces datatable

我通过ListDataModel在JSF页面中接收数据 我需要汇总列的所有值 遵循JSF代码:

<p:dataTable id="table" value="#{ProductBean.listProduct}" var="item" style="font-size:"12px" >
<p:column >
   <f:facet name="header">
      <h:outputText value="NAME" />
   </f:facet >
   <h:outputText value="#{item[0].name}" />
</p:column >
<p:column >
   <f:facet name="header">
      <h:outputText value="VALUE BUY" />
   </f:facet >
   <h:outputText value="#{item[1].valuebuy}" />
</p:column >
<p:column >
   <f:facet name="header">
      <h:outputText value="VALUE SELL" />
   </f:facet >
   <h:outputText value="#{item[2].valuesell}" />
</p:column >
<p:columnGroup type="footer">  
    <p:row>                                        
       <p:column footerText="Total" />  
       <p:column footerText="TotalBuy" />   <<<<--------- Sum All Values of item[1].valuebuy and show here
       <p:column footerText="TotalSell" />  <<<<--------- Sum All Values of item[2].valuesell and show here                  
    </p:row>  
</p:columnGroup>
</p:dataTable >

1 个答案:

答案 0 :(得分:6)

只需向<f:facet name="footer">添加一个包含必要信息的<p:column>,然后调用bean方法即可获取它。

基本示例:

观点:

<p:dataTable id="table" value="#{productBean.products}" var="product">
    <p:column>
        <f:facet name="header">
            <h:outputText value="NAME" />
        </f:facet>
        <h:outputText value="#{product.name}" />
        <f:facet name="footer">
            <h:outputText value="Total" />
        </f:facet>
    </p:column>
    <p:column>
        <f:facet name="header">
            <h:outputText value="VALUE BUY" />
        </f:facet>
        <h:outputText value="#{product.valueBuy}" />
        <f:facet name="footer">
            <h:outputText value="#{productBean.valueBuyTotal}" />
        </f:facet>
    </p:column>
    <p:column>
        <f:facet name="header">
            <h:outputText value="VALUE SELL" />
        </f:facet>
        <h:outputText value="#{product.valueSell}" />
        <f:facet name="footer">
            <h:outputText value="#{productBean.valueSellTotal}" />
        </f:facet>
    </p:column>
</p:dataTable>

豆子:

public class ProductBean {

    private List<Product> products;

    @PostConstruct
    public void init() {
        //load products in some way, like products = service.list();
    }

    public int getValueBuyTotal() {
        int quantity = 0;
        for(Product p : products) {
            quantity += p.getValueBuy();
        }
        return quantity;
    }

    public int getValueSellTotal() {
        int quantity = 0;
        for(Product p : products) {
            quantity += p.getValueSell();
        }
        return quantity;
    }

}

型号:

public class Product {

    String name;
    int valueBuy;
    int valueSell;

}