0

I wanted to create tree structure from two custom objects with master detail relationship. Head is my Parent custom object and member_child is my child custom object with childname__c as field. I am able to print the table showing only parent records with select boxes created using wrapper class.

Problems:

  1. Not able to design VF page to look like

Parent
Child1
Child2

Parent 2
Child3
Child4

  1. Not able to abstract child's object childname field value,

APEX CODE

public with sharing class master_detail_tree_class {

    public List<wrapRecord> wrapRecordList{get; set;}
    public List<Head__c> selectedRecords{get;set;}    
    public String rID{get;set;}

    public master_detail_tree_class () {

        if(wrapRecordList == null) {
            wrapRecordList = new List<wrapRecord>();
            for(Head__c a: [SELECT  Id,Name, (SELECT Id, childname__c FROM member_childs__r) FROM Head__c]) {
                // As each Head is processed we create a new wrapRecord object and add it to the wrapRecordList
                wrapRecordList.add(new wrapRecord(a));                

                System.debug('Head Name:'+ a.Name);
                for(Member_Child__c c : a.Member_Childs__r) { 
                     // here you can access Member_Child using the variable c, like what you have done, in debug statement
                     System.debug('Child Name:' +c.childname__c);
                }
            }
        }   
    }

    public void processSelected() {
        selectedRecords = new List<Head__c>();
        for(wrapRecord wrapRecordObj : wrapRecordList) {
            if(wrapRecordObj.selected == true) {
                selectedRecords.add(wrapRecordObj.ncc);
            }
        }
    }


    //Wrapper class
    public class wrapRecord {

        public Head__c ncc{get;set;} 
        public Boolean selected {get; set;}
        public wrapRecord(Head__c n) {
            ncc = n;
            selected = false;
        }  
    }
}

VF CODE

<apex:page title="Tree Creation" controller="master_detail_tree_class"     showHeader="false" sidebar="false" readOnly="true" cache="false">
<apex:sectionHeader subtitle="Parent Child Tree"/>

   <script type="text/javascript">
        function selectAllCheckboxes(obj,receivedInputID){
        var inputCheckBox = document.getElementsByTagName("input");
        for(var i=0; i<inputCheckBox.length; i++){
            if(inputCheckBox[i].id.indexOf(receivedInputID)!=-1){
                inputCheckBox[i].checked = obj.checked;
            }
        }
    }
</script> 

    <apex:form >
    <apex:pageBlock >

        <apex:pageblockSection title="All Accounts" collapsible="false" columns="2">

            <apex:pageBlockTable value="{!wrapRecordList}" var="accWrap" id="table" title="All Accounts">
                <apex:column >
                    <apex:facet name="header">
                        <apex:inputCheckbox onclick="selectAllCheckboxes(this,'inputId')"/>
                    </apex:facet>
                    <apex:inputCheckbox value="{!accWrap.selected}" id="inputId"/>
                </apex:column>
                <apex:column value="{!accWrap.ncc.Id}" />

                <apex:column value="{!accWrap.ncc.Name}" />


            </apex:pageBlockTable>
        </apex:pageblockSection>


    </apex:pageBlock>
</apex:form>
</apex:page>
0

Refer to this Link : Display Children and Parent Records in Visualforce Page

All you have to do is add another PageBlockTable

 --
 --
 <apex:column value="{!accWrap.ncc.Id}" />

     <apex:column value="{!accWrap.ncc.Name}" />
    <apex:pageBlockTable value="accWrap.ncc.Member_Childs__r" var="child">
        //add columns
    </apex:pageBlockTable> 

 </apex:pageBlockTable>

-- --

  • Tried with the same thing but did not worked well. Anyways, I am able to resolve same by using the Table structure on VF page. – Coder May 2 '16 at 10:59

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.