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
Ram SRam S 

VF page pageblocktable

Hello,

I am new to salesforce development, can any one help on this?

Q: I want vf page pageblocktable should contain 3 columns, in that first two columns is from custom object and values for this columns is pulled from custom object. And 3rd column is result(SUM) of 1st and 2nd column(3rd column doesnt belong to custom object, it is independent column which is used to display result only)? Need help to create this process.


Regards,
Ram

 
Best Answer chosen by Ram S
Amit GhadageAmit Ghadage
Use Wrapper class 
refer apex class and vf page
public class customController
{
    public  List <customWrapper> customWrapperList{get;set;}
    public   void loadCustomObj()
    {
 
        customWrapperList= new List<customWrapper>();
   
  for(Contact c : [select C1, C2  from customObject)
            {
                customWrapper cw = new customWrapper(c.C1,c.C2,  c.C1+c.C2 );
                customWrapperList.add(aw);
                
            }
        }
     
    }
    
    public class customWrapper{
    public  String C1{get; set;}
    public  String C2{get; set;}
     public  String C1C2{get; set;}
        AccountWrapper(String C1,String C2, String C1C2)
        {
            this.C1= C1;
            this.C2= C2;
            this.C1C2= C1C2;   
        }
    }
}
 
<apex:page controller="customController" action="{!loadCustomObj}">
<apex:form >
    <apex:pageBlock id="details">
    <apex:pageBlockTable id="table" value="{!customWrapperList}" var="cw">
     <apex:column value="{!cw.C1}"/>
    <apex:column value="{!cw.C2}"/>
    <apex:column value="{!cw.C1C2}"/>
    </apex:pageBlockTable>
      
    </apex:pageBlock>
</apex:form>
</apex:page>

 

All Answers

Lance Shi 15Lance Shi 15
You need to utilise wrapper class in this case. Below is the code sample: 

Controller: 
public class myController
{
	public List<helperClass> helperList {get; set;}

	public myController()
	{
		List<My_Obj__c> obj_list = [Select Id, col1__c, col2__c From My_Obj__c];

		for(My_Obj__c obj: obj_list) 
		{
			helperClass helper = new helperClass(obj.col1__c, obj.col2__c);
			helperList.add(helper);
		}
	}

	public class helperClass 
	{
		public Integer col1 {get; set;}
		public Integer col2 {get; set;}
		public Integer col3 {get; set;}

		public helperClass(Integer col1, Integer col2) 
		{
			this.col1 = col1;
			this.col2 = col2;
			col3 = col1 + col2;
		}
	}
}

Then you can use <Apex:pageBlockTable> to display the values in helperList. 
Ram SRam S
Thanks Lance,
can u help on VF also?
Amit GhadageAmit Ghadage
Use Wrapper class 
refer apex class and vf page
public class customController
{
    public  List <customWrapper> customWrapperList{get;set;}
    public   void loadCustomObj()
    {
 
        customWrapperList= new List<customWrapper>();
   
  for(Contact c : [select C1, C2  from customObject)
            {
                customWrapper cw = new customWrapper(c.C1,c.C2,  c.C1+c.C2 );
                customWrapperList.add(aw);
                
            }
        }
     
    }
    
    public class customWrapper{
    public  String C1{get; set;}
    public  String C2{get; set;}
     public  String C1C2{get; set;}
        AccountWrapper(String C1,String C2, String C1C2)
        {
            this.C1= C1;
            this.C2= C2;
            this.C1C2= C1C2;   
        }
    }
}
 
<apex:page controller="customController" action="{!loadCustomObj}">
<apex:form >
    <apex:pageBlock id="details">
    <apex:pageBlockTable id="table" value="{!customWrapperList}" var="cw">
     <apex:column value="{!cw.C1}"/>
    <apex:column value="{!cw.C2}"/>
    <apex:column value="{!cw.C1C2}"/>
    </apex:pageBlockTable>
      
    </apex:pageBlock>
</apex:form>
</apex:page>

 
This was selected as the best answer
Ram SRam S
Thanks amit,
it worked