简体   繁体   English

数据表中使用过滤器的动态计算

[英]Dynamic Calculation Using Filter in Datatable

I want to dynamically update the sum of dataSet values as I filter and/or paginate a Primefaces dataTable. 我希望在过滤和/或分页Primefaces dataTable时动态更新dataSet值的总和。

The following jQuery example shows exactly the behavior I want: http://datatables.net/examples/advanced_init/footer_callback.html 下面的jQuery的例子显示了正是我想要的行为: http://datatables.net/examples/advanced_init/footer_callback.html

See below my main code for this issue: 请参阅下面我的主要代码:

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

The method soma: 方法soma:

private float soma;

public float getSoma() {
    for (PurchaseOrder p : getItems()) {
        soma += p.getShippingCost().floatValue();
    }
    return soma;
}

Can someone help me? 有人能帮我吗?

Thanks in advance. 提前致谢。

Updated code 更新的代码

The bean: 豆子:

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

}

The jsf (some columns ommited): 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>

sorry for my lateness but I actually not have a lot of time with my work. 对不起我的迟到,但我的工作实际上没有太多时间。 I took the time yesterday afternoon to built a little working example. 我昨天下午花时间建立了一个小小的工作实例。 Indeed, I faced some problems for updating a footer with AJAX (cfr : http://forum.primefaces.org/viewtopic.php?f=3&t=15658 ) but it is working with a <p:remoteCommand /> workaround. 实际上,我在使用AJAX更新页脚时遇到了一些问题(cfr: http//forum.primefaces.org/viewtopic.php <p:remoteCommand /> ),但它正在使用<p:remoteCommand />解决方法。

You can find the Eclipse sample project here , running on JBoss 7.1.1 with JSF Mojarra 2.2.0m12 and JDK 1.7 你可以在这里找到Eclipse示例项目,在JBoss 7.1.1上使用JSF Mojarra 2.2.0m12和JDK 1.7运行

I think that you can define AJAX calls on PF datatable filtering and/or paging. 我认为您可以在PF数据表过滤和/或分页上定义AJAX调用。

<p:dataTable value="#{myBackingBean.values}" var="row" filteredValue="#{myBackingBean.filteredValues}" >
   <p:ajax event="filter" listener="#{myBackingBean.updateSum}" update="componentToUpdate" />
   ...
</p:dataTable>

In your updateSum() method, you can walk through the filteredValues collection to do your business. updateSum()方法中,您可以浏览filteredValues集合来开展业务。

You may have some difficulties to target the update component enclosed in the datatable but this can be resolved by adding something like styleClass="footerToUpdate" to your and using it in the update with update="@(.footerToUpdate)" 您可能在使用数据表中包含的更新组件时遇到一些困难,但可以通过向您添加类似styleClass =“footerToUpdate”的内容并使用update =“@(。footerToUpdate)”在更新中使用它来解决此问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM