p:datatable摘要行计算

问题描述:

我有一个 p:dataTable 非常类似于 showcase
不幸的是,展示使用随机生成的值为 p:summaryRow

I have a p:dataTable pretty similar to the showcase. Unfortunatly, the showcase uses randomly generated values for the p:summaryRow.

参考展示,想像这辆汽车有一个价格属性。

Refering to the showcase, imagine the car had a price attribute.

我如何总结按照指定列分组的汽车的价格,以便在总结行中显示?

我有与您相同的问题,并从原点找到正确的用法blog
从原点论坛的SummaryRow

I have same problem with you and found correct usage from primefaces blog SummaryRow from primefaces forum

这是一些示例,显示总和价格的正确计算,计数...

this is some example show correct calculation for sum price, count...

监听器获取组属性,它是可用于计算总计的sortBy值,任何其他操作。

Listener gets the group property which is the sortBy value which you can use to calculate totals or any other operation.

<p:summaryRow listener="#{backingBean.calculateTotal}"> 
                        <p:column colspan="3" style="text-align:right"> 
                            Total: 
                        </p:column> 

                        <p:column> 
                            #{backingBean.total}
                        </p:column> 
                    </p:summaryRow>

/* listener method in your bean */
public void calculateTotal(Object o) {
    this.total = 0.0d;
    String name = "";
    if(o != null) {
        if(o instanceof String) {
            name = (String) o;
            for(MyRowObject p : (List<MyRowObject>) dataTableModel.getWrappedData()) { // The loop should find the sortBy value rows in all dataTable data.
                switch(sortColumnCase) { // sortColumnCase was set in the onSort event
                    case 0:
                        if(p.getcolumn0data().getName().equals(name)) {
                            this.total += p.getcolumn0data().getValue();
                        }
                        break;
                    case 1:
                        if(p.getcolumn1data().getName().equals(name)) {
                            this.total += p.getcolumn1data().getValue();
                        }
                        break;
                }
            }
        }
    }
}