vxe-table编辑单元格动态插槽slot的使用

2024-03-02 17:52

本文主要是介绍vxe-table编辑单元格动态插槽slot的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

业务场景:表格中只有特定某一行的的单元格可以编辑,列很多,为每个列写个插槽要写很多重复代码,所以这里使用动态插槽,简化代码量。显示编辑图标,点击编辑图标隐藏。失去焦点保存调后台接口。

解决办法:

1、后端返回的数据里可以编辑的行数据添加属性 edit: true;不可编辑的行数据里添加属性 edit: false;

2、把列数组里的插槽和field提取出来为循环使用做准备,如果直接使用导入进来的columns无法显示,所以需要处理后使用。

列名文件示例columns.js

export const columns1 = [{title: '名称',field: "heatSourceName",align: 'left',width: "160",slots: { header: 'header_heatSourceName', default: '_heatSourceName' },},{title: "日流量(t/h)",field: "supTemp",width: "140",align: 'right',sortable: false,editRender: { autofocus: '.vxe-input--inner' },slots: { default: '_supTemp', edit: 'edit_supTemp' },},{title: "日热量(GJ/h)",field: "supPres",width: "140",align: 'right',sortable: false,editRender: { autofocus: '.vxe-input--inner' },slots: { default: '_supPres', edit: 'edit_supPres' },},{title: "日压力(Mpa)",field: "instFlowSup",width: "160",align: 'right',sortable: false,editRender: { autofocus: '.vxe-input--inner' },slots: { default: '_instFlowSup', edit: 'edit_instFlowSup' },}, ...]

HTML写法

<vxe-grid ref="xGrid1" v-bind="gridOptions1" :span-method="spanMethods"><template #header_heatSourceName><div class="first-col"><div class="first-col-top">指标</div><div class="first-col-bottom">热源</div></div></template><!--  分割线,动态插槽写法   --><template v-for="item in defaultSlots1" :key="item.field" v-slot:[item.slot]="{ row }"><div style="display: flex; align-items: center;`justify-content: ${row[item.field] === 'heatSourceName' ? flex-start : flex-end}`"v-if="row.edit && timeInfo === timeInfo1"><i class="vxe-cell--edit-icon vxe-icon-edit" style="margin-right: 5px;"></i>{{ row[item.field] }}</div><div v-else>{{ row[item.field] }}</div></template><template v-for="item in editSlots1" :key="item.field" v-slot:[item.slot]="{ row }"><div v-if="row.edit && timeInfo === timeInfo1"><vxe-input v-model="row[item.field]" type="number" :min="0" :max="99999999"></vxe-input></div><div v-else>{{ row[item.field] }}</div></template><!--  分割线,下面为常规写法   --><!-- <template #_supTemp="{ row }"><div style="display: flex; align-items: center;justify-content: flex-end;"v-if="row.edit && timeInfo === timeInfo1"><i class="vxe-cell--edit-icon vxe-icon-edit" style="margin-right: 5px;"></i>{{ row.supTemp }}</div><div v-else>{{ row.supTemp }}</div></template><template #edit_supTemp="{ row }"><div v-if="row.edit && timeInfo === timeInfo1"><vxe-input v-model="row.supTemp" type="number" :min="0" :max="99999999"></vxe-input></div><div v-else>{{ row.supTemp }}</div></template><template #_supPres="{ row }"><div style="display: flex; align-items: center;justify-content: flex-end;"v-if="row.edit && timeInfo === timeInfo1"><i class="vxe-cell--edit-icon vxe-icon-edit" style="margin-right: 5px;"></i>{{ row.supPres }}</div><div v-else>{{ row.supPres }}</div></template><template #edit_supPres="{ row }"><div v-if="row.edit && timeInfo === timeInfo1"><vxe-input v-model="row.supPres" type="number" :min="0" :max="99999999"></vxe-input></div><div v-else>{{ row.supPres }}</div></template><template #_waterCnp="{ row }"><div style="display: flex; align-items: center;justify-content: flex-end;"v-if="row.edit && timeInfo === timeInfo1"><i class="vxe-cell--edit-icon vxe-icon-edit" style="margin-right: 5px;"></i>{{ row.waterCnp }}</div><div v-else>{{ row.waterCnp }}</div></template><template #edit_waterCnp="{ row }"><div v-if="row.edit && timeInfo === timeInfo1"><vxe-input v-model="row.waterCnp" type="number" :min="0" :max="99999999"></vxe-input></div><div v-else>{{ row.waterCnp }}</div></template> --></vxe-grid>

 Js写法

 

<script>
import {defineComponent,ref,reactive,toRefs,computed,watch,onMounted,nextTick,
} from 'vue'
import { columns1 } from './columns.js';
import moment from 'moment'
import { useAppStoreWithOut } from '@/store/modules/app'export default defineComponent({setup() {const appStore = useAppStoreWithOut();const state = reactive({timeInfo: moment(appStore.getSysTime).subtract(1, 'day').format('YYYY年MM月DD日'),timeInfo1: moment(appStore.getSysTime).subtract(1, 'day').format('YYYY年MM月DD日'),gridOptions1: {border: true,height: '100%',showFooter: false,showOverflow: true,'column-config': { resizable: false },'edit-config': {trigger: 'click', mode: 'cell', showIcon: false },'scroll-y': { enable: true, mode: 'wheel' },columns: computed(() => {// 拼接序号列return columns1;}),data: computed(() => {let data = [{"heatSourceName": "Leo源","supTemp": null,"supPres": null,"instFlowSup": null,"retTemp": null,"retPres": null,"muwInstFlow": null,"heatCnp": null,"waterCnp": null,"muwaccFlow": null,"accHeat": null},{"heatSourceName": "晋源","supTemp": "86.89","supPres": null,"instFlowSup": "1028.31","retTemp": "41.51","retPres": "0.111","muwInstFlow": "514.64","heatCnp": "7923.92","waterCnp": "396.58","muwaccFlow": "29207293.83","accHeat": "680.0334"},{"heatSourceName": "龙山","supTemp": "86.90","supPres": null,"instFlowSup": "514.24","retTemp": "41.50","retPres": null,"muwInstFlow": "515.29","heatCnp": "3960.97","waterCnp": "395.64","muwaccFlow": "1148264.05","accHeat": "166.2449"}]data.forEach((item, index) => {if (index === 1 || index === 2) {item.edit = true} else {item.edit = false}});return data}),},// 合并单元格方法,这里只合并第一行和第二行spanMethods({ row, $rowIndex, column, data }) {let fields = ["retTemp"]let cellValue = row[column.property]if ($rowIndex == 2 || $rowIndex == 3) {if (cellValue && fields.includes(column.property)) {let prevRow = data[$rowIndex - 1]let nextRow = data[$rowIndex + 1]if (prevRow && prevRow[column.property] === cellValue) {return { rowspan: 0, colspan: 0 }} else {let countRowspan = 1while (nextRow && nextRow[column.property] === cellValue) {nextRow = data[++countRowspan + $rowIndex]}if (countRowspan > 1) {return { rowspan: countRowspan, colspan: 1 }}}}}})// 提取默认插槽const defaultSlots1 = computed(() => {return columns1.map((column, index) => {return {slot: column.slots.default,field: column.field,}})})// 提取编辑插槽const editSlots1 = computed(() => {return columns1.map((column, index) => {return {slot: column.slots.edit,field: column.field}})})}return {...toRefs(state),   defaultSlots1,editSlots1,}},})
</script>

 

这篇关于vxe-table编辑单元格动态插槽slot的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/766949

相关文章

Conda与Python venv虚拟环境的区别与使用方法详解

《Conda与Pythonvenv虚拟环境的区别与使用方法详解》随着Python社区的成长,虚拟环境的概念和技术也在不断发展,:本文主要介绍Conda与Pythonvenv虚拟环境的区别与使用... 目录前言一、Conda 与 python venv 的核心区别1. Conda 的特点2. Python v

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

C#中Guid类使用小结

《C#中Guid类使用小结》本文主要介绍了C#中Guid类用于生成和操作128位的唯一标识符,用于数据库主键及分布式系统,支持通过NewGuid、Parse等方法生成,感兴趣的可以了解一下... 目录前言一、什么是 Guid二、生成 Guid1. 使用 Guid.NewGuid() 方法2. 从字符串创建

Python使用python-can实现合并BLF文件

《Python使用python-can实现合并BLF文件》python-can库是Python生态中专注于CAN总线通信与数据处理的强大工具,本文将使用python-can为BLF文件合并提供高效灵活... 目录一、python-can 库:CAN 数据处理的利器二、BLF 文件合并核心代码解析1. 基础合

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python内置函数之classmethod函数使用详解

《Python内置函数之classmethod函数使用详解》:本文主要介绍Python内置函数之classmethod函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 类方法定义与基本语法2. 类方法 vs 实例方法 vs 静态方法3. 核心特性与用法(1编程客

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

使用Python实现可恢复式多线程下载器

《使用Python实现可恢复式多线程下载器》在数字时代,大文件下载已成为日常操作,本文将手把手教你用Python打造专业级下载器,实现断点续传,多线程加速,速度限制等功能,感兴趣的小伙伴可以了解下... 目录一、智能续传:从崩溃边缘抢救进度二、多线程加速:榨干网络带宽三、速度控制:做网络的好邻居四、终端交互

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四