title: Primefaces Datatable dynamic calculation
body: <p>In my view layer, I use a Primefaces Datatable with 9 columns, which the last one has float values (column name= 'Valor').</p>
  
  <p>There is an integer column named 'prefDep' which has a filter for its values (foreignKey values, by the way):</p>
  
  <pre><code>&lt;!-- column name omitted --&gt;   
  &lt;p:column sortBy="#{item.prefDep}" filterBy="#{item.prefDep}"&gt;
    &lt;h:outputText value="#{item.prefDep}"/&gt;
  &lt;/p:column&gt;
  </code></pre>
  
  <p>The last row of the Datatable has a <strong>fixed</strong> GrandTotal, which is the overall sum of the mentioned 'Valor' column:</p>
  
  <pre><code>&lt;p:columnGroup type="footer"&gt;
    &lt;p:row&gt;
      &lt;p:column colspan="8" footerText="Grand Total:" /&gt;
      &lt;p:column footerText="#{upbController.totalLosses}" /&gt;
    &lt;/p:row&gt;
  &lt;/p:columnGroup&gt;
  </code></pre>
  
  <p>Method in the controller class to get the overall sum:</p>
  
  <pre><code>public int getTotalLosses() {  
    int total = 0;  
      for(Upb id : getItems()) {  
        total += id.getValor();
      }  
       return total;  
  }  
  </code></pre>
  
  <p>What I want: as I filter the Datatable, I want to show the corresponding GrandTotal for that filtered portion of values only, and not the 'fixed' overall sum.</p>
  
  <p>How can I do it?</p>
  
  <p>Thanks in advance.</p>
  
  <p>--</p>
  
  <p>After section:</p>
  
  <p>In my AbstractController I have:</p>
  
  <pre><code>private List&lt;T&gt; filteredUpb;
  //...
  public List&lt;T&gt; getFilteredUpb() {
      if (filteredUpb == null) {
          filteredUpb = this.ejbFacade.findAll();
      }
      return filteredUpb;
  }
  </code></pre>
  
  <p>In my UpbController (managedBean):</p>
  
  <pre><code>private List&lt;Upb&gt; filteredUpb = null;
  //...
  public int getPerdasTotal() {
      int total = 0;
      for (Upb id : getFilteredUpb()) {
          total += id.getVlOco();
      }
      return total;
  }
  </code></pre>
  
  <p>In jsf:</p>
  
  <pre><code>&lt;p:column&gt;
    &lt;f:facet name="footer"&gt;
      &lt;h:outputText value="#{upbController.perdasTotal}" /&gt;
    &lt;/f:facet&gt;
  &lt;/p:column&gt;
  </code></pre>