This scenario mainly describes the usage of Wrapper class. Wrapper class is used to display different types of datatypes in a single table. In the following scenario we are displaying Boolean and list datatypes.
Example:
Visualforce page for displaying the Accounts with select checkbox and displaying the Selected Accounts in another Visualforce Page.
Example:
Visualforce page for displaying the Accounts with select checkbox and displaying the Selected Accounts in another Visualforce Page.
Page1:
<apex:page sidebar="false" controller="WrapTestClassDemoController">
<apex:form >
<apex:pageblock >
<apex:pageblocksection >
<apex:pageblocktable value="{!testingrecords}" var="tr">
<apex:column headervalue="Action">
<apex:inputcheckbox value="{!tr.ischecked}"/>
</apex:column>
<apex:column headerValue="Name">{!tr.Name}
</apex:column>
<apex:column headerValue="City">{!tr.Phone}
</apex:column>
</apex:pageblocktable>
</apex:pageblocksection>
</apex:pageblock>
<apex:commandButton value="SELECT" action="{!selRecDisplay}"/>
</apex:form>
</apex:page>
Controller1:
public with sharing class WrapTestClassDemoController{
List<string> lstselectedNames = new List<string>();
public PageReference selRecDisplay() {
for(wrapper w: lstwrap){
if(w.ischecked==true){
lstselectedNames.add(w.Name);
}
}
List<Account> lstselectedrecords = [select Id,name,Phone from Account where name in : lstselectedNames];
List<String> lstselectedRecordIds = new List<String>();
for(Integer i=0;i<lstselectedrecords.size();i++){
lstselectedRecordIds.add(lstselectedrecords[i].Id);
}
string s='';
for(Integer i=0;i<lstselectedRecordIds.size();i++){
if(i<lstselectedRecordIds.size()-1)
s=s+lstselectedRecordIds[i]+':';
else
s=s+lstselectedRecordIds[i];
}
pagereference ref = new pagereference('/apex/WrapperClassDemoPage2?value='+s);
ref.setredirect(true);
return ref;
}
List<wrapper> lstwrap = new List<wrapper>();
List<Account> lsttest = new List<Account>();
public List<wrapper> getTestingrecords() {
lsttest = [select Id,name,Phone from Account];
for(Integer i=0;i<lsttest.size();i++){
lstwrap.add(new wrapper(lsttest[i].name,lsttest[i].Phone));
}
return lstwrap;
}
public class wrapper{
public String Name{get;set;}
public String Phone{get;set;}
public boolean ischecked{get;set;}
public wrapper(String Name,String Phone){
this.Name=Name;
this.Phone=Phone;
this.ischecked=false;
}
}
}
Page2:
// Page Name: WrapperClassDemoPage2
<apex:page controller="WrapTestClassDemoController2">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!records}" var="r">
<apex:column headerValue="Name">{!r.Name}
</apex:column>
<apex:column headerValue="Phone">{!r.Phone}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller2:
public with sharing class WrapTestClassDemoController2{
List<Account> lsttest = new List<Account>();
String url = apexpages.currentpage().getparameters().get('value');
public List<Account> getRecords() {
List<String> ids = url.split(':');
lsttest = [select Id,name,Phone from Account where id in : ids];
return lsttest;
}
}
Page1 Output:
Page2 Output:
No comments:
Post a Comment