I want to group accounts and contact...on VF page..I have used a wrapper class for this..
VF Page
<apex:pageBlock >
<apex:repeat value="{!wrapperLst}" var="wr">
<apex:pageBlockSection title="{!wr.acc.Name}"/>
<apex:pageBlockTable value="{!wr.gpaList}" var="gp">
<apex:column headerValue="Name" >
{!gp.name}
</apex:column>
</apex:pageBlockTable>
</apex:repeat>
</apex:pageBlock>
Class
accSearch =[select id,name,Phone,(Select id,Name from Contacts)from Account ];
for(Account accRec:accSearch)
{
for(Contact gpa:accRec.contacts)
{
wrapperLst.add(new wrapperAcc(accRec,new List<Contacts>{gpa}));
}
}
public class wrapperAcc
{
public account acc{get;set;}
public List<Contact> gpaList{get;set;}
public wrapperAcc(Account acc,List<Contact> gpaList)
{
this.acc=acc;
this.gpaList=gpaList;
}
}
This is the output i am getting.So it is not grouped correctly.As there are 2 Accounts so only 2 sections should be created but there are 4..
I know the problem when i am adding the value to the wrapper list i have 2 loops so for the same account 2 values are added to the list.I only want to use a wrapper class .
I know the the solution where i can add the list directly with only a single loop.Can someone please help me finding the solution of this with wrapper class.May be creating some MAP or SET in wrapper class.
for(Account accRec:accSearch)
{
for(Contact gpa:accRec.contacts)
{
wrapperLst.add(new wrapperAcc(accRec,new List<Contacts>{gpa}));
}
}
Working Fine
for(Account accRec:accSearch)
{
wrapperLst.add(new wrapperAcc(accRec,accRec.contacts));
}