VF:
<apex:page controller="AccountWrapper" sidebar="false">
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Select" action="{!isSelected}" rerender="table"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="Accounts">
<apex:pageBlockTable value="{!wrapItems}" var="c" id="table">
<apex:column headerValue="Select">
<apex:inputCheckbox value="{!c.selected}"/>
</apex:column>
<apex:column value="{!c.accList.Name}" headerValue="Account Name"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
<!--<apex:pageblockSection title="Related Contacts">
<apex:pageBlockTable value="{!accContacts}" var="c1" id="table">
<apex:column value="{!c1.Name}" headerValue="Contact Name"/>
</apex:pageBlockTable>
</apex:pageblockSection>-->
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
public class AccountWrapper
{
public List<WrapperAccounts> wrapItems{set;get;}
public List<WrapperAccounts> selectedItem{set;get;}
//public List<Contact> accContacts{set;get;}
public AccountWrapper()
{
wrapItems=new List<WrapperAccounts>();
List<Account> accItem2=new List<Account>([SELECT Name FROM Account limit 10]);
for(Account a1:accItem2)
{
boolean check=false;
wrapItems.add(new WrapperAccounts(check,a1));
}
}
//Method to Fetch Records.
public PageReference isSelected()
{
selectedItem=new List<WrapperAccounts>();
for(WrapperAccounts w:wrapItems)
{
if(w.selected==true)
{
fetchRecords();
}
PageReference p=Page.WrapperReturn;
return p;
}
return null;
}
//Method to define fetchRecords
public void fetchRecords()
{
List<Contact> accContacts=new List<Contact>();
for(WrapperAccounts wraps : wrapItems)
{
for(Contact con:[Select Name from Contact WHERE AccountId!=null])
{
if(wraps.selected)
{
accContacts.add(con);
}
}
System.debug('Contacts Lists:'+accContacts);
}
}
//Wrapper Class
public class WrapperAccounts
{
public boolean selected{set;get;}
public Account accList{set;get;}
public WrapperAccounts(Boolean sel,Account acc)
{
this.accList=acc;
this.selected=sel;
}
}
}
Not able get the related contacts of checked accounts. button is not working.