Is it possible to get sObjectDescribe in Lightning Components? In my case I need to have all possible values of picklist-field, but filter out the list of options conditionally.
1 Answer
You can do the describe call in the apex class and filter out the value as you want and return those value to lightning controller:
Adding a snap of the apex class method and lightning controller:
Add the following in the Apex class:
public class picklistWrapper {
@AuraEnabled
public String strValue {get;set;}
@AuraEnabled
public String strLabel {get;set;}
public picklistWrapper(String strValue, String strLabel) {
this.strValue = strValue;
this.strLabel = strLabel;
}
}
@AuraEnabled
public static List<picklistWrapper> getPicklistValues() {
List<picklistWrapper> lstPWrapper = new List<picklistWrapper>();
Schema.DescribeFieldResult fieldResult = Account.BillingStateCode.getDescribe();
for(Schema.PicklistEntry f : fieldResult.getPicklistValues()) {
//write your logic to filter the values here
lstPWrapper.add(new picklistWrapper(f.getValue(), f.getLabel()));
}
return lstPWrapper
}
Add the following to the lightning controller:
getInitialData : function(component, event, helper) {
var action = component.get("c.getPicklistValues");
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS") {
var result = response.getReturnValue();
console.log('=====result====',result);//you will get picklist values here
}
})
$A.enqueueAction(action);
}
-
I definitely can, but want to simplify it and do not perform a call to server for this data Commented Jun 22, 2017 at 7:57
-
I don't think there is a way to do that in the lightning component itself Commented Jun 22, 2017 at 8:02
-
Currently you must fetch this metadata via apex. I suggest you use a storable action though. It's otherwise not available on the client. Commented Jun 23, 2017 at 15:30