+ Start a Discussion
Hugo StoupeHugo Stoupe 

Using a variable on a VF page to sum up values from a LIST of data and display on the page.

I am making a time entry report in a visualforce page that looks up records (hours in a week); based on a start / finish day and then displays the time in a grid format.

Is there a way that I can use a variable on the page to store the hours in each week that is written to the page and then at the bottom of the "Grid" show a total for each day.  I would then also want another variable "grandtotal" to display the total hours of all days.

Below is a screen shot of the page and the visual force code that follows.
User-added image
 
<apex:page controller="selectAllSOQLExampleController" tabStyle="Account" docType="HTML-5.0">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
               
                <b>Starting (yyyy-mm-dd): </b> <apex:inputText size="10" id="startDate" value="{!startDate}"/>&nbsp; &nbsp;
                <b>Ending (yyyy-mm-dd):</b>  <apex:inputText size="10" id="endDate" value="{!endDate}"/>&nbsp; &nbsp;
               
                <apex:commandButton value="Fetch Time Entries" action="{!fetch}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection columns="1" title="Dynamic SOQL Query" collapsible="True">
                <apex:outputText value="{!query}">
                </apex:outputText>
            </apex:pageBlockSection>
 
            <apex:pageBlockSection title="Billable Docket Report" columns="1" collapsible="False">Test
                <apex:pageBlockTable value="{!TEList}" var="tec">
                    <apex:column width="10" title="Week Starting" value="{!tec.Monday_Date__c}"/>
                    <apex:column width="30" value="{!tec.Functional_Area__c}"/>
                    <apex:column width="20" value="{!tec.Standard_Activities__c}"/>
                    <apex:column value="{!tec.Activity_Other__c}"/>
                    <apex:column width="10" value="{!tec.Project__c}"/>
                    <apex:column width="10" value="{!tec.Contract__c}"/>
                    <apex:column width="10" headerValue="Mon">
                        <apex:outputField value="{!tec.Monday__c}" Rendered="{!IF(MONTH(tec.Monday_Date__c) = MONTH(tec.Monday_Date__c), true, false)}"/>
                    </apex:column>
                    <apex:column width="10" headerValue="Tue">
                        <apex:outputField value="{!tec.Tuesday__c}" Rendered="{!IF(MONTH(tec.Tuesday_Date__c) = MONTH(tec.Monday_Date__c), true, false)}"/>
                    </apex:column>
                    <apex:column width="10" headerValue="Wed">
                        <apex:outputField value="{!tec.Wednesday__c}" Rendered="{!IF(MONTH(tec.Wednesday_Date__c) = MONTH(tec.Monday_Date__c), true, false)}"/>
                    </apex:column>
                    <apex:column width="10" headerValue="Thu">
                        <apex:outputField value="{!tec.Thursday__c}" Rendered="{!IF(MONTH(tec.Thursday_Date__c) = MONTH(tec.Monday_Date__c), true, false)}"/>
                    </apex:column>
                    <apex:column width="10" headerValue="Fri">
                        <apex:outputField value="{!tec.Friday__c}" Rendered="{!IF(MONTH(tec.Friday_Date__c) = MONTH(tec.Monday_Date__c), true, false)}"/>
                    </apex:column>
                    <apex:column width="10" headerValue="Sat">
                        <apex:outputField value="{!tec.Saturday__c}" Rendered="{!IF(MONTH(tec.Saturday_Date__c) = MONTH(tec.Monday_Date__c), true, false)}"/>
                    </apex:column>
                    <apex:column width="10" headerValue="Sun">
                        <apex:outputField value="{!tec.Sunday__c}" Rendered="{!IF(MONTH(tec.Sunday_Date__c) = MONTH(tec.Monday_Date__c), true, false)}"/>
                    </apex:column>
                    <apex:column width="10" value="{!tec.subtotal__c}"/>
                    
                </apex:pageBlockTable>
 
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

 
KevinPKevinP
In your visualforce controller for that page, create a List<double> to hold a map of days -> total hours.

Itterate over your hours object, and build the map up storing the sum of hours for that day in the value. each day is a list entry. 
Hugo StoupeHugo Stoupe
Hey Kevin,

My table has a field for each day Monday through Sunday.  Would I create a List for each day as my VF page I would like a sub-total at the bottom for each day.    You have helped me out a lot and I think you're familiar with my controller code by now ;)  Here it is again (I renamed it since the name was not very tellilng of the controller).  
 
public class fetchTimeCardEntryDataController {

    public String datename{get;set;}
    public List<Time_Card_Entry__c> TEList{get;set;}
    public String query{get;set;}
    public dummyTable__c dum{get;set;} 

    public fetchTimeCardEntryDataController() {
        dum=new dummyTable__c();
    }

    public PageReference fetch(){
    
        Date sDate = dum.dateOne__c;
        Date eDate = dum.dateTwo__c;
    
        String SobjectApiName = 'Time_Card_Entry__c';
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
 
        String commaSepratedFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(commaSepratedFields == null || commaSepratedFields == ''){
                commaSepratedFields = fieldName;
            }else{
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;
            }
        }
      query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Where Monday_Date__c >=:sDate AND Monday_Date__c <=:eDate  Limit 500';

     System.debug('query:'+query);

        TEList = Database.query(query);

        return null;
    }
}