0

I want to display the child records in a pageblock table when the parent id is selected.

I am generating the list of Account record on a page load and displayed the account records in a datatable and on click of the Account record i want to display the Child records in the same page, in that retrieved contact record want to edit some fields.Please suggest me the solution.

Thanks in Advance

Visualforce Page:

<apex:page controller="SignupformController">
<apex:form><apex:pageBlock title="Accounts">
<apex:dataTable value="{!accountlist}" var="a" cellPadding="4" border="1">
<apex:column >
<apex:commandLink onclick="{!contactdetails}" >
{!a.Name}
</apex:commandLink>
</apex:column>
</apex:dataTable>
<apex:dataTable value="{!contactdetails}" var="c" id="conid" cellPadding="4" border="1">
<apex:column > 
{!c.Name}
</apex:column>
</apex:dataTable>
</apex:pageBlock>
</apex:form> </apex:page>  

Controller :

public with sharing class SignupformController{
public List<account> accountlist{get;set;}
public set<Id> accountids= new set<Id>();
public  List<contact> contactlist{get;set;}
public string eventid{get;set;}
public SignupformController(){
   accountlist= [select Id,Name FROM Account];
   for(Account a : accountlist){accountids.add(a.Id)}
}
public void getcontactdetails(){
contactlist= [select Id,AccountId,Name From contact WHERE AccountId In:accountids];}
}
1

Your controller should probably be written as an extension of Account. see the Visualforce Developer Guide on writing Controller Extensions. Your controller needs to be initialized with an Account record and an instance of the Standard Controller if you're going to write it as a custom controller.

Either way it needs an accountId or list of Ids to initialize it in the constructor. What your constructor is doing is querying for a list of Accounts without any criteria, then adding Ids to a set from the results of that query.

In essence, your constructor would query every account in your database. At the same time, you have an Apex properties that "gets" the list of accounts for your query from your visualforce page which makes no sense to me. Perhaps that might apply to a different method after initialization?

Again, I suggest you read up on the documentation on how controllers are written and decide how you want to initialize your page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.