Hi Guys,
In my last post I showed how to use SVG Icon in Lightning Component. In this post, I will be showing you how to build a Nested component in Lightning Component using Salesforce Lightning Design System. In this you can see Tree view of Accounts and its related contacts like:
Let’s walk through code by few steps:
Step-1: AccConListController.apxc
public class AccConListController { @AuraEnabled public static List<Account> getAccounts() { List<Account> accounts=[SELECT Id, Name, (select name, Phone, Email FROM Contacts) FROM Account limit 1000]; return accounts; } }
In this class we create a funtion which is used for getting accounts and its related contacts.
Step-2: AccountRow.cmp
<aura:component > <aura:attribute name="acc" type="Account" /> <aura:attribute name="ext" type ="String" default="plus"/> <li id="tree0-node0" class="slds-tree__branch slds-is-open" role="treeitem" aria-level="1" aria-expanded="true"> <div class="slds-tree__item"> <aura:if isTrue="{!v.acc.Contacts[0] != null}"> <aura:if isTrue="{!v.ext=='plus'}"> <div id="plus" >➕</div> <aura:set attribute="else"> <div id="minus">➖</div> </aura:set> </aura:if> <aura:set attribute="else"> </aura:set> </aura:if> <a id="tree0-node0-0-link" tabindex="-1" onclick="{!c.showHidePanel}" role="presentation">{!v.acc.Name}</a> </div> <ul aura:id="{!v.acc.Id}" id="{!v.acc.Id}" style="display:none;" class="slds-tree__group slds-nested" role="group" aria-labelledby="tree0-node0-link"> <aura:iteration items="{!v.acc.Contacts}" var="con"> <li id="tree0-node0-1" class="slds-tree__item" role="treeitem" aria-level="2" style="margin-left: 20px;"> <a href="#" role="presentation" class="slds-truncate" style="color: darkgoldenrod;">{!con.Name}</a> </li> </aura:iteration> </ul> </li> </aura:component>
This is a Child component and it is used for show an account name and its related contacts.
Step-3: AccountRowController.js
({ showHidePanel : function(component, event, helper) { var id=component.get("v.acc.Id"); var e=document.getElementById(id); if (e.style.display == 'block' || e.style.display==''){ e.style.display = 'none'; component.set("v.ext","plus"); }else{ e.style.display = 'block'; component.set("v.ext","minus"); } }, })
This is controller of AccountRow Component.
Step-4: AccountTree.cmp
<aura:component controller="AccConListController" implements="force:appHostable"> <ltng:require styles="/resource/SLDS/assets/styles/salesforce-lightning-design-system-vf.css" scripts="/resource/jquerymin" afterScriptsLoaded="{!c.doInit}" /> <aura:attribute name="Accounts" type="Account[]" /> <div class="slds"> <div class="slds-page-header"> <div class="slds-grid"> <div class="slds-col slds-has-flexi-truncate"> <div class="slds-media"> <div class="slds-media__figure"> <c:svgIcon svgPath="/resource/SLDS/assets/icons/standard-sprite/svg/symbols.svg#process" category="standard" size="large" name="user" /> </div> <div class="slds-media__body"> <p class="slds-text-heading--label">Tree View</p> <div class="slds-grid"> <h1 class="slds-text-heading--medium slds-m-right--small slds-truncate slds-align-middle">Account -> Contact</h1> </div> </div> </div> </div> <!-- /slds-col--> </div> <!-- /slds-grid--> </div> <!-- /slds-page-header--> <div class="slds-tree-container" role="application"> <ul class="slds-tree" role="tree" aria-labelledby="treeheading" aria-activedescendant="tree0-node0"> <aura:iteration items="{!v.Accounts}" var="acc"> <c:AccountRow acc="{!acc}" /> </aura:iteration> </ul> </div> </div> </aura:component>
This is parent component that holds AccountRow component as a nested component. And also i am using here SVG Icon in header. For more detail about SVG Icon see in my previous post or click here.
Step-5: AccountTreeController.js
({ doInit : function(component, event, helper) { helper.getAccounts(component); }, showPanel : function(component, event, helper){ helper.onLoadPage(component); }, })
This is controller of AccountTree Component.
Step-6: AccountTreeHelper.js
({ //Fetch the Accounts from the Apex controller getAccounts: function(component) { var action = component.get("c.getAccounts"); //Set up the callback var self = this; action.setCallback(this, function(actionResult) { component.set("v.Accounts", actionResult.getReturnValue()); }); $A.enqueueAction(action); }, })
This is helper of AccountTree Component. Its getAccounts() function calls getAccounts() method of AccConListController.apxc class
Pingback: Lightning Component Bundle Concept | Sushil Kumar
well tried sperb.. scripts=”/resource/jquerymin” here what u have put in resource
LikeLike
This is jQuery.min.js file which is easily available online. You can download from this link : //code.jquery.com/jquery-1.12.0.min.js or click here
LikeLike
Hi sushil,
I am facing issue in achieving drag and drop functionality in Lightning using jquery. Can you please help me with some sample code and JQuery version which will correctly load in Lightning to achieve Draggable and Droppable functionality.
Thanks,
Chaitanya
LikeLike
Hi Chaitanya
I didn’t work drag and drop functionality with lightning and jQuary. I will try to achieve the same and if i’ll get succes then let you know.
Thanks
Sushil Kumar
LikeLike
Thanks !! Well I am able to achieve using HTML5 in Lightning for Drag and Drop.Let me know if you get success with jquery.
LikeLike
yes sure
LikeLike
Hi Sushil,
Need a help !!
Just like rerender or use of action region in Visualforce.How to rerender a particular block in Lightning.
Thanks
Chaitanya
LikeLike
Hi Sushil,
How we use apex wrapper class in this instead of account object.
LikeLike
Hi Vignesh
We can also use either list of wrapper class instead of the list of Account or it depends on the wrapper.
If you can share your requirement then I can help for the same.
Thanks
LikeLiked by 1 person
hello,
i am trying to use the same nested component and was not able to see any data on page… can you please tell me the version of SLDS you are using
LikeLike
Hi KA
This component will work with all versions of SLDS. I think u r missing something.
LikeLike
Hi Sushil,
i’m having the same problem and can’t see any data.
what am i missing? i just copied your code exactly
maybe you can assist?
Thanks
LikeLike
Hi Sushil,
we tried using this component and got this error. if we are removing the SVG:Icon component from AccountTree.cmp, page will be displayed with only Account->Contacts text and records not displayed. we used the solution mentioned in success story but didnt worked out. Could you please suggest solution.
https://success.salesforce.com/issues_view?id=a1p30000000jhaFAAQ
Aura.loadComponent(): Failed to initialize application.
An internal server error has occurred
Thanks and regards,
Lokesh
LikeLike
Hi Lokesh
Maybe you have missed something in the script that’s why you are getting an error. If you will share code then I can able to resolve your problem.
Thanks & Regards
Sushil Kumar
LikeLike
Hi,
I have a requirement to display a nested data structure of accounts. Each account has multiple addresses. Each account as well as address will have check boxes for user to select and when select will have to do some processing. All this in lightning in SFDC. Can you please help me to implement this requirement?
Thanks,
LikeLike
Hi @suvajitmajumder
If want just to do processing when you will select checkbox then you can write code on “onclick” event and call it into the controller.
LikeLike
Hey Thanks for replying. My requirement is a bit more complicated than that.. So I need to display a tree kind of structure with the accounts at the top node and the addresses as children nodes. Both account and addresses have radio buttons that the user can select and once done,the chosen records will be sent to apex controller for some dml operations. Now the data that is displayed in this tree structure ,account and addresses are not present in salesforce but is received from an external system with a REST callout.
Can you help me here? I can do this in visual force but I am not that ok with lightning so I am a bit confused to implement this in lightning
LikeLike
I think you need to use component event for this scenario. If you able to give me tree structure image then i m able to give you proper way. You can send me image on sushil.verma1988@gmail.com
LikeLike
Like with some sample code or something will really appreciate that.
LikeLiked by 1 person
Hi Sushil,
Nice Blog. I have question regarding this.
My Requirement is for a self Lookup in Asset of Object.I need to create same Tree Structure of the records.I am facing issue with the soql Query for Self Lookup.Can you suggest what to do?
LikeLike
Hi ,
Regarding above Code :- “Nested component in Lightning Component using Salesforce Lightning Design System. In this you can see Tree view of Accounts and its related contacts ”
Could any one suggest can we do 3 level Relationship Like :-
1) Click on Account we are showing child object Contact.
2) But my requirement is
2.1) Click on Account show related Contact and
2.2) After Showing contact if i click any contact then need to show one custom object table related to that contact.
Can you suggest what to do?
Thanks.
LikeLike