Scenario:
A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In Apex and Visualforce this type of class can be extremely useful to achieve lot of businees scenario. Here I will supply a simple demo of how wrapper classes can be used.
Solution:
Visualforce Code:
Apex controller:
Result:
A wrapper or container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In Apex and Visualforce this type of class can be extremely useful to achieve lot of businees scenario. Here I will supply a simple demo of how wrapper classes can be used.
Solution:
Visualforce Code:
<apex:page controller="Checkbox_Class" Tabstyle="Account"> <apex:form > <apex:pageBlock Title="List of Accounts" > <apex:pageBlockButtons > <apex:commandButton value="Display the selected Records" action="{!GetSelected}" rerender="Selected_PBS"/> </apex:pageBlockButtons> <apex:pageblockSection > <apex:pageBlockSection Title="List of Available Accounts" columns="2" > <apex:pageblockTable value="{!accounts}" var="a" > <apex:column headerValue="Select" width="60"> <apex:inputCheckbox value="{!a.selected}" id="checkedone" /> </apex:column> <apex:column headervalue="Account Name" value="{!a.acc.Name}" width="200"/> <apex:column headervalue="Phone" value="{!a.acc.Phone}" width="300"/> </apex:pageblocktable> </apex:pageBlockSection> <apex:pageBlockSection Title="Selected Accounts" id="Selected_PBS"> <apex:pageblockTable value="{!SelectedAccounts}" var="s" > <apex:column headervalue="Account Name" value="{!s.Name}" width="30"/> <apex:column headervalue="Phone" value="{!s.Phone}" width="30"/> </apex:pageblockTable> </apex:pageBlockSection> </apex:pageblockSection> </apex:pageBlock> </apex:form> </apex:page>
Apex controller:
public class Checkbox_Class { List<accountwrapper1> accountList = new List<accountwrapper1>(); List<Account> selectedAccounts = new List<Account>(); public List<accountwrapper1> getAccounts() { for(Account a : [select Id, Name, AccountNumber, Phone from Account limit 5]) accountList.add(new accountwrapper1(a)); return accountList; } public PageReference getSelected() { selectedAccounts.clear(); for(accountwrapper1 accwrapper : accountList) if(accwrapper.selected == true) selectedAccounts.add(accwrapper.acc); return null; } public List<Account> GetSelectedAccounts() { if(selectedAccounts.size()>0) return selectedAccounts; else return null; } public class accountwrapper1 { public Account acc{get; set;} public Boolean selected {get; set;} public accountwrapper1(Account a) { acc = a; selected = false; } } }
Result: