Lightning Component:
<aura:attribute name="plantNo" type ="String"/>
<aura:attribute name="plantMap" type="Map"/>
<aura:attribute name="SelectPlant" type="String" Default="2001" />
<aura:handler name="init" value="{!this}" action="{!c.getPlantpicklist}" />
<lightning:select aura:id="plantPicklist" value="{!v.SelectPlant}" name="Plant Type" label="Plant Type" required="true">
<aura:iteration items="{!v.plantMap}" var="p" indexVar="key">
<option text="{!p.value}" value="{!p.key}" selected="{!p.key=='2001'}" />
</aura:iteration>
</lightning:select>
=============================================================
Lightning component controller:
getPlantpicklist:function(component,event,helper)
{
//get plant picklist
var action = component.get("c.getPlant");
action.setCallback(this, function(response) {
var state = response.getState();
if (state === "SUCCESS") {
var result = response.getReturnValue();
var plantvalueMap = [];
for(var key in result){
plantvalueMap.push({key: key, value: result[key]});
}
component.set("v.plantMap", plantvalueMap);
}
});
$A.enqueueAction(action);
},
=====================================================================
Server side Controller
@AuraEnabled //get Plant Picklist Values from custom meta data
public static Map<String, String> getPlant(){
Map<String, String> options = new Map<String, String>();
//get Plant type Field Describe
Schema.DescribeFieldResult fieldResult = Plant_Setting__mdt.Plant_Type__c.getDescribe();
//get plant Picklist Values
List<Schema.PicklistEntry> pList = fieldResult.getPicklistValues();
for (Schema.PicklistEntry p: pList) {
//Put Picklist Value & Label in Map
options.put(p.getValue(), p.getLabel());
}
system.debug('picklist values: '+options);
return options;
}
No comments:
Post a Comment