I have created a VF tab in which i have retrieved the data with a SOQL statement and displayed the same with VF page using a datatable to make it as a customized report. I can't find a proper solution for the Grandtotal of each and every column for data retrieved and displayed in the VF tab.Kindly help me with this providing a solution or a sample code if any.

Required Output :

      ColA   ColB
       1       3
       5       5
       5       6
       7       8
 Grand -----------
 Total 18      22    

If you want a quick&dirty solution you can generate totals on the fly just using an apex:variable (note that if you need the more complex calculations of the totals it is better to use apex for that):

Apex class. Here just reading a data:

public with sharing class MyTest {

    public List<Account> accounts { get; set; }

    public MyTest(){
        accounts = [select id, name, number1__c, number2__c 
                    from account 
                    where number1__c != null and 
                    number2__c != null];

Page. Here using apex:variable to generate a total and then using some simple javascript to set generated 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

  • Thank you so much Mr.mast0r for your instantaneous help.
    – Mr.Blend
    Nov 12 '13 at 14:42
  • mastOr - what do you make of the line in the VF doc under apex:variable that states Note:<apex:variable> does not support reassignment inside of an iteration component, such as <apex:dataTable> or <apex:repeat>. The result of doing so, e.g., incrementing the <apex:variable> as a counter, is unsupported and undefined ?
    – cropredy
    Jan 26 '15 at 23:56
  • I know, but it works very well for years! Just undocumented feature I think :) Jan 27 '15 at 7:04

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.