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
WYamWYam 

SUM on dataTables

Hi there and thank you for taking the time to read this post.

 

We are using the standard <apex:dataTable> to create a list of records and display them in columns.

 

Three of the columns are currency fields and we're wondering if there's a way to put in the sums of those columns a the bottom of the table similiar the the screenshot:

 

 

 

Any help you can provide is much appreciated.

 

 

Cheers,

Will

TehNrdTehNrd

Here is one way you could go about doing this:

 

Page

 

<apex:page controller="TableSum">
    <apex:pageBlock >
        
        <apex:pageBlockTable value="{!opps}" var="o">
            <apex:column value="{!o.Name}"/>
            <apex:column >
                <apex:outputField value="{!o.Amount}"/>
                <apex:facet name="footer">
                    <apex:outputField value="{!oppTotal.Amount}"/>
                </apex:facet>
            </apex:column>
        </apex:pageBlockTable>
        
    </apex:pageBlock>
</apex:page>

 Controller

 

public with sharing class TableSum {

    List<Opportunity> opps;
    Opportunity oppTotal;
    
    public List<Opportunity> getOpps(){
        if(opps == null){
            opps = [select Id, Name, Amount from Opportunity limit 10];
        }
        return opps;
    }

    public Opportunity getOppTotal(){
        oppTotal = new Opportunity(Amount = 0);
        
        for(Opportunity o : getOpps()){
            oppTotal.Amount += o.Amount;    
        }
        return oppTotal;  
    }

}

 

-Jason