+ Start a Discussion
Arnold Joseph TodasArnold Joseph Todas 

PageblockTable row clickable on AccountName and show related Contacts

How can i make PageblockTable row clickable. Once i click Account Name it will show all the related Contacts for account?
I very very appreciate if you could help me with my code :
 
<apex:page sidebar="false" standardController="Account" recordSetVar="accounts" tabstyle="account">
<img src="{!$Resource.OnePiece}" />

<apex:pageBlock title="Pirate Account"> 
    <apex:form >
      <apex:pageBlockTable value="{!accounts}" var="a" id="list">

        <apex:column headerValue="Account Name">
            <apex:outputLink value="{!a.name}">{!a.name}</apex:outputLink>
        </apex:column>
        <apex:column value="{!a.type}" />
        <apex:column value="{!a.billingstreet}"/>
        <apex:column value="{!a.billingCity}" />
        <apex:column value="{!a.billingCountry}" />
        <apex:column value="{!a.billingPostalCode}"/>
        <apex:column value="{!a.createdById}"/>
        
      </apex:pageBlockTable>
      <center><apex:commandButton value="Show Contacts"/><apex:commandButton value="Hide Contacts"/></center>
      
    </apex:form>
</apex:pageBlock>


</apex:page>

Thanks!
AJ
Vijay NagarathinamVijay Nagarathinam
Hi Aj,
 
You can acheive this with the help of wrapper class.
 
<apex:page controller="wrapperListAccount">
    <apex:form >
        <apex:outputPanel rendered="{!selectedCheck == false}">
        <apex:pageBlock >
            <apex:pageBlockTable value="{!wrappAccList}" var="w" id="test">
                
                <apex:column headerValue="Checkbox">
                    <apex:inputCheckbox value="{!w.checked}" >
                        <apex:actionSupport event="onclick" reRender="test"/>
                    </apex:inputCheckbox>
                </apex:column>
                <apex:column headervalue="Name">
                     <apex:outputtext value="{!w.acc.Name}"/>
                </apex:column>
                <apex:column headerValue="PhoneNumber">
                    <apex:outputField value="{!w.acc.phone}" rendered="{!w.checked}"/>
                </apex:column>
            </apex:pageBlockTable>
            <apex:pageBlockButtons >
                <apex:commandButton value="Display!!" action="{!displayAcc}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
        </apex:outputPanel>
        
        <apex:outputPanel >
           <apex:pageBlock rendered="{!selectedCheck == true}">
               <apex:pageBlockTable value="{!conList}" var="cc">
                   <apex:column headerValue="ContactName">
                       <apex:outputfield value="{!cc.lastname}"/>
                   </apex:column>
                   <apex:column headerValue="AccountName"> 
                       <apex:outputfield value="{!cc.accountid}"/>
                   </apex:column>
               </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:outputPanel>
    </apex:form>
</apex:page>
 
public class wrapperListAccount 
{
    public list<wrapperAcc> wrappAccList{get;set;}
    public list<Account> getAccList = new list<Account>();
    public list<Contact> conList{get;set;}
    public boolean selectedCheck{get;set;}
    
  public wrapperListAccount()
    {   selectedCheck = false;
        wrappAccList = new list<wrapperAcc>();
        for(Account aa : [SELECT id,name,phone from Account WHERE name != null])
        {
            wrappAccList.add(new wrapperAcc(false,aa));
        }
    }
    
    public pagereference displayAcc()
    {    
        selectedCheck = true;
        conList = new list<Contact>();
        for(wrapperAcc aaa : wrappAccList)
        {
            if(aaa.checked == true)
                getAccList.add(aaa.acc);
              system.debug('@@@@@@@@@@@@@@@'+aaa.acc.name);
        }
        update getAccList;
        for(Contact c : [SELECT id,lastname,accountid,account.name from contact WHERE accountid IN: getAccList])
        {
            c.lastname = c.Account.name +'Contact';
            conList.add(c);
        }
        update conList;
        return null;
    }
    
public class wrapperAcc
{
    public Account acc{get;set;}
    public Boolean checked{get;set;}
    
    Public wrapperAcc(Boolean bool,Account aa)
    {
        acc = aa;
        checked = bool;
    }
}
}

In that page contains the list of account are displayed  once you click the checkbox and press button the selected contacts are displayed in the new pageblock.


 
KaranrajKaranraj
Try the below code for your requirement.

Apex Controller : This controller to get the Account id from the page block table when user click it and it will display the list of contacts associated with that particular contact
public with sharing class DisplayContact {
    public List<Contact> conList {get;set;}
    public DisplayContact(ApexPages.StandardSetController controller) {

    }
    
    public PageReference ContactLists()
    {
    if(ApexPages.currentPage().getParameters().get('id') != null)
      conList = [Select id,Name,Phone,Email from contact where accountId =: ApexPages.currentPage().getParameters().get('id')];
     return null;
    }   

}

Visualforce : Visualforce page to display all the acocunt record and its contact record. For the partical page refresh we used rerender concept in the visualforce page
<apex:page standardController="Account" recordSetVar="accounts" tabstyle="account" sidebar="false" extensions="DisplayContact" >
  <apex:pageBlock title="Account" >
    <apex:form >
      <apex:pageBlockTable value="{!accounts}" var="a" id="list">
        <apex:column headerValue="Account Name">
        <apex:commandLink rerender="contactDetails" value=" {!a.Name}" action="{!ContactLists}">    
         <apex:param name="id" value="{!a.id}"/>
       </apex:commandLink> 
         </apex:column>
        <apex:column value="{!a.type}" />
        <apex:column value="{!a.billingstreet}"/>
        <apex:column value="{!a.billingCity}" />
        <apex:column value="{!a.billingCountry}" />
        <apex:column value="{!a.billingPostalCode}"/>
        <apex:column value="{!a.createdById}"/>
      </apex:pageBlockTable>
    </apex:form>
   
  </apex:pageBlock>
  <apex:pageBlock title="Contact">
   <apex:outputPanel id="contactDetails">
     <apex:pageBlockTable value="{!conList}" var="con" id="conlist" title="Contact">
     <apex:column value="{!con.Name}"/>
     <apex:Column value="{!con.Phone}" />
     <apex:Column value="{!con.Email}" />
     </apex:pageBlockTable>
    </apex:outputPanel>
  </apex:pageBlock>
  
</apex:page>