Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Created February 28, 2018 15:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brianmfear/32bd056260cf4850ba15bc5641b6c42f to your computer and use it in GitHub Desktop.
Save brianmfear/32bd056260cf4850ba15bc5641b6c42f to your computer and use it in GitHub Desktop.
Recursive Tree in Lightning Demo
<aura:component >
<aura:attribute name="node" type="Object" />
<aura:attribute name="expanded" type="Boolean" default="{!false}" />
<div>
<div onclick="{!c.toggle}">
<aura:if isTrue="{!v.node.items.length>0}">
[ {!v.expanded?'-':'+'} ]
</aura:if>
<span>
<aura:if isTrue="{!v.node.SmallPhotoUrl}">
<img src="{!v.node.SmallPhotoUrl}" />
</aura:if>
{!v.node.Name}
</span>
</div>
<aura:if isTrue="{!v.expanded}">
<div class="node">
<aura:iteration items="{!v.node.items}" var="node">
<c:treeNode node="{!node}" />
</aura:iteration>
</div>
</aura:if>
</div>
</aura:component>
.THIS .node {
margin-left: 1em;
}
({
toggle: function(component, event, helper) {
component.set("v.expanded", !component.get("v.expanded"));
}
})
public class TreeNodeDataProvider {
@AuraEnabled public static User[] getUsers() {
return [SELECT Name, ManagerId, SmallPhotoUrl FROM User];
}
}
<aura:application controller="TreeNodeDataProvider">
<aura:attribute name="root" type="Map" default="{ 'Name': 'Loading...', items: []}" />
<aura:handler name="init" value="{!this}" action="{!c.init}" />
<c:treeNode node="{!v.root}" expanded="{!true}" />
</aura:application>
({
init: function(component, event, helper) {
var action = component.get("c.getUsers");
action.setCallback(this, response => {
var users = {}, results = response.getReturnValue();
users[undefined] = { Name: "Root", SmallPhotoUrl: null, items: [] };
results.forEach(v => users[v.Id] = { Name: v.Name, SmallPhotoUrl: v.SmallPhotoUrl, items: [] });
results.forEach(v => users[v.ManagerId].items.push(users[v.Id]));
component.set("v.root", users[undefined]);
console.log(users[undefined]);
});
$A.enqueueAction(action);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment