+ Start a Discussion
Bhavin Mehta 20Bhavin Mehta 20 

Display child record in same pagblock table on buttonclick

I have got a pageblock table with all parents records and all of them has a lineitem button linked to it, now the requirement is to display child records in the same pageblock table under the particular parent recordwhen this button is clicked.Both parent and child belongs to same object and they have self relationship.Thanks in advance
David ZhuDavid Zhu
I came up witht following code for your reference. It works for Account object without changing any line.
The code has no cosmetic, it is for function reference only.
 
public class AccountCustController {
    public list<Account> PAccts {get;set;}
    public list<Account> CAccts {get;set;}
    public id pid {get;set;}
    public boolean showChild {get;set;}
    
    public AccountCustController()
    {
        PAccts = [select id,name from account where Parentid = null];
        CAccts = new list<Account>();
        showChild = false;
    }
    
    public PageReference GetChildAccounts()
    {
        CAccts = [select id, name from account where parentid = :pid];
        showChild= true;
        system.debug('pid =' + pid);
        return null;
    }
}

<apex:page controller="AccountCustController">
    <apex:form>
        <apex:pageBlock>
            <apex:pageBlockSection>
                <apex:pageBlockTable columns="1" value="{!PAccts}" var="p">
                    <apex:column id="idacc">
                        <apex:outputPanel>
                            <apex:outputLabel value="{!p.name}"/>
                            <apex:commandButton action="{!GetChildAccounts}" value="Show Child" rerender="idacc">
                                <apex:param name="pid" value="{!p.id}" assignTo="{!pid}"/>
                            </apex:commandButton>
                        </apex:outputPanel>
                        <apex:outputPanel  rendered ="{!showChild}" >
                            <apex:dataTable value="{!CAccts}" var="c">
                                <apex:column value="{!c.name}"/>
                            </apex:dataTable>
                        </apex:outputPanel>
                    </apex:column>/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>



 
Bhavin Mehta 20Bhavin Mehta 20
User-added image

Currently Show Child button on every parent line item is displayed, I want to display child line item in same table below the row for which show child is clicked. Please check the code below and let me know how can I achieve the same.
 
<apex:page standardController="Order" extensions="orderext">
<apex:detail relatedList="False"/>
<apex:relatedList list="OrderItems"/>
<apex:form >

<apex:pageBlock title="Order Product" > </apex:pageBlock>
<apex:pageBlock >
<apex:outputPanel id="idacc">
<apex:pageBlockTable value="{!parentitem}" var="ab" >
<apex:column >
<apex:commandButton action="{!GetChildAccounts}" value="Show Child" rerender="idacc">
                                <apex:param name="pid" value="{!ab.id}" assignTo="{!pid}"/>
                            </apex:commandButton>
</apex:column>
<apex:column value="{!ab.Pricebookentry.Product2Id}" />
<apex:column headerValue="Order Product Number">
              <apex:outputLink value="/{!ab.id}">{!ab.OrderItemNumber}</apex:outputLink>
         </apex:column>
      
<apex:column value="{!ab.PricebookEntry.productcode}"/>
<apex:column value="{!ab.Child_Product_del__c}"/>
</apex:pageBlockTable>
</apex:outputPanel>

</apex:pageBlock>

</apex:form>
</apex:page>
 
public class orderext {


    public list<orderitem> parentitem{get;set;}
     public list<orderitem> childitem{get;set;}
    public boolean showChild {get;set;}
      public id pid {get;set;}
   

    public orderext(ApexPages.StandardController controller) {

        
        
       parentitem =  [Select id,PricebookEntry.productcode,PricebookEntry.pricebook2id,Pricebookentry.Product2.productcode,OrderItemNumber,Child_Product_del__c,quantity,Pricebookentry.Product2Id from orderitem where order.Id = :System.currentPageReference().getParameters().get('id') and Child_Product_del__c = null];
        childitem = new list<Orderitem>();
        showchild = false;
        
        
        
    }
    
     public PageReference GetChildAccounts()
     {
        childitem =  [ Select id,PricebookEntry.productcode,PricebookEntry.pricebook2id,Pricebookentry.Product2.productcode,OrderItemNumber,Child_Product_del__c,quantity,Pricebookentry.Product2Id from orderitem where order.Id = :System.currentPageReference().getParameters().get('id') and Child_Product_del__c = :pid];
         showChild= true;
         return null;
     }
    
    


}