+ Start a Discussion
Hugo StoupeHugo Stoupe 

Grand total for a column in VisualForce

I have been trying various "examples" to create a sub-total for columns in a visualforce page and have had no luck.  The closest I have gotten is the one below, however, the <script> doesn't seem to be working as I don't get any value in the <span></span> block when I render the page.

Could anyone take a look to see if my code is missing anything ?  See the <apex:facet> at the bottom which references the place that should be populated with the total.  I have a variable that should be holding the total, but not sure how to check this.
 
<apex:page controller="fetchTimeCardEntryDataController" tabStyle="Account" docType="HTML-5.0">
    <apex:form >
        <!-- Variables to hold Totals -->
        <apex:variable value="{!0.00}" var="tMon"/>
        <apex:variable value="{!0.00}" var="Total_Tuesday"/>
        <apex:variable value="{!0.00}" var="Total_Wednesday"/>
        <apex:variable value="{!0.00}" var="Total_Thursday"/>
        <apex:variable value="{!0.00}" var="Total_Friday"/>
        <apex:variable value="{!0.00}" var="Total_Saturday"/>
        <apex:variable value="{!0.00}" var="Total_Sunday"/>
        <apex:variable value="{!0.00}" var="Total_Grand"/>
        
        <apex:pageBlock Title="Time Card Entry Report">
            <!--Start and End block along with fetch button -->
            <apex:pageBlockButtons >
                <b>Starting Date</b><apex:inputfield value="{!dum.dateOne__c}"/>
                <b>Ending Date</b><apex:inputfield value="{!dum.dateTwo__c}"/>
                <apex:commandButton value="Fetch Time Entries" action="{!fetch}"/>
            </apex:pageBlockButtons>
          
           <!-- Debug - SQL statement -->
        <!--   <apex:pageBlockSection columns="1" title="DEBUG - Dynamic SOQL Query" collapsible="True">
                <apex:outputText value="{!query}">
                </apex:outputText>
            </apex:pageBlockSection> -->
             
            <!-- Display Time Entry Date -->
          
          <apex:pageblocktable value="{!TEList}" var="tec1" id="list1">
              <apex:column width="10" title="Week Starting" value="{!tec1.Monday_Date__c}"/>
              <apex:column headerValue="Mon">
                  <apex:outputfield value="{!tec1.Monday__c}"/>
                  <apex:variable var="tMon" value="{!tMon+1+tec1.Monday__c}"/>
                      <apex:facet name="footer">
                        t<span ID="tMonday"></span>
                      </apex:facet>
               </apex:column>
          
          </apex:pageblocktable>
          
          
         </apex:pageBlock>
        
        <!-- Script for generating Totals-->
        <script>
            document.getElementsByID('tMonday')[0].innerHTML='{!tMon}';
        </script>
         
    </apex:form>
</apex:page>

 
Dushyant SonwarDushyant Sonwar

use <input type="hidden" value="{tec1.monday__c}" id="mon"> in apex:column

then use this script
function total(){
var sum=0;
var values =document.getElementsByTagName("input");
for(var i=0; i<values.length; i++){
if(values[i].id.indexOf('mon'))
sum=sum+parseInt(values[i]);

document.getElementById('id which you have given for last column') =sum;
}
Hugo StoupeHugo Stoupe
Hey Dushyant,   How and where do I then call the function total()?  I was getting an error on save:  Element type "input" must be followed by either attribute specifications, ">" or "/>"
Hugo StoupeHugo Stoupe
forget the error - that was a typo, but I am still not callin gthe total() function.  Any ideas?  This is what it looks like now:
 
<apex:pageblocktable value="{!TEList}" var="tec1" id="list1">
              <apex:column width="10" title="Week Starting" value="{!tec1.Monday_Date__c}"/>
              <apex:column width="10" title="mon" value="{!tTues}" />
              <apex:column headerValue="Mon">
                  <input type="hidden" value="{tec1.monday__c}" id="mon"/>
                  <apex:outputfield value="{!tec1.Monday__c}"/>
                  <apex:variable var="tTues" value="{!IF(tec1.Monday__c != null, tTues+1+tec1.Monday__c,"")}"/>
               <!--!IF(MONTH(tec.Monday_Date__c) = MONTH(tec.Monday_Date__c), true, false)}-->
                      <apex:facet name="footer">
                        t<span ID="tMonday"></span>
                      </apex:facet>
               </apex:column>
          
          </apex:pageblocktable>
          
          
         </apex:pageBlock>
        
        <!-- Script for generating Totals-->
        
        
        
        <script>
            function total(){
            var sum=0;
            var values =document.getElementsByTagName("input");
                for(var i=0; i<values.length; i++){
                    if(values[i].id.indexOf('mon'))
                        sum=sum+parseInt(values[i]);
                 } 
                     document.getElementById('itMonday') =sum;
            }

 
Dushyant SonwarDushyant Sonwar

<script>
function total(){
var sum=0;
var values =document.getElementsByTagName("input");
for(var i=0; i<values.length; i++)
{
if(values[i].id.indexOf('mon')) {
sum=sum+parseInt(values[i].value);
}
document.getElementById('itMonday') =sum;
}
}
total();
</script>
try this one hugo...

 
Hugo StoupeHugo Stoupe
Thank you Dushyant.  For whatever reason, my output   <span ID="tMonday"></span> is not giving me any totals...

Does anyone have a working sample (visualforce page) that does show the totals so I could use it to troubleshoot my code?

Hugo
Abhilash Gandhewar 5Abhilash Gandhewar 5
Because of some null value <span ID="tMonday"></span> is not giving any totals. Whenever you get null please change null to 0 (zero). You will get Output.