Skip to content

Instantly share code, notes, and snippets.

@cdcarter
Last active November 28, 2016 17:51
Show Gist options
  • Save cdcarter/bd8d37947b22c3832d02d897d239e143 to your computer and use it in GitHub Desktop.
Save cdcarter/bd8d37947b22c3832d02d897d239e143 to your computer and use it in GitHub Desktop.
ValueSetProvider.cmp
public with sharing class LightningValueSetProvider {
@AuraEnabled
public static List<CustomValue> getValueSetForField(String sObjectName, String fieldName) {
List<CustomValue> options = new List<CustomValue>();
Schema.SObjectField field = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap().get(fieldName);
for (PicklistEntry value : field.getDescribe().getPicklistValues()) {
options.add(new CustomValue(value.getLabel(), value.getValue(), value.isDefaultValue()));
}
return options;
}
public class CustomValue {
@AuraEnabled public String label {get; set;}
@AuraEnabled public String value {get; set;}
@AuraEnabled public Boolean isDefaultValue {get; set;}
public CustomValue(String label, String value, Boolean def) {
this.label = label;
this.value = value;
this.isDefaultValue = def;
}
}
}
<aura:component controller="LightningValueSetProvider" access="public" description="Gets a value set from the server and assigns to an attribute.">
<aura:attribute name="sObjectName" type="String" required="true" access="public"/>
<aura:attribute name="field" type="String" required="true" access="public"/>
<aura:attribute name="values" type="Object[]" access="public" description="The value set."/>
<aura:handler name="init" value="{!this}" action="{!c.loadValues}"/>
</aura:component>
({
loadValues : function(component, event, helper) {
var callback = function(data) {
component.set('v.assignTo', data);
};
helper.cachedAction(component,
'c.getValueSetForField', {
'sObjectName': component.get('v.sObjectName'),
'fieldName': component.get('v.field')
},
callback);
}
})
<aura:component access="public" description="A SLDS styled radio button picklist group.">
<!-- get the picklist -->
<aura:attribute name="sObjectName" type="String" required="true" access="public"/>
<aura:attribute name="field" type="String" required="true" access="public"/>
<aura:attribute name="opts" access="private" type="Object[]" default="[]"/>
<c:valueSetProvider sObjectName="{!v.sObjectName}" field="{!v.field}" values="{!v.opts}"/>
<!-- Selected Value -->
<aura:attribute name="value" type="Object" access="public" default=""/>
<!-- Body -->
<c:sldsRadioButtonGroup name="{!globalId + '_buttongroup'}" options="{!v.opts}" selectedValue="{!v.value}"/>
</aura:component>
({
/**
* CACHEDACTION - quickly run an aura action. like a bawz.
* lovingly stolen and modified from Douglas Ayers
* actionName = the apex controller method to call (e.g. 'c.myMethod' )
* params = JSON object specifying action parameters (e.g. { 'x' : 42 } )
* successCallback = function to call when action completes (e.g. function( response ) { ... } )
* failureCallback = function to call when action fails (e.g. function( response ) { ... } )
*/
cachedAction : function(component, actionName, params, successCallback, failureCallback) {
var action = component.get(actionName);
if(params) {
action.setParams(params);
}
action.setStorable();
action.setCallback(this, function(response) {
if(component.isValid() && response.getState() === 'SUCCESS') {
if(successCallback) {
successCallback(response.getReturnValue());
}
} else {
$A.log(response,'Error calling action "' + actionName + '" with state: ' + response.getState());
if (failureCallback) {
failureCallback(response.getError(), response.getState());
} else {
this.logActionErrors(component, response);
}
}
});
$A.enqueueAction(action);
},
logActionErrors : function(component, response, errors) {
var errors = response.getError();
if (errors) {
for (var i in errors) {
console.error('Error: ' + errors[i].message);
}
} else {
console.error('Unknown error');
console.log(response);
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment