自由定义表单table组件(antdesign版)

对表单自由排序,决定哪些列显示隐藏,能保存设置过的操作
效果图
在这里插入图片描述
使用页,操作列dataIndex要设置为action,forKey必需是唯一的
用的vue2版的antdesign vue写的样式,想用其它的ui框架可以自行修改样式

<customTable :tableHeadList="tableHead" @customTableUpde="customTableUpde" forKey="dataIndex"></customTable>import customTable from "./components/customTable.vue" //引入
const tableHead = [{title: '序号',dataIndex: 'index',scopedSlots: { customRender: 'index' },width: 60,align: 'center'},{title: '产品图片',dataIndex: 'pic',scopedSlots: { customRender: 'pic' },ellipsis: true,align: 'center'},{title: '产品名称',dataIndex: 'goodsName',ellipsis: true,align: 'center'},{title: '原价',dataIndex: 'oriPrice',ellipsis: true,width: 80,align: 'center'},{title: '现价',dataIndex: 'price',ellipsis: true,width: 80,align: 'center'},{title: '产品分类',dataIndex: 'goodsCategoryName',ellipsis: true,align: 'center'},{title: '状态',dataIndex: 'publishStatusName',scopedSlots: { customRender: 'publishStatusName' },ellipsis: true,align: 'center'},{title: '操作',dataIndex:'action',// fixed: 'right',scopedSlots: { customRender: 'action' },width: 140,align: 'center'}
];
export default {components: { customTable },data() {return {tableHeadList:[],//表单使用的头部数据}},methods:{//更新表格头部customTableUpde(e){this.tableHeadList = e;},}

customTable组件

<template><div><a-popover title="" v-model="customVisible" trigger="click" placement="topRight"><a-icon type="setting" style="font-size:20px;" @click="waiClick" /><template slot="content"><div class="customBigBox"><div :id="customId" :ref="customId" v-if="headList&&headList.length>0" class="customBox"><div v-for="(item,index) in headList" :key="item[forKey]" ><div class="customEach" v-if="item.dataIndex != operation"><a-checkbox@change="customCheckChange($event,index)" style="width: 20px;height: 20px;margin-right:4px;":checked="item.ischecked"v-model="item.ischecked" ></a-checkbox><div class="customText">{{item.title}}</div><a-tooltip placement="top" title="固定到左侧"><a-icon type="vertical-right" class="customFixed" @click="addFixed('left',index)" :style="{'margin-left':'4px','color': (item[fixedName]&&item[fixedName]=='left')?'#1890ff':'#777'}" /></a-tooltip><a-tooltip placement="top" title="固定到右侧"><a-icon type="vertical-left" class="customFixed" @click="addFixed('right',index)" :style="{'margin-left':'10px','color': (item[fixedName]&&item[fixedName]=='right')?'#1890ff':'#777'}" /></a-tooltip></div></div></div><div class="customNodata" v-else>暂无数据</div></div><div class="customAction"><a-button @click="reset" style="margin-right: 16px;">重置</a-button><a-button type="primary" @click="save">保存</a-button></div></template></a-popover></div>
</template><script>//拖拽插件,官网https://sortablejs.com/import Sortable from 'sortablejs'export default{data(){return {customVisible:false,headList:[],//数据el:null,//列表元素}},props:{//组件idcustomId:{type:String,default:'customCustomTable'},//指定的key字段forKey:{type:String,default:'dataIndex'},//表头数据tableHeadList:{type:Array,default:[]},//表头数据缓存IDcustomCacheId:{type:String,default:'customCustomTable'},//左右固定属性名fixedName:{type:String,default:'fixed'},//操作列对应的名字operation:{type:String,default:'action'}},mounted(){         let data = JSON.parse(JSON.stringify(this.tableHeadList));this.dealList(data);this.$nextTick(() =>{this.initDrag()this.watchList()})},methods:{//重置reset(){window.localStorage.removeItem(this.customCacheId)let data = JSON.parse(JSON.stringify(this.tableHeadList))this.dealList(data)this.$message.success('重置成功')this.customVisible = false;},//保存save(){//加个排序this.headList.map((res,index) =>{res.issort = index+1;})window.localStorage.setItem(this.customCacheId,JSON.stringify(this.headList))console.log('保存',this.headList)this.$message.success('保存成功')this.customVisible = false;},//处理初始化数据dealList(e){const that = this;let old = window.localStorage.getItem(this.customCacheId);if(old){//有旧数据,新旧合并,保留旧数据的操作let newlist = [];let oldlist = JSON.parse(old);console.log('拿取',oldlist)e.map(res =>{let resul = oldlist.filter(ve => ve[that.forKey] == res[that.forKey]);if(resul&&resul.length>0){let currentobj = res;currentobj.issort = resul[0].issort;currentobj.ischecked = resul[0].ischecked;currentobj[that.fixedName] = resul[0][that.fixedName];newlist.push(currentobj)}else{res.issort = 9999;res.ischecked = true;if(!res[that.fixedName]){res[that.fixedName] = '';}newlist.push(res)}})//排序let sortlist = newlist.sort((a,b) =>{let value1 = a['issort'];let value2 = b['issort'];return value1 - value2;})this.headList = sortlist;}else{e.map(res =>{res.issort = 9999;res.ischecked = true;if(!res[that.fixedName]){res[that.fixedName] = '';}})this.headList = e;}this.outputData()},//重新初始化拖拽waiClick(){if(!this.el){setTimeout(() =>{this.$nextTick(() =>{this.initDrag()})},100)}},//初始化拖拽initDrag(){const that = this;const el = document.getElementById(this.customId);if(el){this.el = el;// 创建拖拽实例new Sortable(el, {group:this.customId+'group',animation: 150,sort: true,onEnd: (e) => {//拖拽回调that.dealDragList(e.newIndex,e.oldIndex)}});}},//监听数据变化watchList(){this.unwatch = this.$watch('tableHeadList',function(newlist,oldlist){let data = JSON.parse(JSON.stringify(newlist));this.dealList(data)},{deep:true //深度监听})},//处理拖拽后的数据dealDragList(newIndex,oldIndex){if(newIndex == oldIndex)return;let data = JSON.parse(JSON.stringify(this.headList));const currentRow = data[oldIndex];data.splice(oldIndex, 1);//删除原位置数据data.splice(newIndex, 0, currentRow);		this.headList = data;this.outputData()},//选择customCheckChange(e,index){this.headList[index].ischecked = e.target.checked;this.outputData()},//固定项addFixed(e,index){if(e == 'left'){if(!this.headList[index][this.fixedName] || this.headList[index][this.fixedName] == 'right'){this.headList[index][this.fixedName] = 'left';}else{this.headList[index][this.fixedName] = '';}}else{if(!this.headList[index][this.fixedName] || this.headList[index][this.fixedName] == 'left'){this.headList[index][this.fixedName] = 'right';}else{this.headList[index][this.fixedName] = '';}}this.outputData()},//输出数据outputData(e){let data = JSON.parse(JSON.stringify(this.headList));//将符合要求的返回let newdata = data.filter(res => res.ischecked);this.$emit('customTableUpde',newdata)console.log('新返回的数组',data)},},beforeDestroy(){//移除监听if(this.unwatch){this.unwatch()}}}
</script><style scoped>
.customBigBox{width:270px;
}
.customBox{width:100%;height: 280px;overflow-y: scroll;background-color: #fff;border-radius: 6px;padding-right: 5px;box-sizing: border-box;
}
.customEach{width: 100%;height: 34px;user-select: none;list-style-type: none;color: #333;padding-left: 10px;font-size: 14px;display: flex;justify-content: space-between;align-items: center;background-color: #fff;
}
.customText{flex: 1;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;
}
.customText:hover{color: #1890ff;
}
.customFixed{font-size: 16px;
}
.customFixed:hover{color: #1890ff;
}
.customAction{width: 100%;height: 60px;display: flex;justify-content: flex-end;align-items: center;padding: 5px 14px;box-sizing: border-box;
}
.customNodata{width: 100%;height: 100%;display: flex;align-items: center;justify-content: center;font-size: 14px;color: #999;
}
</style>

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

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

相关文章

Brain.js 的力量:构建多样化的人工智能应用程序

机器学习&#xff08;ML&#xff09;是人工智能 (AI) 的一种形式&#xff0c;旨在构建可以从处理的数据中学习或使用数据更好地执行的系统。人工智能是模仿人类智能的系统或机器的总称。 机器学习&#xff08;ML&#xff09;与复杂的数学纠缠在一起&#xff0c;让大多数初学者…

解决nginx代理后,前端拿不到后端自定义的header

先说结论&#xff0c;因为前端和nginx对接&#xff0c;所以需要在nginx添加如下配置向前端暴露header add_header Access-Control-Expose-Headers Authorization 排查过程 1.后端设置了Authorization 的响应头作为token的返回&#xff0c;前后端本地联调没有问题 response.s…

嵌入式驱动学习第六周——内核函数调用(堆栈打印)

前言 在内核中&#xff0c;函数调用堆栈非常重要&#xff0c;因为它可以帮助开发人员理解代码是如何执行的&#xff0c;从而进行调试、性能优化或问题排查。堆栈可以显示当前执行的函数以及导致该函数调用的先前函数&#xff0c;从而形成一个函数调用链。本篇博客就介绍堆栈打印…

软件无线电原理

常规软件无线电接收器&#xff1a; 首先&#xff0c;来自天线的射频信号被放大&#xff0c;通常射频部分利用一个调谐器将感兴趣的频段区域的信号进行放大。这个放大的射频信号被送入一个混频器。来自本振的信号也被送入混频器&#xff0c;其频率由无线电的调谐控制决定。混频器…

【LeetCode】--- 动态规划 集训(二)

目录 一、63. 不同路径 II1.1 题目解析1.2 状态转移方程1.3 解题代码 二、931. 下降路径最小和2.1 题目解析2.2 状态转移方程2.3 解题代码三、174. 地下城游戏3.1 题目解析3.2 状态转移方程3.3 解题代码 一、63. 不同路径 II 题目地址&#xff1a; 不同路径 II 一个机器人位于…

传输层 --- TCP (下篇)

目录 1. 超时重传 1.1. 数据段丢包 1.2. 接收方发送的ACK丢包 1.3. 超时重传的超时时间如何设置 2. 流量控制 3. 滑动窗口 3.1. 初步理解滑动窗口 3.2. 滑动窗口的完善理解 3.3. 关于快重传的补充 3.4. 快重传和超时重传的区别 4. 拥塞控制 4.1. 拥塞控制的宏观认识…

「 典型安全漏洞系列 」11.身份验证漏洞详解

身份验证是验证用户或客户端身份的过程。网站可能会暴露给任何连接到互联网的人。这使得健壮的身份验证机制成为有效的网络安全不可或缺的一部分。 1. 什么是身份验证 身份验证即认证&#xff0c;是验证给定用户或客户端身份的过程。身份验证漏洞使攻击者能够访问敏感数据和功…

Linux网络基础 (三) —— Socket

文章目录 Socket 编程基本概念Socket背景Socket 为了解决什么问题 socketsockaddr结构sockaddrsockaddr_insockaddr 和 sockaddr_in 的关系sockaddr_un 示例代码 &#x1f396; 博主的CSDN主页&#xff1a;Ryan.Alaskan Malamute &#x1f4dc; 博主的代码仓库主页 [ Gitee ]&…

【MySQL】数据操作语句(DML)

&#x1f466;个人主页&#xff1a;Weraphael ✍&#x1f3fb;作者简介&#xff1a;目前学习计网、mysql和算法 ✈️专栏&#xff1a;MySQL学习 &#x1f40b; 希望大家多多支持&#xff0c;咱一起进步&#xff01;&#x1f601; 如果文章对你有帮助的话 欢迎 评论&#x1f4ac…

【论文通读】AgentStudio: A Toolkit for Building General Virtual Agents

AgentStudio: A Toolkit for Building General Virtual Agents 前言AbstractMotivationFramework评估GUI GroudingReal-World Cross-Application Benchmark Suite Conclusion 前言 来自昆仑万象的一篇智能体环境数据大一统框架工作&#xff0c;对未来计算机智能体的发展具有指…

FPGA常用IP核之FIFO学习

IP核是FPGA芯片公司提供的逻辑功能块&#xff0c;在FPGA芯片中可以进行优化和预先配置&#xff0c;可以直接在用户设计的程序中使用&#xff0c;应用范围很广。在FPGA设计开发过程中使用IP核&#xff0c;可以大大的缩短开发周期&#xff0c;高度优化的IP核可以使FPG开发工程师专…

前端三剑客 —— CSS (第六节)

目录 内容回顾&#xff1a; 弹性布局属性介绍 案例演示 商品案例 布局分析 登录案例 网格布局 内容回顾&#xff1a; 变量&#xff1a;定义变量使用 --名称&#xff1a;值&#xff1b; 使用变量&#xff1a; 属性名&#xff1a;var&#xff08;--名称&#xff09;&a…

虚拟主机、VPS主机和云服务器的区别

对于每个建站新手来说&#xff0c;首先要解决的就是服务器购买的问题&#xff0c;目前市面有很多类型的服务器&#xff0c;常见的有&#xff1a;阿里云、腾讯云、Vultr云服务器&#xff0c;也有RackNerd、Cloudways等提供的VPS&#xff0c;还有SiteGround、ChemiCloud 、 Hosti…

数据结构之堆底层实现的循序渐进

题外话 把没写的都补回来! 正题 堆 概念 堆是一棵完全二叉树&#xff0c;因此可以层序的规则采用顺序的方式来高效存储&#xff0c; 大根堆:指根结点比左右孩子都大的堆 小根堆:指根结点比左右孩子都小的堆 性质 1.堆中某个节点的值总是不大于或不小于其父节点的值 2…

鸿蒙OS元服务开发:【(Stage模型)设置应用主窗口】

一、设置应用主窗口说明 在Stage模型下&#xff0c;应用主窗口由UIAbility创建并维护生命周期。在UIAbility的onWindowStageCreate回调中&#xff0c;通过WindowStage获取应用主窗口&#xff0c;即可对其进行属性设置等操作。还可以在应用配置文件中设置应用主窗口的属性&…

每日一题(leetcode1026):节点与其祖先的最大差值--dfs

考虑到只能计算祖先之间的节点差而不能计算兄弟之间的节点差&#xff0c;所以思考使用dfs来解决该题。 /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), ri…

嵌入式开发学习---(部分)数据结构(无代码)

数据结构 为什么学习数据结构&#xff1f; 1&#xff09;c语言告诉如何写程序&#xff0c;数据结构是如何简洁高效的写程序 2&#xff09;遇到一个实际问题&#xff0c;需要写程序去实现相应功能&#xff0c;需要解决那两个方面的问题&#xff1f; 如何表达数据之间的逻辑规律…

背包问题---

一、背包模型 有一个体积为V的背包,商店有n个物品,每个物品有一个价值v和体积w,每个物品只能被拿一次,问能够装下物品的最大价值。 这里每一种物品只有两种状态即"拿"或"不拿". 设状态dp[i][j]表示到第i个物品为止,拿的物品总体积为j的情况下的最大价…

一、持续集成介绍

持续集成介绍 一、什么是持续集成二、持续集成的流程三、持续集成的组成要素四、持续集成的好处 一、什么是持续集成 持续集成&#xff08;CI&#xff09;指的是&#xff0c;频繁地&#xff08;一天多次&#xff09;将代码集成到主干。持续集成的目的&#xff0c;就是让产品可…

LeetCode:1483. 树节点的第 K 个祖先(倍增 Java)

目录 1483. 树节点的第 K 个祖先 题目描述&#xff1a; 实现代码与解析&#xff1a; 倍增 原理思路&#xff1a; 1483. 树节点的第 K 个祖先 题目描述&#xff1a; 给你一棵树&#xff0c;树上有 n 个节点&#xff0c;按从 0 到 n-1 编号。树以父节点数组的形式给出&#…