0

I need your help in updating an outputText with the total Amount field for the selected checkboxes in a dataTable. The jsf has the below code:

    <p:dataTable id="PendingRequests" var="hr" selection="#{hrdirector.selectedRequests}"
             value="#{hrdirector.listPendingRequests}" rowKey="#{hr.requestNo}"
             filteredValue="#{hrdirector.filteredRequests}" widgetVar="dataTableWidgetVar">
        <p:column selectionMode="multiple" style="width:16px;text-align:center"></p:column>
        <p:column headerText="Request No.">
            <h:outputText value="#{hr.requestNo}"/>
        </p:column>
        <p:column headerText="Request Amount">
            <h:outputText value="#{hr.requestAmount}"/>
        </p:column>
    </p:dataTable>

    <h:outputText id="Sum" value="#{hr.Sum}"/>

The user is going to select a number of checkboxes and I need to know the appropriate way to call a method through ajax to update the outputText with the Total Requests Amounts selected.

The method to be called is:

   public void ShowTotal() {


       try {

        String [] tranAmountArr = new String[selectedRequests.size()];

        for (int i = 0; i < selectedRequests.size(); i++) {

            tranAmountArr[i] = selectedRequests.get(i).getEncashmentAmount();

            Sum = Sum + Double.parseDouble(tranAmountArr[i]);

        }

        System.out.println(Sum);


    } catch (Exception e) {

        System.err.print(e);

        e.printStackTrace();

        log.error("Error in ShowTotal()");

    }


}
99maas
  • 1,239
  • 12
  • 34
  • 59
  • 1
    http://stackoverflow.com/q/20456143/1391249 – Tiny Sep 29 '15 at 17:12
  • @BalusC is a very elegant solution if you only need to output the sum. If you later want to do any validation or additional operation you will have to add server side listeners. – Javier Haro Sep 29 '15 at 17:40

1 Answers1

0

Just add two Ajax tags inside your table:

<p:ajax event="rowSelect" listener="#{hrdirector.showTotal}" 
process="@this" update="sum" /> 

And:

<p:ajax event="rowUnselect" listener="#{hrdirector.showTotal}" 
process="@this" update="sum" /> 

Note: For method names, paremeters, attributtes and IDs use lowercase.

Note: I would name the method "updateTotal" instead "ShowTotal".

Javier Haro
  • 1,255
  • 9
  • 14
  • Thanks it worked. But instead of rowSelect/rowUnselect, I should use rowSelectCheckbox and rowUnselectCheckbox – 99maas Sep 30 '15 at 12:50