function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Swamy PSwamy P 

How to Show Grand Total by the dynamically added Rows in Page Block Table

Hello Everyone,

I wanted to show a Grand Total Value by adding all of the Total Values which is appearing in the below image. All these are ROWS are added dynamically.
User-added image

I've already implemented some of the code but it is not showing Correct/Exact values. Please provide your valuable solutions.
Best Answer chosen by Swamy P
Swamy PSwamy P
Hello Gaurav & Everyone,

This issue has been resolved now. Below is the code which I've created for the total Calculation. I hope this may help others!!!!

function calculate_finalValue() {
    var total_Tcv = 0;
    var GranToral=0;
    $("[id$='kpt']").each(function(){       // kpt will be the ID of an Total Field.
        var values = parseInt($(this).val().trim());
        if(!isNaN(values)){
            GranToral+=values;
        }
    });
  $("[id$='grndt']").val(GranToral);       // grndt is an "Grand Total:" of my field.
}

<apex:column headerValue="Total">
          <apex:inputText value="{!e1.total}" id="kpt"  style="width:40px;" html-readonly="true" onchange="calculate_finalValue();"/>                                   
          <apex:facet name="footer">
              Grand Total:<apex:inputtext id="grndt" value="{!grandTotalVal}" style="width:40px;" html-readonly="true"/>
           </apex:facet>
 </apex:column>  

All Answers

sandeep reddy 37sandeep reddy 37
whats the wrong in this  send me code  error also please
 
GauravGargGauravGarg
Hi Swamy,

The Grand total can be dynamically calculated using Formula field. 

Please try using formula field adding all the values (api field name).

Hope this will help you. 

Thanks
Swamy PSwamy P
Hi Gaurav,

Can you mention what exactly we can add to get the result. This functionality is in VF Page.
GauravGargGauravGarg
Hi Swamy,

If you need display on VF page, we need to implement this logic via Javascript using ".each" function. I have tried the same in my org, will provide you the snapshot. Please check below and let me know if you face any issues. 
 
// Function to Calculate Final Value
function calculate_finalValue() {
                    var total_Tcv = 0;
                    $("span[id*='TCV']").each(function(key, value) { // Only apex:outputField with Name "TCV" will get calculated. 
                        var Tcv = value.innerHTML.replace(/,/g, "");
                        total_Tcv += parseInt(Tcv);
                    });
                    return addCommas((total_Tcv).toFixed(0));
                }

// Sample name for output field tag.
<apex:outputField id="finalTCV" value="Product Sector TCV(USD)"></apex:outputField >

Thanks,
Gaurav
Email: gauravgarg.nmims@gmail.com
Swamy PSwamy P
Hello Gaurav & Everyone,

This issue has been resolved now. Below is the code which I've created for the total Calculation. I hope this may help others!!!!

function calculate_finalValue() {
    var total_Tcv = 0;
    var GranToral=0;
    $("[id$='kpt']").each(function(){       // kpt will be the ID of an Total Field.
        var values = parseInt($(this).val().trim());
        if(!isNaN(values)){
            GranToral+=values;
        }
    });
  $("[id$='grndt']").val(GranToral);       // grndt is an "Grand Total:" of my field.
}

<apex:column headerValue="Total">
          <apex:inputText value="{!e1.total}" id="kpt"  style="width:40px;" html-readonly="true" onchange="calculate_finalValue();"/>                                   
          <apex:facet name="footer">
              Grand Total:<apex:inputtext id="grndt" value="{!grandTotalVal}" style="width:40px;" html-readonly="true"/>
           </apex:facet>
 </apex:column>  
This was selected as the best answer
Test Org 134Test Org 134

Swamy,

Can I request you to tell me what is the code of value="{!grandTotalVal}"??
I am stuck, I really;y need your help.