我希望在过滤和/或分页Primefaces dataTable时动态更新dataSet值的总和。
以下jQuery示例显示完全我想要的行为:http://datatables.net/examples/advanced_init/footer_callback.html
请参阅下面我的主要代码:
jsf
<p:columnGroup type="footer">
<p:row>
<p:column colspan="6" footerText="Grand Total:" style="text-align:right"/>
<p:column colspan="7">
<f:facet name="footer">
<h:outputText value="#{purchaseOrderController.soma}">
<f:convertNumber pattern="###,###" locale="pl_PL"/>
</h:outputText>
</f:facet>
</p:column>
</p:row>
</p:columnGroup>
方法soma:
private float soma;
public float getSoma() {
for (PurchaseOrder p : getItems()) {
soma += p.getShippingCost().floatValue();
}
return soma;
}
有人可以帮助我吗?
提前致谢。
更新了代码
豆子:
@Named(value = "purchaseOrderController")
@ViewScoped
public class PurchaseOrderController extends AbstractController<PurchaseOrder> implements Serializable {
private float soma = 0;
private float somaFiltered = 0;
private List<PurchaseOrder> filteredPurchaseOrder;
public List<PurchaseOrder> getFilteredPurchaseOrder() {
return filteredPurchaseOrder;
}
public void setFilteredPurchaseOrder(List<PurchaseOrder> filteredPurchaseOrder) {
this.filteredPurchaseOrder = filteredPurchaseOrder;
}
//return the sum for the FILTERED list - using this method, get a NullPointerException (the filtered list is null)
public float getSomaFiltered() {
for (PurchaseOrder p : filteredPurchaseOrder) {
somaFiltered += p.getQuantity() * p.getShippingCost().floatValue();
}
return somaFiltered;
}
//return the sum for the ENTIRE list - using this method it's ok
public float getSoma() {
for (PurchaseOrder p : getItems()) {
soma += p.getQuantity() * p.getShippingCost().floatValue();
}
return soma;
}
@Inject
private PurchaseOrderFacade ejbFacade;
public PurchaseOrderController() {
super(PurchaseOrder.class);
}
}
jsf(某些列无名):
<h:form id="PurchaseOrderListForm">
<p:panel header="#{appBundle.ListPurchaseOrderTitle}">
<p:dataTable id="datalist" var="item" value="#{purchaseOrderController.items}"
filteredValue="#{purchaseOrderController.filteredPurchaseOrder}"
selectionMode="single" selection="#{purchaseOrderController.selected}"
rowKey="#{item.orderNum}"
paginator="true"
rows="10"
rowsPerPageTemplate="10,20,30"
>
<p:ajax event="rowSelect" update="createButton viewButton editButton deleteButton"/>
<p:ajax event="rowUnselect" update="createButton viewButton editButton deleteButton"/>
<p:ajax event="filter" listener="#{purchaseOrderController.filteredPurchaseOrder}" update="partialSoma" />
<p:column sortBy="#{item.quantity}" filterBy="#{item.quantity}">
<f:facet name="header">
<h:outputText value="#{appBundle.ListPurchaseOrderTitle_quantity}"/>
</f:facet>
<h:outputText value="#{item.quantity}"/>
</p:column>
<p:column sortBy="#{item.shippingCost}" filterBy="#{item.shippingCost}">
<f:facet name="header">
<h:outputText value="#{appBundle.ListPurchaseOrderTitle_shippingCost}"/>
</f:facet>
<h:outputText value="#{item.shippingCost}"/>
</p:column>
<p:columnGroup type="footer">
<p:row>
<p:column colspan="6" footerText="Grand Total:" style="text-align:right"/>
<p:column colspan="7">
<f:facet name="footer">
<h:outputText id="partialSoma" value="#{purchaseOrderController.somaFiltered}">
</h:outputText>
</f:facet>
</p:column>
</p:row>
</p:columnGroup>
</p:dataTable>
</p:panel>
</h:form>
答案 0 :(得分:2)
<p:remoteCommand />
解决方法。
您可以在JBoss 7.1.1上使用JSF Mojarra 2.2.0m12和JDK 1.7找到Eclipse示例项目here
答案 1 :(得分:1)
我认为您可以在PF数据表过滤和/或分页上定义AJAX调用。
<p:dataTable value="#{myBackingBean.values}" var="row" filteredValue="#{myBackingBean.filteredValues}" >
<p:ajax event="filter" listener="#{myBackingBean.updateSum}" update="componentToUpdate" />
...
</p:dataTable>
在 updateSum()方法中,您可以浏览 filteredValues 集合来开展业务。
您可能难以定位数据表中包含的更新组件,但可以通过向 styleClass =“footerToUpdate”添加一些内容并在更新中使用来解决此问题。更新= “@(footerToUpdate)”强>