I have a VF Page which displays the values from different object as a table. I need to perform the row based calculation in that table. Please find below the example and help me out in solving this.

   Rl R2 Total  Total(%)
A  25 25 50

B  50 75 125
  • row based summation ? Dec 16 '13 at 16:12
  • Please provide us with specify example explaining the scenario and expected output to help.Thanks Dec 16 '13 at 16:13
  • 1
    Yes it is row based summation. Actually we have a upload file for the previous year and current year of product sale information. We are storing the previous year and current year information in different objects. And using vf page we are displaying this information as a report. In that report we have total and percentage column for every row. We have to calculate the difference between the previous and current year. Please find below the report. We need to calculate the +/-LY %+/-LY. GOLDWELL TY LY +/-LY %+/-LY Topchic $2,000.00 $1,000.00 $1,000.00 $10.00
    – Udaya
    Dec 16 '13 at 16:25

One way i could think of implementing the same is

1)Create a wrapper class for your tables

2)Perform all calculations at the backend and use that to diaplay the data

public class wrappercalc {

public class mainwrap {
    public List < intwrap > Data {

public class intwrap {
    public integer row1 {
    public integer row2 {
    public integer sum {

    public intwrap(integer i1, integer i2) {
        row1 = i1;
        row2 = i2;
        sum = i1 + i2; //Logic in constructor

public List < intwrap > getData() {

    mainwrap m = new mainwrap();

    m.Data = new List < intwrap > ();

    intwrap i1 = new intwrap(25, 30);
    intwrap i2 = new intwrap(50, 100);


    return m.Data;

 <apex:page controller="wrappercalc">
   <apex:pageBlock >
     <apex:pageBlockTable value="{!Data}" var="d">
       <apex:column value="{!d.row1}" title="R1"/>
       <apex:column value="{!d.row2}" title="R2"/>
       <apex:column value="{!d.sum}" title="SUM"/>

The above code should give an idea. The basic wrapper class concept is needed for your example.


Another solution is to generate totals on the fly just using an apex:variable

Here using apex:variable to generate a total and then set value to the footer:

<apex:page controller="MyTest">
<apex:form >

<!-- Need this variables for totals generation -->
<apex:variable value="{!0.00}" var="total1"/>
<apex:variable value="{!0}" var="total2"/>

<apex:pageBlock title="Totals">
<apex:pageBlockTable value="{!accounts}" var="item" id="list1">

    <apex:column value="{!item.name}" width="20%"/>

    <apex:column headerValue="Number1">
        <apex:outputField value="{!item.number1__c}" />

        <!-- Generating a first total -->
        <apex:variable var="total1" value="{!total1 + item.number1__c}" />

        <!-- Footer of the first Cell -->>
        <apex:facet name="footer">
            Total: <span class="t1"></span>

    <apex:column headerValue="Number2">
        <apex:outputField value="{!item.number2__c}" />
        <apex:variable var="total2" value="{!total2 + item.number2__c}" />
        <apex:facet name="footer">
            Total: <span class="t2"></span>


    // Here we will set the generated subtotals to the footer cells
    document.getElementsByClassName('t1')[0].innerHTML = '{!total1}';
    document.getElementsByClassName('t2')[0].innerHTML = '{!ROUND(total2,0)}';


Here is the result:

enter image description here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.