+ Start a Discussion
acrozieracrozier 

Adding cell values for a sum

Hello,

 

I need to do some calculations on cells in a datatable to generate sums, for instance, I have a QTY field and a Honoraria field.  I need to mutiply the value of the Honoraria field by the QTY field to give me my Total.  How can I do this in either Javascript or Apex?  Code examples below,

 

 

DataTable

<apex:dataTable id="incentives" cellpadding="4px" width="100%" frame="none" border="1" Value="{!Incents}" Var="I">
                <apex:column width="20%">
                    <apex:outputField value="{!I.QTY__c}"/>
                </apex:column>
                <apex:column width="60%">
                    <apex:outputField value="{!I.Description__c}"/>
                </apex:column>
                <apex:column width="20%">
                    <apex:outputField value="{!I.Honoraria__c}"/>
                </apex:column>
            </apex:dataTable>

 

Extension

 

public with sharing class Incent {
public list<AIIncentive__c> Incents {get; set;}
public list<job__c> job {get; set;}
public id jobid;
public string NewIncentive {get; set;}
public PageReference refresh = ApexPages.currentPage(); 

    public Incent(ApexPages.StandardController controller) {
    	
     	jobid = apexpages.currentpage().getParameters().get('id');
        system.debug('################################## Id'+jobid);

        Incents = [Select QTY__c, Description__c,  Honoraria__c, Job__c, Deleted__c  From AIIncentive__c  where Job__c = :jobid]; 
        
        job = [SELECT Account__c FROM Job__c WHERE id= :jobid];    
        
        }


    public PageReference NewIncentive(){
    	
    	AIIncentive__c obj = new AIIncentive__c();

          	obj.QTY__c = 0;
     		obj.Description__c = '';
     		obj.Honoraria__c = 0;
     		obj.Job__c = jobid;

	 	update Incents;
	 		 
     	insert obj;
     
        	refresh.setRedirect(true);
     
   	 	return refresh;
     
    }
    
    public PageReference save() {
        	        	
    	update Incents;
        
	        refresh.setRedirect(true);        

        return refresh;

    }
    
 
//    public PageReference SaveReturn() {
    	
    	
//    	update Incents;
    		
    	
//    	return 
    		
//    }
       
    
}

 

Starz26Starz26
In apex, you could create variables and set them to the results of a function that does the math, if it is an overall total

If per record, you will have to create a wrapper class....

acrozieracrozier

any help or examples on creating the wrapper class?

Starz26Starz26

Sure...

 

Assume the object is Survey:

 

QTY field and a Honoraria field

Public without sharing class surveyWrapper{
//Wrapper class to store the final data 

public Survey__c theSurvey {get;set;}
Public Decimal qty__c {get;set;} 
Public Decimal Honoraria__c {get;set;}
Public Decimal totPayOut__c {get;set;}


      Public surveyWrapper(Survey__c s, Decimal q, Decimal h){
        theSurvey__c = s;
        qty__c = q;
        Honoraria__c = h;
        totPayOut = q * h;


    }  


}

 

Then to use in your VF page, you would use it just like any class.....

 

 

acrozieracrozier

I see this would apply it to the entire object, what syntax would I use to apply this to an SOQL query that brings up related data from the object?  Below is the code I am using for this query, I am assuming I could ad dthe wrapper method to this extension?

 

public with sharing class Incent {
public list<AIIncentive__c> Incents {get; set;}
public list<job__c> job {get; set;}
public id jobid;
public string NewIncentive {get; set;}
public PageReference refresh = ApexPages.currentPage(); 

    public Incent(ApexPages.StandardController controller) {
    	
     	jobid = apexpages.currentpage().getParameters().get('id');
        system.debug('################################## Id'+jobid);

        Incents = [Select QTY__c, Description__c,  Honoraria__c, Job__c, Deleted__c  From AIIncentive__c  where Job__c = :jobid]; 
        
        job = [SELECT Account__c FROM Job__c WHERE id= :jobid];    
        
        }


    public PageReference NewIncentive(){
    	
    	AIIncentive__c obj = new AIIncentive__c();

          	obj.QTY__c = 0;
     		obj.Description__c = '';
     		obj.Honoraria__c = 0;
     		obj.Job__c = jobid;

	 	update Incents;
	 		 
     	insert obj;
     
        	refresh.setRedirect(true);
     
   	 	return refresh;
     
    }
    
    public PageReference save() {
        	        	
    	update Incents;
        
	        refresh.setRedirect(true);        

        return refresh;

    }
    
 
//    public PageReference SaveReturn() {
    	
    	
//    	update Incents;
    		
    	
//    	return 
    		
//    }
       
    
}

 

acrozieracrozier

Sorry, I forgot to mention the Incents variable is the SOQL I am using to pull up related objects.