salesforce lightning零基础学习(十五) 公用组件之 获取表字段的Picklist(多语言)
此篇参考:salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type)
我们在lightning中在前台会经常碰到获取picklist的values然后使用select option进行渲染成下拉列表,此篇用于实现针对指定的sObject以及fieldName(Picklist类型)获取此字段对应的所有可用的values的公用组件。因为不同的record type可能设置不同的picklist values,所以还有另外的参数设置针对指定的record type developer name去获取指定的record type对应的Picklist values.
一. PicklistService公用组件声明实现
Common_PicklistController.cls:有三个形参,其中objectName以及fieldName是必填参数,recordTypeDeveloperName为可选参数。
public without sharing class Common_PicklistController { @AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPicklistValues(String objectName, String fieldName,String recordTypeDeveloperName) {
//1. use object and field name get DescribeFieldResult and also get all the picklist values
List<Schema.PicklistEntry> allPicklistValuesByField;
try {
List<Schema.DescribeSobjectResult> objDescriptions = Schema.describeSObjects(new List<String>{objectName});
Schema.SObjectField field = objDescriptions[0].fields.getMap().get(fieldName);
Schema.DescribeFieldResult fieldDescribeResult = field.getDescribe();
allPicklistValuesByField = fieldDescribeResult.getPicklistValues();
} catch (Exception e) {
throw new AuraHandledException('Failed to retrieve values : '+ objectName +'.'+ fieldName +': '+ e.getMessage());
} //2. get all active field name -> label map
List<Map<String,String>> activeOptionMapList = new List<Map<String,String>>();
Map<String,String> optionValue2LabelMap = new Map<String,String>();
List<String> optionValueList;
for(Schema.PicklistEntry entry : allPicklistValuesByField) {
if (entry.isActive()) {
System.debug(LoggingLevel.INFO, '*** entry: ' + JSON.serialize(entry));
optionValue2LabelMap.put(entry.getValue(), entry.getLabel());
}
} //3. generate list with option value(with/without record type)
if(String.isNotBlank(recordTypeDeveloperName)) {
optionValueList = PicklistDescriber.describe(objectName,recordTypeDeveloperName,fieldName);
} else {
optionValueList = new List<String>(optionValue2LabelMap.keySet());
} //4. generate and format result
if(optionValueList != null) {
for(String option : optionValueList) {
String optionLabel = optionValue2LabelMap.get(option);
Map<String,String> optionDataMap = new Map<String,String>();
optionDataMap.put('value',option);
optionDataMap.put('label', optionLabel);
activeOptionMapList.add(optionDataMap);
}
} return activeOptionMapList;
}
}
Common_PicklistService.cmp:声明了getPicklistInfo方法,有以下三个主要参数.objectName对应sObject的API名字,fieldName对应的此sObject中的Picklist类型的字段,recordTypeDeveloperName对应这个sObject的record type的developer name
<aura:component access="global" controller="Common_PicklistController">
<aura:method access="global" name="getPicklistInfo" description="Retrieve active picklist values and labels mapping with(without) record type" action="{!c.getPicklistInfoAction}">
<aura:attribute type="String" name="objectName" required="true" description="Object name"/>
<aura:attribute type="String" name="fieldName" required="true" description="Field name"/>
<aura:attribute type="String" name="recordTypeDeveloperName" description="record type developer name"/>
<aura:attribute type="Function" name="callback" required="true" description="Callback function that returns the picklist values and labels mapping as [{value: String, label: String}]"/>
</aura:method>
</aura:component>
Common_PicklistServiceController.js: 获取传递过来的参数,调用后台方法并对结果放在callback中。
({
getPicklistInfoAction : function(component, event, helper) {
const params = event.getParam('arguments');
const action = component.get('c.getPicklistValueList');
action.setParams({
objectName : params.objectName,
fieldName : params.fieldName,
recordTypeDeveloperName : params.recordTypeDeveloperName
});
action.setCallback(this, function(response) {
const state = response.getState();
if (state === 'SUCCESS') {
params.callback(response.getReturnValue());
} else if (state === 'ERROR') {
console.error('failed to retrieve picklist values for '+ params.objectName +'.'+ params.fieldName);
const errors = response.getError();
if (errors) {
console.error(JSON.stringify(errors));
} else {
console.error('Unknown error');
}
}
}); $A.enqueueAction(action);
}
})
二. 公用组件调用
上面介绍了公用组件以后,下面的demo是如何调用。
SimplePicklistDemo引入Common_PicklistService,设置aura:id用于后期获取到此component,从而调用方法
<aura:component implements="flexipage:availableForAllPageTypes">
<!-- include common picklist service component -->
<c:Common_PicklistService aura:id="service"/>
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <aura:attribute name="accountTypeList" type="List"/>
<aura:attribute name="accountTypeListByRecordType" type="List"/> <lightning:layout verticalAlign="center" class="x-large">
<lightning:layoutItem flexibility="auto" padding="around-small">
<lightning:select label="account type">
<aura:iteration items="{!v.accountTypeList}" var="type">
<option value="{!type.value}" text="{!type.label}"></option>
</aura:iteration>
</lightning:select>
</lightning:layoutItem> <lightning:layoutItem flexibility="auto" padding="around-small">
<lightning:select label="account type with record type">
<aura:iteration items="{!v.accountTypeListByRecordType}" var="type">
<option value="{!type.value}" text="{!type.label}"></option>
</aura:iteration>
</lightning:select>
</lightning:layoutItem>
</lightning:layout> </aura:component>
SimplePicklistDemoController.js:初始化方法用于获取到公用组件component然后获取Account的type的values,第一个是获取所有的values/labels,第二个是获取指定record type的values/labels。
({
doInit : function(component, event, helper) {
const service = component.find('service');
service.getPicklistInfo('Account','type','',function(result) {
component.set('v.accountTypeList', result);
}); service.getPicklistInfo('Account','type','Business_Account',function(result) {
component.set('v.accountTypeListByRecordType',result);
});
}
})
三.效果展示:
1. account type的展示方式
2. account type with record type的展示方式。
总结:篇中介绍了Picklist values针对with/without record type的公用组件的使用,感兴趣的可以进行优化,篇中有错误的欢迎指出,有不懂的欢迎留言。
salesforce lightning零基础学习(十五) 公用组件之 获取表字段的Picklist(多语言)的更多相关文章
- salesforce lightning零基础学习(十六) 公用组件之 获取字段label信息
我们做的项目好多都是多语言的项目,针对不同国家需要展示不同的语言的标题.我们在classic中的VF page可谓是得心应手,因为系统中已经封装好了我们可以直接在VF获取label/api name等 ...
- salesforce lightning零基础学习(十四) Toast 浅入浅出
本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:showToast/specification h ...
- salesforce lightning零基础学习(十二) 自定义Lookup组件的实现
本篇参考:http://sfdcmonkey.com/2017/01/07/custom-lookup-lightning-component/,在参考的demo中进行了简单的改动和优化. 我们在ht ...
- salesforce lightning零基础学习(十三) 自定义Lookup组件(Single & Multiple)
上一篇简单的介绍了自定义的Lookup单选的组件,功能为通过引用组件Attribute传递相关的sObject Name,捕捉用户输入的信息,从而实现搜索的功能. 我们做项目的时候,可能要从多个表中获 ...
- salesforce lightning零基础学习(十) Aura Js 浅谈三: $A、Action、Util篇
前两篇分别介绍了Component类以及Event类,此篇将会说一下 $A , Action以及 Util. 一. Action Action类通常用于和apex后台交互,设置参数,调用后台以及对结 ...
- salesforce lightning零基础学习(十七) 实现上传 Excel解析其内容
本篇参考: https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader https://github.com/SheetJS/sheetjs ...
- salesforce lightning零基础学习(二) lightning 知识简单介绍----lightning事件驱动模型
看此篇博客前或者后,看一下trailhead可以加深印象以及理解的更好:https://trailhead.salesforce.com/modules/lex_dev_lc_basics 做过cla ...
- salesforce lightning零基础学习(一) lightning简单介绍以及org开启lightning
lightning对于开发salesforce人员来说并不陌生,即使没有做过lightning开发,这个名字肯定也是耳熟能详.原来的博客基本都是基于classic基于配置以及开发,后期博客会以ligh ...
- salesforce lightning零基础学习(五) 事件阶段(component events phase)
上一篇介绍了lightning component events的简单介绍.此篇针对上一篇进行深入,主要讲的内容为component event中的阶段(Phase). 一. 阶段(Phase)的概念 ...
随机推荐
- rabbitmq学习-如何安装rabbitmq
学习当然还是需要看官网地址的哈 官网地址 你可能会说老铁,看不懂英文咋办?我只能说各大翻译软件以及广大网友总有一款是你喜欢的 广大网友翻译的 中文文档 什么是rabbitmq? rabbitmq (R ...
- GStreamer基础教程11 - 与QT集成
摘要 通常我们的播放引擎需要和GUI进行集成,在使用GStreamer时,GStreamre会负责媒体的播放及控制,GUI会负责处理用户的交互操作以及创建显示的窗口.本例中我们将结合QT介绍如何指定G ...
- 前后端hosts配置访问问题解决思路
问题背景:前后端分离情况下后端开发测试需要配置hosts,有此问题的人员有RD,QA,PM,User 测试环境由于用户使用这种配置导致无法使用线上系统发起单据影响用户使用,同时让用户误以为系统出问题而 ...
- Redis5源码解析-Sentinel
简单的概念就不解释.基于Redis5.0.5 从Sentinel主函数触发 在sentinel.c文件的最后面可以发现sentinelTimer函数,这个就是Sentinel的主函数,sentinel ...
- 防抖(debounce)和 节流(throttling)
防抖(debounce)和 节流(throttling) 1.防抖和节流出现的原因 防抖和节流是针对响应跟不上触发频率这类问题的两种解决方案. 在给DOM绑定事件时,有些事件我们是无法控制触发频率的. ...
- if __name__ == "__main__" 的作用
作用:当模块被直接运行时,以下代码块将被运行,当模块是被导入时,代码块不被运行. 例子: # file one.py def func(): print("func() in one.py& ...
- Java基本数据类型的传值
传递值: 说明:标题其实说法是错误的.Java中只有值传递,没有引用传递. ... ... //定义了一个改变参数值的函数 public static void changeValue(int x) ...
- JVM性能调优详解
前面我们学习了整个JVM系列,最终目标的不仅仅是了解JVM的基础知识,也是为了进行JVM性能调优做准备.这篇文章带领大家学习JVM性能调优的知识. 性能调优 性能调优包含多个层次,比如:架构调优.代码 ...
- javascript中判断数据类型
编写javascript代码的时候常常要判断变量,字面量的类型,可以用typeof,instanceof,Array.isArray(),等方法,究竟哪一种最方便,最实用,最省心呢?本问探讨这个问题. ...
- php mkdir不能创建文件夹的原因
php mkdir不能创建文件夹的原因 1 权限问题2 open_basedir设置问题 参考方法http://newmiracle.cn/?p=2896