+ Start a Discussion
hisrinuhisrinu 

How to display Parent to Child Relationship in VF?

How to display the contact Name in VF page.
Below is the example, I want to display Contact Name in VF_Page.
Any Suggestions.

Code:
<apex:page controller="Relationships">
<apex:dataTable value="{!Accounts}" var="a">
<apex:column headervalue="Name" value="{!a.Id}"/>
<apex:column headervalue="Name" value="{!a.Contact.Name}"/>
</apex:dataTable>
</apex:page>

public class Relationships 
{
    public List<Account> getAccounts()
    {
        List<Account> AL = new List<Account>();
        AL = [SELECT Id, (SELECT Name FROM Contacts WHERE Name != '') FROM Account];
        return AL;
    }
}

 
MohanaGopalMohanaGopal

Hi.. Srini..

 

Try this

Code:
<apex:page standardcontroller="Account">
<apex:dataTable value="{!Account.Contacts}" var="con">
<apex:column headervalue="Account" value="{!con.Account.Name}"/>
<apex:column headervalue="Contact Name" value="{!con.Name}"/>
</apex:dataTable>
</apex:page>


 

hisrinuhisrinu
Hi Mohana,

Thanks for your reply. I know that but It will display in two tables but I want to display them in only one table.
Because the inner query will be in a list format so we need to display as separate table but is there any other way.

JimRaeJimRae
One thing you need to remember, you SOQL is actually going to return an array of contacts, since more than one contact could be on each account.  I also don't think your query will prevent accounts that have no contacts from being selected, you will need to do something more advanced to get that to work.  If you want all of the contacts related to accounts, you might want to reverse your query, and make it based on Contacts and pull the account data as well (ie: select id,name, Accountid,Account.name from contact where name !='')

Here is an example of a nested query result dispaly using pageblock and the repeat tag that might help you get started.

Code:
<apex:page controller="Relationships">
   <apex:pageblock>
    <apex:repeat value="{!Accounts}" var="a">
        <apex:pageblockSection title="{!a.Name}" columns="1">      
           <apex:repeat value="{!a.contacts}" var="c">
                 <apex:pageblockSectionItem >
                     <apex:outputLabel value="     Contact Name: " for="contact_name"/>
                     <apex:outputText title="Contact Name: " style="font-style:bold" value="{!c.name}" id="contact_name" />
                 </apex:pageblockSectionItem>

                 <br/>
           </apex:repeat>
       </apex:pageblockSection>
</apex:repeat>
</apex:pageblock>
</apex:page>

 

hisrinuhisrinu
Jim,

 Thanks for your reply, I know that if we fetch from contact it will be easier.
  But my actual requirement is I have to fetch from Account, Contact and Opportunity
  That is the reason I am trying in this manner.
JimRaeJimRae
If that is the case, you might want to consider creating a wrapper class to contain your objects.
What is your desired hierarchy? 
Are you looking for this:

Account
-Contact
-Contact
-Contact
--Opportunity
--Opportunity
--Opportunity

Any restrictions? Like do you not want accounts that don't have both contacts and opportunities?  Some other conditions?
One consideration we haven't discussed is how many results you expect to have.  Lists won't handle over 1000 results, and this would show a pretty large page result anyway.