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
John NeffJohn Neff 

How do I create Column summaries for a PageBlockTable?

Hello, 

I have a pageBlockTable that returns a list of records, but how can I sum the column values at the bottom?  Do I need to create an extension to my controller?  Or is there a way I can sum the values "on the fly"?

Here is my table: 
 
<apex:pageBlockTable value="{!listOfCO}" var="co">
            <apex:column value="{!co.Buyer_Name__c}"/>
            <apex:column value="{!co.Total_Order__c}"/>
            <apex:column value="{!co.Balance_to_Collect__c}"/>
            </apex:pageBlockTable>

I would really appreciate any help!

Thanks, 

John

 
Best Answer chosen by John Neff
kevin lamkevin lam
I'm not sure how you would like to show the total so I have just added a line below the table:

<apex:variable var="totalBalance" value="{!0}"/>
<apex:pageBlockTable value="{!listOfCO}" var="co">
   <apex:column value="{!co.Buyer_Name__c}"/>
   <apex:column value="{!co.Total_Order__c}"/>
   <apex:column headerValue="Balance to Collect">
      <apex:outputField value="{!co.Balance_to_Collect__c}"/>
      <apex:variable var="totalBalance" value="{!totalBalance+co.Balance_to_Collect__c}"/>
   </apex:column/>
</apex:pageBlockTable>
<apex:outputText value="{0, number, 0.00}">
   <apex:param value="{!totalBalance}" />
</apex:outputText>

All Answers

kevin lamkevin lam
If you don't want to do that in an extension, try the apex:variable component like this:

<apex:variable var="totalBalance" value="{!0}"/>
<apex:pageBlockTable value="{!listOfCO}" var="co">
   <apex:column value="{!co.Buyer_Name__c}"/>
   <apex:column value="{!co.Total_Order__c}"/>
   <apex:column value="{!co.Balance_to_Collect__c}">
      <apex:variable var="totalBalance" value="{!totalBalance+co.Balance_to_Collect__c}"/>
   </apex:column/>
</apex:pageBlockTable>
John NeffJohn Neff
Thank you so much for your response Kevin - however I am getting the error: 
 
Error: You may specify either a ''value'' attribute or a body for the column component, but not both

I am not quite sure which component to remove.
kevin lamkevin lam
I tried this and it compiled without errors:

<apex:variable var="totalBalance" value="{!0}"/>
<apex:pageBlockTable value="{!listOfCO}" var="co">
   <apex:column value="{!co.Buyer_Name__c}"/>
   <apex:column value="{!co.Total_Order__c}"/>
   <apex:column headerValue="Balance to Collect">
      <apex:outputField value="{!co.Balance_to_Collect__c}"/>
      <apex:variable var="totalBalance" value="{!totalBalance+co.Balance_to_Collect__c}"/>
   </apex:column/>
</apex:pageBlockTable>
 
John NeffJohn Neff
Thanks Kevin - I was able to get it to compile without error, but the "total" line does not show on my page. Is there something I have to do to make the output value appear? Thanks, John
kevin lamkevin lam
I'm not sure how you would like to show the total so I have just added a line below the table:

<apex:variable var="totalBalance" value="{!0}"/>
<apex:pageBlockTable value="{!listOfCO}" var="co">
   <apex:column value="{!co.Buyer_Name__c}"/>
   <apex:column value="{!co.Total_Order__c}"/>
   <apex:column headerValue="Balance to Collect">
      <apex:outputField value="{!co.Balance_to_Collect__c}"/>
      <apex:variable var="totalBalance" value="{!totalBalance+co.Balance_to_Collect__c}"/>
   </apex:column/>
</apex:pageBlockTable>
<apex:outputText value="{0, number, 0.00}">
   <apex:param value="{!totalBalance}" />
</apex:outputText>
This was selected as the best answer
John NeffJohn Neff
This is perfect, thank you Kevin!!