title: Dynamic Calculation Using Filter in Datatable
body: <p>I want to dynamically update the sum of dataSet values as I filter and/or paginate a Primefaces dataTable.</p>
  
  <p>The following jQuery example shows <strong>exactly</strong> the behavior I want: <a href="http://datatables.net/examples/advanced_init/footer_callback.html" rel="nofollow">http://datatables.net/examples/advanced_init/footer_callback.html</a></p>
  
  <p>See below my main code for this issue:</p>
  
  <p>The jsf</p>
  
  <pre><code>&lt;p:columnGroup type="footer"&gt;
    &lt;p:row&gt;
      &lt;p:column colspan="6" footerText="Grand Total:" style="text-align:right"/&gt;
        &lt;p:column colspan="7"&gt;
          &lt;f:facet name="footer"&gt;
            &lt;h:outputText value="#{purchaseOrderController.soma}"&gt;
              &lt;f:convertNumber pattern="###,###" locale="pl_PL"/&gt;
            &lt;/h:outputText&gt;
          &lt;/f:facet&gt;
      &lt;/p:column&gt;
    &lt;/p:row&gt;
  &lt;/p:columnGroup&gt;
  </code></pre>
  
  <p>The method soma:</p>
  
  <pre><code>private float soma;
  
  public float getSoma() {
      for (PurchaseOrder p : getItems()) {
          soma += p.getShippingCost().floatValue();
      }
      return soma;
  }
  </code></pre>
  
  <p>Can someone help me?</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Updated code</strong></p>
  
  <p>The bean:</p>
  
  <pre><code>@Named(value = "purchaseOrderController")
  @ViewScoped
  public class PurchaseOrderController extends AbstractController&lt;PurchaseOrder&gt; implements Serializable {
  
  private float soma = 0;
  private float somaFiltered = 0;
  private List&lt;PurchaseOrder&gt; filteredPurchaseOrder;
  
  public List&lt;PurchaseOrder&gt; getFilteredPurchaseOrder() {
      return filteredPurchaseOrder;
  }
  
  public void setFilteredPurchaseOrder(List&lt;PurchaseOrder&gt; 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);
  }
  
  }
  </code></pre>
  
  <p>The jsf (some columns ommited):</p>
  
  <pre><code>&lt;h:form id="PurchaseOrderListForm"&gt;
    &lt;p:panel header="#{appBundle.ListPurchaseOrderTitle}"&gt;
       &lt;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"
                    &gt;
  
           &lt;p:ajax event="rowSelect"   update="createButton viewButton editButton deleteButton"/&gt;
           &lt;p:ajax event="rowUnselect" update="createButton viewButton editButton deleteButton"/&gt;
           &lt;p:ajax event="filter" listener="#{purchaseOrderController.filteredPurchaseOrder}" update="partialSoma" /&gt;
  
           &lt;p:column sortBy="#{item.quantity}" filterBy="#{item.quantity}"&gt;
               &lt;f:facet name="header"&gt;
                   &lt;h:outputText value="#{appBundle.ListPurchaseOrderTitle_quantity}"/&gt;
               &lt;/f:facet&gt;
               &lt;h:outputText value="#{item.quantity}"/&gt;
           &lt;/p:column&gt;
           &lt;p:column sortBy="#{item.shippingCost}" filterBy="#{item.shippingCost}"&gt;
               &lt;f:facet name="header"&gt;
                   &lt;h:outputText value="#{appBundle.ListPurchaseOrderTitle_shippingCost}"/&gt;
               &lt;/f:facet&gt;
               &lt;h:outputText value="#{item.shippingCost}"/&gt;
           &lt;/p:column&gt;
           &lt;p:columnGroup type="footer"&gt;
               &lt;p:row&gt;
                   &lt;p:column colspan="6" footerText="Grand Total:" style="text-align:right"/&gt;
                      &lt;p:column colspan="7"&gt;
                          &lt;f:facet name="footer"&gt;
                              &lt;h:outputText id="partialSoma" value="#{purchaseOrderController.somaFiltered}"&gt;
                              &lt;/h:outputText&gt;
                          &lt;/f:facet&gt;
                      &lt;/p:column&gt;
                  &lt;/p:row&gt;
              &lt;/p:columnGroup&gt;
          &lt;/p:dataTable&gt;
      &lt;/p:panel&gt;
  &lt;/h:form&gt;
  </code></pre>