9

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
3
  • row based summation ? Commented Dec 16, 2013 at 16:12
  • Please provide us with specify example explaining the scenario and expected output to help.Thanks Commented Dec 16, 2013 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
    Commented Dec 16, 2013 at 16:25

2 Answers 2

7

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 {
        get;
        set;
    }
}

public class intwrap {
    public integer row1 {
        get;
        set;
    }
    public integer row2 {
        get;
        set;
    }
    public integer sum {
        get;
        set;
    }

    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);

    m.Data.add(i1);
    m.Data.add(i2);

    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"/>
     </apex:pageBlockTable>
   </apex:pageBlock>
 </apex:page>

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

3

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:facet>
    </apex:column>

    <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>
        </apex:facet>
    </apex:column>

</apex:pageBlockTable>
</apex:pageBlock>

<script>
    // 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)}';
</script>

</apex:form>
</apex:page>

Here is the result:

enter image description here

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .