本篇可参看:

Salesforce LWC学习(六) @salesforce & lightning/ui*Api Reference

salesforce零基础学习(八十七)Apex 中Picklist类型通过Control 字段值获取Dependent List 值

拥有dependence的picklist在项目中经常出现,我们在项目中需要展示级联的picklist展示。Salesforce lwc中可以通过wire adapter + combobox来实现级联,步骤和代码如下。

1. 在Account中声明两个字段,Controlling_Picklist__c以及Master_Picklist__c两个picklist字段,其中Master_Picklist作为主

2. 通过getObjectInfo的wire adapter获取record type等信息,然后通过getPicklistValuesByRecordType可以获取表中所有的picklist的metadata信息。

这里先可以看一下picklist metadata信息如下。我们在第二个链接中可以看到dependence picklist通过valid for进行了关联,所以我们成功的关键也是通过valid for进行了关联。

{
"picklistFieldValues": {
"Controlling_Picklist__c": {
"controllerValues": {
"M2": 1,
"M3": 2,
"M1": 0
},
"values": [
{
"validFor": [
0
],
"label": "M1->D1",
"value": "M1->D1",
"attributes": null
},
{
"validFor": [
0
],
"label": "M1->D2",
"value": "M1->D2",
"attributes": null
},
{
"validFor": [
1
],
"label": "M2->D1",
"value": "M2->D1",
"attributes": null
},
{
"validFor": [
1
],
"label": "M2->D2",
"value": "M2->D2",
"attributes": null
}
],
"defaultValue": null,
"url": "/services/data/v47.0/ui-api/object-info/Account/picklist-values/0120I000000OaFoQAK/Controlling_Picklist__c"
},
"Master_Picklist__c": {
"controllerValues": {},
"values": [
{
"validFor": [],
"label": "M1",
"value": "M1",
"attributes": null
},
{
"validFor": [],
"label": "M2",
"value": "M2",
"attributes": null
},
{
"validFor": [],
"label": "M3",
"value": "M3",
"attributes": null
}
],
"defaultValue": null,
"url": "/services/data/v47.0/ui-api/object-info/Account/picklist-values/0120I000000OaFoQAK/Master_Picklist__c"
}
}
}

代码实现如下:

dependentPicklistInLWC.html:使用combobox展示两个下拉列表,设置相关的事件处理

<template>
<lightning-card title="Custom Dependent Picklist using Lightning Web Components"><br/>
<lightning-layout>
<lightning-layout-item padding="around-small" flexibility='auto' size='6'>
<lightning-combobox label="master picklist"
name="master"
onchange={handleMasterPicklistChange}
options={masterValues}
placeholder="--None--"
value={selectedMasterValue}></lightning-combobox>
</lightning-layout-item>
<lightning-layout-item padding="around-small" flexibility='auto' size='6'>
<lightning-combobox label="controlling picklist"
name="controlling"
onchange={handleControllingPicklistChange}
options={controllingValues}
placeholder="--None--"
value={selectedControllingValue}
></lightning-combobox>
</lightning-layout-item> </lightning-layout>
</lightning-card>
</template>

dependentPicklistInLWC.js:使用getObjectInfo以及getPicklistValuesByRecordType去进行Picklist的select option的构造,这里的关键就是理解好valid for

/* eslint-disable no-console */
import { LightningElement, wire, track } from 'lwc';
import { getPicklistValuesByRecordType } from 'lightning/uiObjectInfoApi';
import { getObjectInfo } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account'; export default class DependentPickListInLWC extends LightningElement { //存储controlling picklist的所有的值
@track masterValues = [];
//存储dependent picklist的所有的值
@track controllingValues = [];
//选择的controlling picklist 的值
@track selectedMasterValue;
//选择的dependent picklist的值
@track selectedControllingValue; @track error;
//用来记录master picklist中的 value -> valid for的列表集合
master2ValidForValues;
//用来记录controlling picklist的value以及valid for等信息的列表集合
controllingValuesWithValidFor = []; // 获取account 的schema info
@wire(getObjectInfo, { objectApiName: ACCOUNT_OBJECT })
objectInfo; // 获取 control picklist的值并且组装dependent picklist
@wire(getPicklistValuesByRecordType, { objectApiName: ACCOUNT_OBJECT, recordTypeId: '$objectInfo.data.defaultRecordTypeId'})
countryPicklistValues({error, data}) {
if(data) {
this.error = null;
let masterOptions = []; data.picklistFieldValues.Master_Picklist__c.values.forEach(key => {
masterOptions.push({
label : key.label,
value: key.value
})
}); this.masterValues = masterOptions; let controllingOptions = []; this.master2ValidForValues = data.picklistFieldValues.Controlling_Picklist__c.controllerValues;
//用来记录controlling picklist的value以及valid for等信息的列表集合 Picklist values
this.controllingValuesWithValidFor = data.picklistFieldValues.Controlling_Picklist__c.values;
this.controllingValuesWithValidFor.forEach(key => {
controllingOptions.push({
label : key.label,
value: key.value
})
}); this.controllingValues = controllingOptions;
}
else if(error) {
this.error = JSON.stringify(error);
}
} handleMasterPicklistChange(event) {
//set selected master Value
this.selectedMasterValue = event.target.value;
this.selectedControllingValue = '';
let controllingList = []; if(this.selectedMasterValue) {
//通过valid for进行mapping,匹配的放进controlling list中
this.controllingValuesWithValidFor.forEach(conValues => {
if(conValues.validFor.some(item => item === this.master2ValidForValues[this.selectedMasterValue])) {
controllingList.push({
label: conValues.label,
value: conValues.value
})
}
}) this.controllingValues = controllingList;
}
} handleControllingPicklistChange(event) {
this.selectedControllingValue = event.target.value;
}
}

效果展示:当选择了左侧的master以后,右侧的controlling值便会动态改变。

当然,我们使用这个大前提是当前的object支持getObjectInfo等wire adapter,如果不支持我们需要通过后台schema方式去搞定,比如Task/Event对象并不支持,我们就没法通过前端搞定,那样就只能通过第二个链接中的方式去从后台去构建进行获取。

总结:篇中主要介绍了可以使用wire adapter情况下dependence picklist的实现。篇中有错误地方欢迎指出,有不懂的欢迎留言。

Salesforce LWC学习(十二) Dependence Picklist实现的更多相关文章

  1. Salesforce LWC学习(十五) Async 以及 Picklist 公用方法的实现

    本篇参考:salesforce 零基础学习(六十二)获取sObject中类型为Picklist的field values(含record type) https://developer.salesfo ...

  2. Salesforce LWC学习(十六) Validity 在form中的使用浅谈

    本篇参考: https://developer.salesforce.com/docs/component-library/bundle/lightning-input/documentation h ...

  3. Salesforce LWC学习(十) 前端处理之 list 处理

    本篇参看:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array list是我们经 ...

  4. Salesforce LWC学习(十八) datatable展示 image

    本篇参看: https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentati ...

  5. Salesforce LWC学习(十四) Continuation进行异步callout获取数据

    本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.apex_continua ...

  6. Salesforce LWC学习(十九) 针对 lightning-input-field的label值重写

    本篇参考: https://salesforcediaries.com/2020/02/24/how-to-override-lightning-input-field-label-in-lightn ...

  7. Salesforce LWC学习(四十) dynamic interaction 浅入浅出

    本篇参考: Configure a Component for Dynamic Interactions in the Lightning App Builder - Salesforce Light ...

  8. Salesforce LWC学习(三十) lwc superbadge项目实现

    本篇参考:https://trailhead.salesforce.com/content/learn/superbadges/superbadge_lwc_specialist 我们做lwc的学习时 ...

  9. Salesforce LWC学习(三十九) lwc下quick action的recordId的问题和解决方案

    本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ...

  10. (转)SpringMVC学习(十二)——SpringMVC中的拦截器

    http://blog.csdn.net/yerenyuan_pku/article/details/72567761 SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter, ...

随机推荐

  1. 侠客行+越女剑 <随笔>

    侠客行:自己提炼剧情是一个很费时费劲的事情,好在剽窃百度百科不算抄袭,而且也足够还原,红字为补充 一向平静祥和的小市镇侯监集上,忽然来了二百多名杀人不眨眼的强盗.镇上乡亲们都熟悉的卖饼老者王老汉,却被 ...

  2. 第二课 如何安装java

    1.三大版本 JDK: Java Development Kit JRE: Java Runtime Environment JVM: JAVA Virtual Machine 2.java开发环境搭 ...

  3. Ubuntu16.04配置网卡

    设置步骤: 1.路由器插电后,电脑使用网线,连接无线路由器任一LAN口,注意两者单独连接,先不要连接宽带网线.打开电脑浏览器,在地址栏输入192.168.100.1. 在路由器的管理界面,输入路由器的 ...

  4. JS中Promise

    Promise的作用: Promise是异步微任务,解决了异步多层嵌套回调的问题,让代码的可读性更高,更容易维护. Promise如何使用: Promise是ES6提供的一个构造函数,可以使用Prom ...

  5. 音标s ed

    1 p /s/ cups  2 t /s/ hats puts3 k /s/ cakes books desks works worked /t/4 f /s/ roofssiz ziz s加其他清辅 ...

  6. OPENSSL 生成RSA公钥、私钥和证书

    在命令窗口执行下列操作. 1)生成RSA私钥: openssl genrsa -out rsa_private_key.pem 2048 生成内容: -----BEGIN RSA PRIVATE KE ...

  7. centos8 安装 spdk

    1. 下载 2.配置 ./configure --enable-debug --disable-tests --without-isal --without-ocf  --with-uring --w ...

  8. FPGA串口 波特率的计数器值

    开发板时钟为50Mhz, t为 20ns; xxx波特率时指每秒传xxx bit字节数据.也就是T=1/xxx; 再用T/t就可以得出波特率的计数周期了: 例如9600:T=1/96000=1.041 ...

  9. mysql锁表原因及解决方法

    mysql锁表原因及解决方法   一.导致锁表的原因 1.锁表发生在insert update .delete 中: 2.锁表的原理是 数据库使用独占式封锁机制,当执行上面的语句时,对表进行锁住,直到 ...

  10. linux 离线安装mysql 配置开机自启动

    系统版本:centos7.8 | mysql版本:5.7.35 安装配置mysql数据库 mysql数据库配置开机自启动 1. 安装配置mysql数据库 mysql版本:5.7.35 点击下载 提取码 ...