Wrapper class for displaying the selected records in Next page
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.
page1:
<apex:page sidebar="false" controller="WrapTestClass">
<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.TName}
</apex:column>
<apex:column headerValue="City">
{!tr.TCity}
</apex:column>
</apex:pageblocktable>
</apex:pageblocksection>
</apex:pageblock>
<apex:commandButton value="SELECT" action="{!selRecDisplay}"/>
</apex:form>
</apex:page>
class1:
public with sharing class WrapTestClass {
List<string> lstselectedNames = new List<string>();
public PageReference selRecDisplay() {
for(wrapper w: lstwrap){
if(w.ischecked==true){
lstselectedNames.add(w.Tname);
}
}
List<Testing__c> lstselectedrecords = [select Id,name,city__c from Testing__c 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/t123?value='+s);
ref.setredirect(true);
return ref;
}
List<wrapper> lstwrap = new List<wrapper>();
List<Testing__c> lsttest = new List<Testing__c>();
public List<wrapper> getTestingrecords() {
lsttest = [select Id,name,city__c from Testing__c];
for(Integer i=0;i<lsttest.size();i++) {
lstwrap.add(new wrapper(lsttest[i].name,lsttest[i].city__c));
}
return lstwrap;
}
public class wrapper{
public String Tname{get;set;}
public String Tcity{get;set;}
public boolean ischecked{get;set;}
public wrapper(String Tname,String Tcity) {
this.Tname=Tname;
this.Tcity=Tcity;
this.ischecked=false;
}
}
}
page2:
<apex:page controller="DClass">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:pageBlockTable value="{!records}" var="r">
<apex:column headerValue="Name">
{!r.Name}
</apex:column>
<apex:column headerValue="City">
{!r.City__c}
</apex:column>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
class2:
public with sharing class DClass {
List<Testing__c> lsttest = new List<Testing__c>();
public List<Testing__c> getRecords() {
List<String> ids = url.split(':');
lsttest = [select Id,name,city__c from Testing__c where id in : ids];
return lsttest;
}
String url = apexpages.currentpage().getparameters().get('value');
}