1
我通過ListDataModel接收JSF頁面中的數據。
我需要總和列的所有值。
沿用了JSF代碼:PrimeFaces中的Sum Column Datatable
<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 >
而不是使用假的getter方法,最好將總和保存在託管bean的字段中。 –
@LuiggiMendoza正確,但是如果OP'修改'產品的值,他需要在setter方法中更新適當的總和,以便它始終保持一致。由於它在表面層面不可見,這種方法基本上是「動態安全」的。 – skuntsel
您每次修改列表時都可以調用該方法計算總和:)。 –