首页 > 其他分享 >Salesforce LWC学习(四十六) 自定义Datatable实现cell onclick功能

Salesforce LWC学习(四十六) 自定义Datatable实现cell onclick功能

时间:2023-12-05 17:25:39浏览次数:31  
标签:LightningDatatable Salesforce 自定义 value label cell import message type

本篇参考: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的效果从而进行了更好的扩展。篇中有错误地方欢迎指出,有不懂欢迎留言。

标签:LightningDatatable,Salesforce,自定义,value,label,cell,import,message,type
From: https://www.cnblogs.com/zero-zyq/p/17877102.html

相关文章

  • uniapp开发——创建安卓自定义调试基座,实现热更新调用原生功能
    一.生成本地包:选中项目,头部菜单栏“发行"-"生成本地打包App资源"打包成功二.打包完成,复制App资源包到安卓studio项目中uniapp项目根目录下,找到unpackage目录,打开resources目录,复制下边的_UNI_XXXXX格式的目录三.把App资源包粘贴到Androidstudio项目中,目录路径为:app-sr......
  • 10、KVM自定义网桥实现虚拟机的内部通讯
    摘自:https://blog.51cto.com/mfc001/6411430基于自定义网桥的虚拟网络两个宿主机:自定义网桥virbr1新增eth1网卡:VMnet1仅主机模式(配好之后,两台宿主机上的虚拟机就可以通过VMnet1相互通信了)原wth0网卡:VMnet8NAT模式和外网相连   ubuntu宿主机第......
  • Power BI Report Server自定义Form登录
    一、条件1、windowsserver主机一台,我是windowsserver2019(当然windows10或者10月份5日更新的windows11也是可以行的)。 2、SQLSever,我用的是SQLServer2019。3、PowerBIReportServer默认位置安装(默认位置:C:\ProgramFiles\MicrosoftPowerBIReportServer)。......
  • echarts中自定义tooltip的换行问题
    echarts中自定义tooltip的换行问题使用extraCssText属性 在官网文档中描述tooltip:{trigger:'item',show:true,formatter:'{b}:{c}({d}%)',extraCssText:'max-width:200px;white-space:pre-wrap;wor......
  • 98、swift--- tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPat
    作用:复用cell.可以用标识符从表视图中获得可重用单元格.for:indexPath通过指定单元格位置获得可重用单元格,不需要判断.用于dequeue(出队)一个可复用的cell,用于在UITableView或UICollectionView中显示。这个方法接收两个参数:withIdentifier:一个字符串,表示要dequeue的......
  • 五、自定义组件
    1.创建自定义组件在ArkUI中,UI显示的内容均为组件,由框架直接提供的称为系统组件,由开发者定义的称为自定义组件。在进行UI界面开发时,通常不是简单的将系统组件进行组合使用,而是需要考虑代码可复用性、业务逻辑与UI分离,后续版本演进等因素。因此,将UI和部分业务逻辑封装成自定义组件......
  • 滚动条自定义样式
    .ant-table-content{&::-webkit-scrollbar{height:10px!important;}&::-webkit-scrollbar-thumb{border-radius:0!important;}&::-webkit......
  • php8自定义扩展
    1:进入php源码目录下的ext.如/usr/local/php-8/ext2.生成自定义扩展的名字phpext_skel.php--extpython3.撰写函数原型,编辑python.stub.php3.1默认是test1,test2<?php/**@generate-function-entries*/functionall(array$arr):bool{}function......
  • Spring实践之自定义命名空间并解析
    自定义一个命名空间1、新建一个空项目,在resources/META-INF目录下新建一个spring.handlers文件,内容如下:http\://open.harvey.com/schema/dog=com.harvey.open.annotation.spring.DogNamespaceHandler文件内容为一个键值对。key为自定义命名空间:http://open.harvey.com/sc......
  • 自定义精美商品分类列表组件 侧边栏商品分类组件 category组件(适配vue3)
    随着技术的发展,开发的复杂度也越来越高,传统开发方式将一个系统做成了整块应用,经常出现的情况就是一个小小的改动或者一个小功能的增加可能会引起整体逻辑的修改,造成牵一发而动全身。通过组件化开发,可以有效实现单独开发,单独维护,而且他们之间可以随意的进行组合。大大提升开发效率......