1.通过集合初始化方式
- component中声明一个属性,并在选项列表控件使用
<aura:attribute name="departs1" type="Department__c[]" />
<lightning:select aura:id="D1" label="事业部" class="dynamic" value="{!v.d1Selected}" onchange="{!c.d1Change}">
<option value="">请选择</option>
<aura:iteration items="{!v.departs1}" var="dep">
<option value="{!dep.Id}" label="{!dep.Name}"></option>
</aura:iteration>
</lightning:select>
- JS初始化时从后台获取集合的值,获取后赋值给attribute
js Controller:
init: function(component, event, helper) {
//初始化事业部的值
var depart_action = component.get("c.getD1");
depart_action.setCallback(this, function(response) {
if (response.getState() == "SUCCESS") {
var departs = response.getReturnValue();
if (departs != null && departs.length > 0) {
component.set("v.departs1", departs);
console.log('Get Department1 SUCCESS!!!');
} else {
console.log('Get Department1 Failed!!!');
}
} else {
console.log('Get Department1 Failed: ' + response.getState());
}
});
$A.enqueueAction(depart_action);
}
apex Controller:
//获取D1事业部
@AuraEnabled
public static List<Department__c> getD1(){
return [select Id,Name,Parent_Code__c from Department__c where Is_Active__c = true];
}
2.通过JS添加option方式
component 代码:
<ui:inputSelect label="Status" class="dynamic" aura:id="InputSelectDynamic" value="{!v.leadObj.Status}" required="true"/>
js controller:
doInit : function(component, event, helper) {
var action = component.get("c.getLeadStatus");
var inputsel = component.find("InputSelectDynamic");
var opts=[];
action.setCallback(this, function(a) {
for(var i=0;i< a.getReturnValue().length;i++){
opts.push({"class": "optionClass", label: a.getReturnValue()[i], value: a.getReturnValue()[i]});
}
inputsel.set("v.options", opts);
});
$A.enqueueAction(action);
}
apex:controller:
public static List<String> getLeadStatus(){
List<String> options = new List<String>();
Schema.DescribeFieldResult fieldResult = lead.status.getDescribe();
List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
for (Schema.PicklistEntry f: ple) {
options.add(f.getLabel());
}
return options;}