Created
November 15, 2018 06:20
-
-
Save sudikrt/6f66e281069f7ea4c5faea6e30508a34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DynamicTabsCntl { | |
@AuraEnabled | |
public static List< foolish__TabsOnLightningComponent__mdt > getData () { | |
return [SELECT Id, foolish__ComponentName__c , DeveloperName, MasterLabel, foolish__IsActive__c ,foolish__Params__c | |
FROM foolish__TabsOnLightningComponent__mdt WHERE foolish__IsActive__c = true]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<aura:component controller="DynamicTabsCntl"> | |
<aura:attribute name="tabs" type="Aura.Component[]" /> | |
<aura:attribute name="tabsData" type="Object"/> | |
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/> | |
<aura:dependency resource="markup://lightning:tab" /> | |
<lightning:tabset variant="vertical"> | |
<aura:iteration items="{!v.tabs}" var="item"> | |
{!item} | |
</aura:iteration> | |
</lightning:tabset> | |
</aura:component> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
doInit : function(component, event, helper) { | |
helper.doInit (component, event); | |
}, | |
lazyLoadTabs : function (component, event, helper) { | |
helper.lazyLoadTabs (component, event); | |
} | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
({ | |
doInit : function(component, event) { | |
let action = component.get ('c.getData'); | |
let self = this; | |
action.setCallback (this, function (res){ | |
if (res.getState () === 'SUCCESS') { | |
if (res && res.getReturnValue() && res.getReturnValue().length > 0) { | |
self.createComponent (component, res.getReturnValue()); | |
} | |
} else { | |
console.log ('Error in fetching custom MetaData'); | |
} | |
}); | |
$A.enqueueAction (action); | |
}, | |
createComponent : function (component, mdtList) { | |
if (mdtList && mdtList.length > 0) { | |
let items = {}; | |
var newlst =[]; | |
mdtList.forEach (function (val) { | |
items [val.DeveloperName] = val; | |
}); | |
component.set ('v.tabsData', items); | |
mdtList.forEach (function (val) { | |
$A.createComponent("lightning:tab", { | |
"label": val.MasterLabel, | |
"id": val.DeveloperName, | |
"onactive": component.getReference("c.lazyLoadTabs") | |
}, function (newTab, status, error) { | |
if (status === "SUCCESS") { | |
newlst.push(newTab); | |
component.set("v.tabs", newlst); | |
} else { | |
throw new Error(error); | |
} | |
}); | |
}); | |
} | |
}, | |
lazyLoadTabs: function (component, event) { | |
var tab = event.getSource(); | |
if (tab) { | |
let tabItem = component.get ('v.tabsData') [tab.get ('v.id')]; | |
this.injectComponent(tabItem.foolish__ComponentName__c, JSON.parse(tabItem.foolish__Params__c), tab); | |
} else { | |
console.log ('Error in tab Id'); | |
} | |
}, | |
injectComponent: function (name, params, target) { | |
if (!params) { | |
params = {}; | |
} | |
$A.createComponent(name, params, function (contentComponent, status, error) { | |
if (status === "SUCCESS") { | |
target.set('v.body', contentComponent); | |
} else { | |
throw new Error(error); | |
} | |
}); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment