i am trying to create account contact hierarchy in the lightning:tree
and identifying nth level and creating tree like nested tree structure.
parent account contact1 contact2 child account1 contact3 contact4 childaccount2 contact5 contact6
it is creating tree on same levels
Contents
hide
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Adapting the code I wrote in my gist, you simply need to build the nested tree. Here’s an example I wrote up, now also available as a gist:
Apex Code
public class q216767 { @AuraEnabled public static Account[] getRecords() { return [SELECT Name, ParentId, (SELECT Name FROM Contacts) FROM Account]; } }
Application
<aura:application controller="q216767" extends="force:slds"> <aura:attribute name="data" type="List" default="[]" /> <aura:handler name="init" value="{!this}" action="{!c.init}" /> <lightning:tree header="Accounts" items="{!v.data}" /> </aura:application>
Controller
({ init: function(component, event, helper) { var action = component.get("c.getRecords"); action.setCallback(this, result => helper.parse(component, result)); $A.enqueueAction(action); } })
Helper
({ parse: function(component, result) { var accounts = result.getReturnValue(), parents = { undefined: { items: [] }}; accounts.forEach(account => parents[account.Id] = { items: [], name: account.Id, label: "Account: "+account.Name, expanded: false}); accounts.forEach(account => { if(account.Contacts) { account.Contacts.forEach(contact => parents[account.Id].items.push({items: [], name: contact.Id, label: "Contact: "+contact.Name, expanded: false}))}}); accounts.forEach(account => parents[account.ParentId].items.push(parents[account.Id])); component.set("v.data", parents[undefined].items); } })
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0