【Vue+ElementUI】Table表格实现自定义表头展示+表头拖拽排序(附源码)

效果图

在这里插入图片描述
在这里插入图片描述

因项目采用的是Vue2,所以这个功能目前采用的是Vue2的写法。
Vue3请自行修改扩展代码;或收藏关注帖子,后续Vue3项目如有用到会在本帖子更新修改。

安装vuedraggable(拖拽插件)

cnpm i vuedraggable

先说用法,下方附全源码

引入自定义表头组件

import indicatorTable from "@/components/indicatorTable/index.vue";

使用:(传参说明已在下方标识)

<indicatorTableref="rois":defaultArr="columns":cardDataProp="cardDataProp"cacheKeyProp="keyROI"@propData="propsTableHander"currenKey="ROT"
/>

props参数说明:(均为必传字段)

// ref:用于调用子组件方法。
// columns:表头数据,例如:
[{prop: "cost_platform",label: "广告金",
}]// cardDataProp:可选表头复选框,列如:
cardDataProp: [{title: "指标", // 每一项的分类title标题,详见第一张效果图checkboxes: [...columns], // columns这个就是上面的一样},
],// cacheKeyProp:储存的key名,名字自定义来,避免缓存的key一样就行,列如:
cacheKeyProp="keyROI"// propData:回调方法,用于更新表头,接受函数,直接表头columns数据 = 参数即可// currenKey:保存的指标key,避免缓存的key一样就行。

页面table使用方法,需用循环:

<el-tablev-loading="loading":data="tableList"border@sort-change="tableSort":height="tableHeight"ref="tableRef"
><el-table-columnv-for="item in columns":prop="item.prop":label="item.label":width="item.width"align="center"sortable="custom":show-overflow-tooltip="true"></el-table-column>
</el-table>

上面表格的参数不用多说了吧,除非你不会前端!

附源码(拿来直接用!只要参数没问题!)

如遇到报错、不显示等问题,一定是参数不对!已自测 无任何报错或警告信息!
如需要Vue3版本,自行开发或私信,有空定会帮助!
新建组件直接复制:

<template><div class="indicator-all-box"><el-popover placement="bottom" width="300" trigger="click"><div class="add-custom-indicator-container"><el-button type="primary" @click="addUserDefinedIndicators">新增自定义指标</el-button><div class="indicator-list"><ul><liv-for="(item, index) in pointerArr":key="index":class="currenPointIndex == index ? 'active-li' : ''"@click="pointClick(item, index)"><div class="flex-indicator-item"><span>{{ item.title }}</span><div class="right-indicator"><iclass="el-icon-edit"@click.stop.prevent="pointIndexHander(item, 'edit')"></i><iclass="el-icon-delete"@click.stop.prevent="pointIndexHander(item, 'delete')"></i></div></div></li></ul></div></div><el-button slot="reference" type="success">自定义指标</el-button></el-popover><!-- 弹窗自定义 --><el-dialog :title="openTitle" :visible.sync="dialogVisible" width="70%"><div class="customize-indicator-data-container"><div class="card-checkbox-content-left"><el-cardv-for="(item, index) in cardData":key="index"class="box-card"><div slot="header" class="clearfix"><span>{{ item.title }}</span><el-checkboxv-model="item.selectedAll"@change="handleSelectAll(item)":indeterminate="isIndeterminate(item)"style="float: right">全选</el-checkbox></div><div class="check-card-item"><el-checkbox-groupref="checkboxGroup"v-model="selectedCheckboxes"><el-checkboxv-for="(checkbox, idx) in item.checkboxes":key="idx":label="checkbox.label">{{ checkbox.label }}</el-checkbox></el-checkbox-group></div></el-card></div><div class="sort-view-dx"><el-divider>排序</el-divider><div class="sort-row"><draggablev-if="selectedCheckboxes.length > 0"v-model="selectedCheckboxes"animation="300"><p v-for="(item, index) in selectedCheckboxes" :key="index">{{ item }}</p></draggable><el-empty v-else></el-empty></div></div></div><span slot="footer" class="dialog-footer"><el-button @click="dialogVisible = false">取 消</el-button><el-button type="primary" @click="addPointerSubmit">确 定</el-button></span></el-dialog></div>
</template><script>
import draggable from "vuedraggable";export default {name: "indicatorTable",components: {draggable,},props: {// 默认指标defaultArr: {type: Array,required: true,},// 可选指标cardDataProp: {type: Array,required: true,},// 存储指标keycacheKeyProp: {type: String,required: true,},// 存储的索引key名currenKey: {type: String,required: true,},},data() {return {// 弹窗showdialogVisible: false,// 全部指标数组cardData: this.cardDataProp,// 勾选指标selectedCheckboxes: [],// 弹框titleopenTitle: "添加",// 下拉指标列表pointerArr: [],// 获取当前编辑itemeditItem: null,// 传出去的prop数组emitArr: null,// 当前选择的指标currenPointIndex: null,};},computed: {Local() {return {get(key) {const value = localStorage.getItem(key);if (value == "[]") {return null;} else {return value !== null ? JSON.parse(value) : null;}},set(key, value) {localStorage.setItem(key, JSON.stringify(value));},remove(key) {localStorage.removeItem(key);},};},// 指标指定label排序sortColumns() {return function (data, sort) {if (data) {return data.sort((a, b) => sort.indexOf(a.label) - sort.indexOf(b.label));}};},// 获取选指标labelfilterCheckbox() {return function (data, isSort = false, sortData) {if (data) {let filteredCheckboxes = [];this.cardData.forEach((item) => {item.checkboxes.forEach((checkbox) => {if (data.arrayCheck.includes(checkbox.label)) {filteredCheckboxes.push(checkbox);}});});// 获取后是否排序if (isSort) {return this.sortColumns(filteredCheckboxes, sortData);} else {return filteredCheckboxes;}}};},},created() {// this.Local.remove("displayType");this.getPointData("init");},methods: {// 存储key索引storeSetCurrentIndex(type = "set") {if (type === "set") {const getIndexObj = this.Local.get("pointIndex") || {};getIndexObj[this.currenKey] = this.currenPointIndex;this.Local.set("pointIndex", getIndexObj);} else {return this.Local.get("pointIndex") || {};}},// 选择当前指标pointClick(row, index) {if (this.currenPointIndex != index) {this.currenPointIndex = index;// 存储当前点击指标indexthis.storeSetCurrentIndex("set");const checkData = this.filterCheckbox(row,true,this.pointerArr[this.currenPointIndex].arrayCheck);this.$emit("propData", checkData);}},// 扩展方法-倍数ROI处理// roiDisposeFn() {//   const getPonit = this.Local.get(this.cacheKeyProp);//   const displayType = this.Local.get("displayType");//   const prointArrItem = this.pointerArr[this.currenPointIndex];//   const updatedArray = prointArrItem.arrayCheck.map((item) => {//     if (//       displayType == 2 &&//       item.startsWith("ROI") &&//       !item.includes("倍数")//     ) {//       return item + "倍数";//     } else if (displayType != 2 && item.includes("倍数")) {//       return item.replace("倍数", "");//     }//     return item;//   });//   const labelCheckBoxAll = this.filterCheckbox({//     arrayCheck: updatedArray,//   }).map((item) => item.label);//   if (prointArrItem.arrayCheck !== labelCheckBoxAll) {//     getPonit[this.currenPointIndex].arrayCheck = labelCheckBoxAll;//     this.Local.set(this.cacheKeyProp, getPonit);//     this.pointerArr[this.currenPointIndex].arrayCheck = labelCheckBoxAll;//   }// },// 获取-更新指标getPointData(type) {const getPonit = this.Local.get(this.cacheKeyProp);if (getPonit) {this.pointerArr = getPonit;this.currenIndexNob();const prointArrItem = this.pointerArr[this.currenPointIndex];this.roiDisposeFn();const checkData = this.filterCheckbox(this.pointerArr[this.currenPointIndex],true,prointArrItem.arrayCheck);if (checkData) {this.$emit("propData", checkData);}} else if (!getPonit && type !== "init") {// 如果是空this.Local.remove(this.cacheKeyProp);this.$emit("propData", []);} else {// 如果默认的if (this.defaultArr && type === "init" && this.pointerArr.length <= 0) {const arrs = JSON.parse(JSON.stringify(this.defaultArr));const labelsArray = arrs.map((item) => item.label);this.currenIndexNob();this.pointerArr.push({title: "默认指标",arrayCheck: labelsArray,});const prointArrItem = this.pointerArr[this.currenPointIndex];const checkData = this.filterCheckbox(prointArrItem,true,labelsArray);this.$emit("propData", checkData);}}},// 编辑-删除指标pointIndexHander(item, type) {if (type === "edit") {this.openTitle = "编辑";this.selectedCheckboxes = item.arrayCheck;this.editItem = item;this.dialogVisible = true;} else {const itemToDelete = this.pointerArr.find((ls) => ls.title === item.title);if (itemToDelete) {const indexToDelete = this.pointerArr.indexOf(itemToDelete);if (indexToDelete > -1) {this.pointerArr.splice(indexToDelete, 1);this.Local.set(this.cacheKeyProp, this.pointerArr);// 删除当前行更新,否则不更新if (indexToDelete === this.currenPointIndex) {this.getPointData();} else {this.currenIndexNob();}}}}},// 全选当前指标handleSelectAll(item) {item.checkboxes.forEach((checkbox) => {const checkboxIndex = this.selectedCheckboxes.indexOf(checkbox.label);if (item.selectedAll && checkboxIndex === -1) {this.selectedCheckboxes.push(checkbox.label);} else if (!item.selectedAll && checkboxIndex !== -1) {this.selectedCheckboxes.splice(checkboxIndex, 1);}});},// 全选状态判断isIndeterminate(item) {const selectedLabels = this.selectedCheckboxes;const allLabels = item.checkboxes.map((checkbox) => checkbox.label);const selectedCount = selectedLabels.filter((label) =>allLabels.includes(label)).length;item.selectedAll = selectedCount === allLabels.length;return selectedCount > 0 && selectedCount < allLabels.length;},// 指定索引currenIndexNob() {const getIndexObj = this.storeSetCurrentIndex("get");this.currenPointIndex = getIndexObj[this.currenKey];if (!this.currenPointIndex) {this.currenPointIndex = 0;} else {if (this.pointerArr.length <= 1) {this.currenPointIndex = 0;} else {this.currenPointIndex = getIndexObj[this.currenKey] || 0;}}this.storeSetCurrentIndex("set");},// 添加指标addPointerSubmit() {this.dialogVisible = false;this.emitArr = this.filterCheckbox({arrayCheck: this.selectedCheckboxes,},true,this.selectedCheckboxes);const dataItem = {title: "",arrayCheck: this.selectedCheckboxes,};if (this.openTitle === "添加") {this.$prompt("请输入指标名", "提示", {confirmButtonText: "确定",cancelButtonText: "取消",closeOnClickModal: false,inputValidator: (value) => {if (!value) {return "不能为空!";}},beforeClose: (action, instance, done) => {if (action === "confirm") {const isDuplicate = this.pointerArr.some((item) => item.title === instance.inputValue);if (isDuplicate) {this.$message.error("已存在相同指标名");return false;} else {done();}} else {done();}},}).then(({ value }) => {dataItem.title = value;if (this.pointerArr && Array.isArray(this.pointerArr)) {const updatedData = [...this.pointerArr, dataItem];this.Local.set(this.cacheKeyProp, updatedData);} else {const newData = [dataItem];this.Local.set(this.cacheKeyProp, newData);}this.$emit("propData", this.emitArr);this.getPointData();});} else {const editIndex = this.pointerArr.findIndex((item) => item.title === this.editItem.title);if (editIndex !== -1) {(dataItem.title = this.editItem.title),(this.pointerArr[editIndex] = dataItem);this.Local.set(this.cacheKeyProp, this.pointerArr);}this.$emit("propData", this.emitArr);}},// 新增自定义指标addUserDefinedIndicators() {this.openTitle = "添加";this.selectedCheckboxes = [];this.dialogVisible = true;},},
};
</script><style lang="scss" scoped>
.indicator-all-box {float: right;margin-right: 5px;
}
.indicator-list {ul {padding: 0;li {padding: 10px 0;border-bottom: 1px solid #e1e1e1;}}.flex-indicator-item {display: flex;justify-content: space-between;padding: 0 15px;.right-indicator {i {padding-left: 10px;display: inline-block;font-size: 16px;cursor: pointer;}}}
}.box-card {margin-bottom: 10px;
}.el-divider {margin: 10px 0;
}.rihgt-all-check {float: right;padding: 3px 0;
}.el-checkbox {margin-bottom: 10px;
}.customize-indicator-data-container {display: flex;min-height: 60vh;.card-checkbox-content-left {max-height: 600px;overflow-y: scroll;flex: 1;}.sort-view-dx {width: 300px;margin-left: 15px;.sort-row {height: 60vh;overflow-y: scroll;p {background-color: #fdfdfd;height: 32px;line-height: 32px;border: 1px solid #ebebeb;padding: 0 10px;margin: 5px 0;&:hover {cursor: move;}}}}
}.active-li {background-color: #efefef;
}
</style>

上方注释扩展方法说明:
比如你上方有筛选条件需要关联切换的,拿我自己的例子,见顶部ROI区域
他筛选条件有一个ROI、ROI倍数的筛选。然后字段展示是ROI123456…等,是循环的数量。切换ROI倍数的时候 表头原有的ROI需要变成ROI倍数 以及prop也一样要变化。
列如顶部ROI附加复选框的方法:
在这里插入图片描述

this.cardDataProp[1] = {title: "ROI指标",checkboxes: Array.from({ length: this.queryParams.displayNum },(_, i) => ({prop: `roi${i + 1}${this.queryParams.displayType == 1 ? "_rate" : ""}`,label: `ROI${i + 1}${this.queryParams.displayType == 2 ? "倍数" : ""}`,})),
};筛选条件选择切换displayType类型后调用 this.$refs.rois.getPointData("init"); 刷新表头

以上根据了选项displayType变化label和prop 但又是属于同一个label表头 只是字段不一样的 或者要用循环的,可采用这种方式,扩展方法自己研究…估计没有其他人需要用这个扩展的,就注释了,不用的可以删掉!

在这里插入图片描述
感谢你的阅读,如对你有帮助请收藏+关注!
只分享干货实战精品从不啰嗦!!!
如某处不对请留言评论,欢迎指正~
博主可收徒、常玩QQ飞车,可一起来玩玩鸭~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/274504.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

prometheus 原理(架构,promql表达式,描点原理)

大家好&#xff0c;我是蓝胖子&#xff0c;提到监控指标&#xff0c;不得不说prometheus&#xff0c;今天这篇文章我会对prometheus 的架构设计&#xff0c;promql表达式原理和监控图表的绘图原理进行详细的解释。来让大家对prometheus的理解更加深刻。 架构设计 先来看看&am…

【REST2SQL】12 REST2SQL增加Token生成和验证

【REST2SQL】01RDB关系型数据库REST初设计 【REST2SQL】02 GO连接Oracle数据库 【REST2SQL】03 GO读取JSON文件 【REST2SQL】04 REST2SQL第一版Oracle版实现 【REST2SQL】05 GO 操作 达梦 数据库 【REST2SQL】06 GO 跨包接口重构代码 【REST2SQL】07 GO 操作 Mysql 数据库 【RE…

【Echarts】曲线图上方显示数字以及自定义值,标题和副标题居中,鼠标上显示信息以及自定义信息

欢迎来到《小5讲堂》 大家好&#xff0c;我是全栈小5。 这是《前端》系列文章&#xff0c;每篇文章将以博主理解的角度展开讲解&#xff0c; 特别是针对知识点的概念进行叙说&#xff0c;大部分文章将会对这些概念进行实际例子验证&#xff0c;以此达到加深对知识点的理解和掌握…

个人博客系列-后端项目-RBAC角色管理(6)

修改上一篇文章创建的用户表 ## 用户表 from django.contrib.auth.hashers import make_password, check_password from django.contrib.auth.models import AbstractBaseUserclass User(AbstractBaseUser):username models.CharField(max_length255, uniqueTrue, verbose_na…

Go语言框架路由Controller控制器设计思路gin路由根据控制器目录分层生成路由地址

Controller设计好处 框架设计用controller分请求路由层级&#xff0c;应用从app目录开始对应请求url路由地址&#xff0c;这样设计师方便开发时候通过请求地址层级快速定位接口方法对应的代码位置。 例如api接口请求路径为&#xff1a;​​http://localhost:8110/​​busines…

C#,老鼠迷宫问题的回溯法求解(Rat in a Maze)算法与源代码

1 老鼠迷宫问题 迷宫中的老鼠,作为另一个可以使用回溯解决的示例问题。 迷宫以块的NN二进制矩阵给出,其中源块是最左上方的块,即迷宫[0][0],目标块是最右下方的块,即迷宫[N-1][N-1]。老鼠从源头开始,必须到达目的地。老鼠只能朝两个方向移动:向前和向下。 在迷宫矩阵…

如何在Linux使用docker安装Plik并实现无公网ip上传下载内网存储的文件资源

文章目录 1. Docker部署Plik2. 本地访问Plik3. Linux安装Cpolar4. 配置Plik公网地址5. 远程访问Plik6. 固定Plik公网地址7. 固定地址访问Plik 正文开始前给大家推荐个网站&#xff0c;前些天发现了一个巨牛的 人工智能学习网站&#xff0c; 通俗易懂&#xff0c;风趣幽默&…

数据结构小记【Python/C++版】——散列表篇

一&#xff0c;基础概念 散列表&#xff0c;英文名是hash table&#xff0c;又叫哈希表。 散列表通常使用顺序表来存储集合元素&#xff0c;集合元素以一种很分散的分布方式存储在顺序表中。 散列表是一个键值对(key-item)的组合&#xff0c;由键(key)和元素值(item)组成。键…

Go语言数据结构(二)堆/优先队列

文章目录 1. container中定义的heap2. heap的使用示例3. 刷lc应用堆的示例 更多内容以及其他Go常用数据结构的实现在这里&#xff0c;感谢Star&#xff1a;https://github.com/acezsq/Data_Structure_Golang 1. container中定义的heap 在golang中的"container/heap"…

Java详解:单列 | 双列集合 | Collections类

○ 前言&#xff1a; 在开发实践中&#xff0c;我们需要一些能够动态增长长度的容器来保存我们的数据&#xff0c;java中为了解决数据存储单一的情况&#xff0c;java中就提供了不同结构的集合类&#xff0c;可以让我们根据不同的场景进行数据存储的选择&#xff0c;如Java中提…

985硕的4家大厂实习与校招经历专题分享(part1)

先简单介绍一下我的个人经历&#xff1a; 985硕士24届毕业生&#xff0c;实验室方向:CV深度学习 就业&#xff1a;工程-java后端 关注大模型相关技术发展 校招offer: 阿里巴巴 字节跳动 等10 研究生期间独立发了一篇二区SCI 实习经历:字节 阿里 京东 B站 &#xff08;只看大厂…

Python 导入Excel三维坐标数据 生成三维曲面地形图(面) 4-4、线条平滑曲面(修改颜色)去除无效点

环境和包: 环境 python:python-3.12.0-amd64包: matplotlib 3.8.2 pandas 2.1.4 openpyxl 3.1.2 scipy 1.12.0 代码: import pandas as pd import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from scipy.interpolate import griddata fro…

深入解析Java内存模型

一、背景 并发编程本质问题是&#xff1a;CPU、内存以及IO三者之间的速度差异。CPU速度快于内存、内存访问速度又远远快于IO&#xff0c;根据木桶理论&#xff0c;程序性能取决于最慢的操作&#xff0c;即IO操作。这样会出现CPU和内存交互时&#xff0c;CPU性能无法被充分利用…

QT 配置https 5.12.2 64位kitsMINGW_64

将 D:\QT5.12.2\Tools\mingw730_64\opt\bin 中的libeay32.dll 和 ssleay32.dll 复制到D:\QT5.12。2\5.12.2\msvc2017_64\bin中 尝试了各种各样的方法&#xff0c;直接这一步就解决了

Python批量提取Word文档表格数据

在大数据处理与信息抽取领域中&#xff0c;Word文档是各类机构和个人普遍采用的一种信息存储格式&#xff0c;其中包含了大量的结构化和半结构化数据&#xff0c;如各类报告、调查问卷结果、项目计划等。这些文档中的表格往往承载了关键的数据信息&#xff0c;如统计数据、项目…

555经典电路

1、555介绍&#xff1a; 555 定时器是一种模拟和数字功能相结合的中规模集成器件。一般用双极性工艺制作的称为 555&#xff0c;用 CMOS 工艺制作的称为 7555&#xff0c;除单定时器外&#xff0c;还有对应的双定时器 556/7556。555 定时器的电源电压范围宽&#xff0c;可在 4…

蓝牙系列七:开源蓝牙协议栈BTStack数据处理(Wireshark抓包分析)

继续蓝牙系列的研究。 在上篇博客&#xff0c;通过阅读BTStack的源码&#xff0c;大体了解了其框架&#xff0c;对于任何一个BTStack的应用程序都有一个main函数&#xff0c;这个main函数是统一的。这个main函数做了某些初始化之后&#xff0c;最终会调用到应用程序提供的btst…

Qt Creator常见问题解决方法

Qt Creator源文件重命名的正确方法 光改文件名是不够的&#xff0c;还要在.pro文件中的SOURCES中把名字改成之后的。 中文乱码&#xff08;字符集设置&#xff09; 菜单栏-工具-选项-设置为utf-8

DHCP中继实验(华为)

思科设备参考&#xff1a;DHCP中继实验&#xff08;思科&#xff09; 一&#xff0c;技术简介 DHCP中继&#xff0c;可以实现在不同子网和物理网段之间处理和转发DHCP信息的功能。如果DHCP客户机与DHCP服务器在同一个物理网段&#xff0c;则客户机可以正确地获得动态分配的IP…

表单进阶(3)-上传文件和隐藏字段

上传文件&#xff1a;<input type"file"> 隐藏字段&#xff1a;<input type"hidden" name"" id"" value"带给后端的信息"> 禁用disabled&#xff1a;<button disabled"disabled">注册</bu…