Salesforce LWC学习(四十六) 自定义Datatable实现cell onclick功能
本篇参考:https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable
背景:我们有时会有这种类似的需求,显示Account列表,Account列除了需要显示Account信息,还需要显示其他的内容,比如当前Account有多少Opportunity,有多少Contact数量。点击数量信息,可以显示详细的信息。 这种需求就需要datatable的某个单元格允许点击,点击以后进行某些逻辑,比如popup。因为标准的datatable无法实现功能,仅支持 onrowaction,所以我们继承LightningDatatable来自定义。
步骤如下:
1. 继承 LightningDatatable,创建template;
2. template中通过a标签,添加 onclick事件;
3. 针对onclick的handler,通过事件/广播方式传递给上层组件,从而上层事件来处理。(dispatchEvent测试以后发现不可用,所以demo中以message channel作为最终的呈现)
具体实施
filterChange.messageChannel-meta.xml: 设置message channel以及创建需要的变量,不同的需求有不同的变量,可以基于自己的需求来看。
<?xml version="1.0" encoding="UTF-8"?>
<LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata">
<isExposed>true</isExposed>
<lightningMessageFields>
<description>Record Id</description>
<fieldName>dataId</fieldName>
</lightningMessageFields>
<lightningMessageFields>
<description>Record Type</description>
<fieldName>dataType</fieldName>
</lightningMessageFields>
<masterLabel>Filters Change Message Channel</masterLabel>
</LightningMessageChannel>
datatableWithClick.js: 用于继承LightningDatatable,设置自定义type:clickrow,template通过 onclickRow.html来操作。
import { LightningElement, track, wire } from 'lwc';
import LightningDatatable from 'lightning/datatable';
import onclickRow from './onclickRow.html';
export default class datatableWithClick extends LightningDatatable {
static customTypes = {
clickrow: {
template: onclickRow
}
};
}
onclickRow.html: 和datatableWithClick在同一个目录下,UI通过datatable-click-template来渲染,并且将参数值传递给param
<template>
<c-datatable-click-template
param={value}
>
</c-datatable-click-template>
</template>
datatableClickTemplate.html:a标签显示内容,然后设置 onclick事件
<template>
<a onclick={handleClickAction}>{label}</a>
</template>
datatableClickTemplate.js: 这里通过传递的value通过指定的格式来拆分,我们这里通过分号,实际可以基于自己的需求来弄。当点击以后,通过message channel发布事件
import { LightningElement, track, wire,api } from 'lwc';
import { publish, MessageContext } from 'lightning/messageService';
import FILTERSCHANGEMC from '@salesforce/messageChannel/filterChange__c';
export default class datatableClickTemplate extends LightningElement {
@wire(MessageContext)
messageContext;
@track label;
@track recordId;
@track type;
@api set param(value) {
console.log(value);
if(value && value.includes(';')) {
this.label = value.split(';')[0];
this.recordId = value.split(';')[1];
this.type = value.split(';')[2];
}
}
get param() {
return label;
}
handleClickAction(event) {
const filters = {
dataId: this.recordId,
dataType: this.type
};
publish(this.messageContext, FILTERSCHANGEMC, filters);
}
}
datatableSample.html: 调用 datatableWithClick组件
<template>
<c-datatable-with-click
data={data}
columns={columns}
key-field="id">
</c-datatable-with-click>
</template>
datatableSample.js: 设置初始值以及订阅发布的广播,订阅后执行handleFilterChange方法。
import { LightningElement,wire } from 'lwc';
import {
subscribe,
unsubscribe,
MessageContext
} from 'lightning/messageService';
import FILTERSCHANGEMC from '@salesforce/messageChannel/filterChange__c';
const columns = [
{label: 'Account name', fieldName: 'accountName', type: 'text'},
{
type: "clickrow",
fieldName: "numberOfOppty",
label: "Opportunity Count"
}
];
const data = [{
id: 'a',
accountName: 'Cloudhub',
numberOfOppty: '2;a;testRecordType'
},
{
id: 'b',
accountName: 'Quip',
numberOfOppty: '5;b;testOtherRT'
}];
export default class datatableSample extends LightningElement {
data = data;
columns = columns;
@wire(MessageContext)
messageContext;
connectedCallback() {
this.subscription = subscribe(
this.messageContext,
FILTERSCHANGEMC,
(message) => {
this.handleFilterChange(message);
}
);
}
disconnectedCallback() {
unsubscribe(this.subscription);
this.subscription = null;
}
handleFilterChange(filters) {
console.log('execute');
console.log(filters.dataId);
console.log(filters.dataType);
}
}
效果展示:

系统渲染的元素如下图所示,demo中使用的message channel,如果使用dispatchEvent,即使设置了bubble等于true, datatable-sample仍然无法handle,没有进行深入研究。

总结:篇中通过继承LightningDatatable实现了cell click的效果从而进行了更好的扩展。篇中有错误地方欢迎指出,有不懂欢迎留言。
Salesforce LWC学习(四十六) 自定义Datatable实现cell onclick功能的更多相关文章
- Salesforce LWC学习(四十) dynamic interaction 浅入浅出
本篇参考: Configure a Component for Dynamic Interactions in the Lightning App Builder - Salesforce Light ...
- Salesforce LWC学习(四十二) getRecordNotifyChange已弃用
本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_get ...
- Salesforce LWC学习(二十六) 简单知识总结篇三
首先本篇感谢长源edward老哥的大力帮助. 背景:我们在前端开发的时候,经常会用到输入框,并且对这个输入框设置 required或者其他的验证,当不满足条件时使用自定义的UI或者使用标准的 inpu ...
- Salesforce LWC学习(三十六) Quick Action 支持选择 LWC了
本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_quick_act ...
- Salesforce LWC学习(四十) datatable的dynamic action的小坑浅谈
本篇参考:https://developer.salesforce.com/docs/component-library/bundle/lightning-datatable/documentatio ...
- Salesforce LWC学习(三十九) lwc下quick action的recordId的问题和解决方案
本篇参考: https://developer.salesforce.com/docs/component-library/bundle/force:hasRecordId/documentation ...
- Salesforce LWC学习(三十) lwc superbadge项目实现
本篇参考:https://trailhead.salesforce.com/content/learn/superbadges/superbadge_lwc_specialist 我们做lwc的学习时 ...
- Java开发学习(四十六)----MyBatisPlus新增语句之id生成策略控制及其简化配置
在前面有一篇博客:Java开发学习(四十一)----MyBatisPlus标准数据层(增删查改分页)开发,我们在新增的时候留了一个问题,就是新增成功后,主键ID是一个很长串的内容. 我们更想要的是按照 ...
- Salesforce LWC学习(二十四) Array.sort 浅谈
本篇参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort sal ...
- Salesforce LWC学习(三十四) 如何更改标准组件的相关属性信息
本篇参考: https://www.cnblogs.com/zero-zyq/p/14548676.html https://www.lightningdesignsystem.com/platfor ...
随机推荐
- 《Pro Git》Git基础笔记
获取Git仓库 在已存在目录中初始化仓库 $ git init 该命令会创建一个名为.git的隐藏文件. 克隆现有的仓库 $ git clone <url> # 例如 git clone ...
- 快速解决 const 与 typedef 类型组合时 ,const修饰谁的问题
C++使用typedef 给复合类型定义别名时,与const结合会产生看似"令人困惑"的类型推定,例如 typedef char* pstring; const pstring c ...
- NOIP 2022 VP游记
总结:挂大分. HA NOIP没初中生的份,VP. CSP-S 图论专场 NOIP 数数专场. CCF 我服你. T1 看完之后,感觉不难,瞎搞了 40min+,过了大样例. 对拍不会写. T2 猜不 ...
- 【page cache】简介
目录 page cache 直接 IO 与 缓存 IO Linux IO 栈 Linux 中的具体实现 相关结构体 超级块 super_block 索引节点 inode 文件 file 目录项 den ...
- Adobe全家桶PS、PR、AU等2022正版永久有效,无需破解直接安装就能用
[Adobe全家桶]已经亲测绝对好用,下载地址: 关注我的wx公众号"奋斗在IT"回复1013获取下载地址.
- 利用别名简化进入docker容器数据库的操作
之前研究docker和数据库的交互,越发对docker这个东西喜爱了.因为平常偶尔会用到各类数据库测试环境验证一些想法,需要进一步简化进入到这些环境的步骤. 比如我现在有三套docker容器数据库测试 ...
- 「codeforces - 868F」Yet Another Minimization Problem
link. 值域分治优化决策单调性 DP 的 trick.朴素做法 trivial,不赘述. 考虑求取一个区间 \([l,r]\) 的 DP 值.先搞定在 \(m=\lfloor\frac{l+r}{ ...
- Python基础——垃圾回收、格式化输入输出、基本运算符、流程控制
文章目录 每日测验 垃圾回收机制详解(了解) 引用计数 标记清除 分代回收 与用户交互 接收用户的输入 字符串的格式化输出 填充与格式化 基本运算符 算数运算符 比较运算符: >.>=.& ...
- Oracle中的substr()函数和INSTR()函数和mysql中substring_index函数字符截取函数用法:计算BOM系数用量拼接字符串*计算值方法
最近一直在研究计算产品BOM的成本系数,将拼接的元件用量拼接后拆分计算是个问题,后来受到大佬在mysql中截取字符串的启发在oracle中以substr和instr实现了 1.以下是我在mysql中 ...
- OSPF常用配置和常用的查看命令
转载请注明出处: 1.启动OSPF进程,进入OSPF视图. [Huawei] ospf [ process-id | Router ID Router ID ] 路由器支持OSPF多进程,进程号是本地 ...