2

我通过 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 >

4

1 回答 1

6

Just add a <f:facet name="footer"> to <p:column> that will contain the necessary information and call a bean method to get it.

Basic example:

The view:

<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>

The bean:

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

}

The model:

public class Product {

    String name;
    int valueBuy;
    int valueSell;

}
于 2013-05-29T13:06:19.410 回答