Scenario: I have to create a picklist in Lightning, the values of which will be fetched from an already defined custom setting. The Apex method will be returning a map of values.
Define a method in Apex class SampleController which will return a map of values.
Now in client side controller, create a new array optionsList. Add the values to this new array from the response object returned.
Once the array is populated with the values, using aura:iteration tag populate the picklist values as below:
Define a method in Apex class SampleController which will return a map of values.
@AuraEnabled
public static Map<String, String> getselectOptions() {
Map<String, CustomSettingName__c> values = CustomSettingName__c.getAll();
Map<String, String> options = new Map<String, String>();
for (String key: values.keyset()) {
options.put(key, values.get(key).CustomSetting_Column__c);
}
return options;
}
Now in client side controller, create a new array optionsList. Add the values to this new array from the response object returned.
var optionsList = [];
for (var key in response) {
if (response.hasOwnProperty(key)) {
optionsList.push({value: key, label: response[key]});
}
};
component.set('v.optionsList', optionsList);
Once the array is populated with the values, using aura:iteration tag populate the picklist values as below:
<aura:component controller="SampleController">
<aura:attribute name="optionsList" type="List"/>
<lightning:select name="selectItem" label="text" onchange="{!c.checkOptionValue}" value="{!v.partnerTypeSelected}" >
<aura:iteration items="{!v.optionsList}" var="option">
<option value="{!option.value}" text="{!option.label}"
selected="{!option.selected}">
</option>
</aura:iteration>
</lightning:select>
</aura:component>
it was useful.
ReplyDeleteCould you please let me know what is partnerTypeSelected. Is it attribute type as string?
ReplyDelete