el-table 手动选择展示列

需求:

由于表格的列过多,用滚动条进行滚动对比数据不方便,所以提出,手动选择展示列

实现思路:
  1. 表格默认展示所有字段,每个字段通过 v-if 属性来进行判断是否显示;
  2. 点击设置按钮图标(表格右上角),弹出选择框el-popover;
  3. 选择框中包括:全选复选框、所有字段复选框组 el-checkbox-group、取消按钮、确定按钮;
  4. 选择展示字段后,点击“确定”按钮:更新每个字段值的show属性(true / false);
  5. 关闭弹出框;重新渲染表格;
  6. 选择展示字段后,点击“取消”按钮:关闭弹出框;
  7. 打开弹出框时:选中当前表格所展示的字段;重新渲染表格;
开发时遇到的坑点: 

打开和确定操作后,需要重新渲染表格:

// 重新渲染表格nextTick(() => {tableRef.value.doLayout();})

 具体实现代码:vue3为例
html部分
//设置按妞点击打开选择框 
<el-popover :visible="popoverVisible" :width="160" @show="showPopover"><div class="popoverCheckBoxArea"><el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox><div style="margin: 10px 0;"></div><el-checkbox-group v-model="checkedColumns" @change="handleCheckedColumnsChange"><el-checkbox v-for="column in columns" :label="column.lable" :key="column.lable">{{column.lable}}</el-checkbox></el-checkbox-group></div><div style="text-align: right; margin: 10px 0 0 0"><el-button size="small" text @click="popoverVisible = false" style="margin-right: 10px;">取消</el-button><el-button size="small" type="primary" @click="confirmPopover ">确定</el-button></div><template #reference><el-button :icon="Setting" circle style="float: right;"  @click="popoverVisible = true"/></template></el-popover>
//表格部分<el-table v-loading="data.loading" @selection-change="handleSelectionChange" class="list-table":data="data.dataList" border stripe highlight-current-row ref="tableRef"><el-table-column type="selection" width="45" align="center" /><el-table-column label="#" align="center" type="index" width="60"/><el-table-column prop="code" label="服务器名称" align="center" width="140" v-if="showColumn('服务器名称')"/><!-- <el-table-column prop="interIp" label="内网IP" width="120" align="center" /> --><el-table-column prop="hostName" label="域名" align="center" width="200" v-if="showColumn('域名')"><template #default="scope"><el-tooltip class="box-item" effect="dark" :content="scope.row.interIp" placement="top">{{ scope.row.hostName }}</el-tooltip></template></el-table-column><el-table-column prop="isDeploy" label="是否部署" align="center" width="85" v-if="showColumn('是否部署')"><template #default="scope"><dict-tag :type="DICT_TYPE.IS_DEPLOYMENT" :value="scope.row.isDeploy" :projectId="queryParams.projectId" /></template></el-table-column>><el-table-column prop="httpPort" label="HTTP" align="center" width="60px" v-if="showColumn('HTTP')"/><el-table-column prop="socketPort" label="SOCKET" align="center" width="68px" v-if="showColumn('SOCKET')"/><el-table-column prop="operationType" label="状态" align="center" width="90px" v-if="showColumn('状态')"><template #default="scope"><el-tag v-if="scope.row.operationType != undefined" :type="operationTypecolor(scope.row.operationType)":effect="scope.row.operationType == '未知' ? 'light' : 'dark'">{{ scope.row.operationType }}</el-tag></template></el-table-column><el-table-column prop="gsVer" label="当前版本" align="center" width="130px" v-if="showColumn('当前版本')"/><el-table-column prop="gsVerUpdateTime" label="上次更新时间" width="160" align="center" :formatter="(dateFormatter as any)" v-if="showColumn('上次更新时间')"/><el-table-column prop="publish" label="正式服" align="center" width="85px" v-if="showColumn('正式服')"><template #default="scope"><dict-tag :type="DICT_TYPE.FORMALDRESS" :value="scope.row.publish" :projectId="queryParams.projectId" /></template></el-table-column>><el-table-column prop="showTime" label="展示时间" width="160" align="center" :formatter="(dateFormatter as any)" v-if="showColumn('展示时间')"/><el-table-column prop="startTime" label="开服时间" width="95" align="center" :formatter="dateFormatters" v-if="showColumn('开服时间')"/><el-table-column prop="mergeTime" label="合服时间" width="95" align="center" :formatter="dateFormatters" v-if="showColumn('合服时间')"/><el-table-column prop="mergeCount" label="合服次数" width="81" align="center" v-if="showColumn('合服次数')"/><el-table-column prop="gsFrontVersion" label="前端版本" width="100" align="center" v-if="showColumn('前端版本')"/><el-table-column prop="serviceDays" label="开服天数" width="100" align="center" v-if="showColumn('开服天数')"/><el-table-column prop="gsLevel" label="游戏服等级" width="100" align="center" v-if="showColumn('游戏服等级')"/><el-table-column prop="s_type" label="执行区" width="100"  align="center" fixed="right"><template #default="scope"><div style="display: flex;"><span class="readoDiv" v-if="scope.row.s_type != undefined" :style="color(scope.row.s_type)"></span><span class="readoSpan">{{ scope.row.s_type }}</span></div></template></el-table-column><el-table-column label="操作" width="110px" align="center" v-auth="['operation:gameserver:handleServiceList']" fixed="right"><template #default="scope"><el-dropdown ref="dropdown" trigger="click" @command="(command) => handleCommand(command, scope.row)"v-auth="['operation:gameserver:handleServiceList',]" @visible-change="visiblechange"><el-button type="primary" link><Icon icon="ep:d-arrow-right" />>> 更多</el-button><template #dropdown><el-dropdown-menu><el-popconfirm title="查看运行状态?" v-auth="'operation:gameserver:handleServiceList'"@confirm="runningState(scope.row)" confirm-button-text="确定" cancel-button-text="取消" @show="show"><template #reference><el-button :disabled="dropdowndisabled">运行状态</el-button></template></el-popconfirm><el-popconfirm title="查看关闭游戏?" v-auth="'operation:gameserver:handleServiceList'"@confirm="closeGames(scope.row)" confirm-button-text="确定" cancel-button-text="取消" @show="show"><template #reference><el-button :disabled="dropdowndisabled">关闭游戏</el-button></template></el-popconfirm><el-popconfirm title="查看启动游戏?" v-auth="'operation:gameserver:handleServiceList'"@confirm="startGames(scope.row)" confirm-button-text="确定" cancel-button-text="取消" @show="show"><template #reference><el-button :disabled="dropdowndisabled">启动游戏</el-button></template></el-popconfirm><el-popconfirm title="查看热更游戏?" v-auth="'operation:gameserver:handleServiceList'"@confirm="watchGames(scope.row)" confirm-button-text="确定" cancel-button-text="取消" @show="show"><template #reference><el-button :disabled="dropdowndisabled">热更游戏</el-button></template></el-popconfirm><el-dropdown-item command="updateGames" style="padding: 0; width: 100%;"v-auth="'operation:gameserver:handleServiceList'" :disabled="dropdowndisabled"><el-button :disabled="dropdowndisabled">更新游戏</el-button></el-dropdown-item><el-dropdown-item command="startFile" :disabled="dropdowndisabled"><el-button :disabled="dropdowndisabled">更新start文件</el-button></el-dropdown-item><el-dropdown-item command="configFile" :disabled="dropdowndisabled"><el-button :disabled="dropdowndisabled">更新config文件</el-button></el-dropdown-item></el-dropdown-menu></template></el-dropdown></template></el-table-column></el-table>
js部分
const popoverVisible = ref(false)
const checkAll = ref(true)
// 选择展示的字段数组默认的
const columns = ref([{ lable: '服务器名称', show: true },{ lable: '域名', show: true },{ lable: '是否部署', show: true },{ lable: 'HTTP', show: true },{ lable: 'SOCKET', show: true },{ lable: '状态', show: true },{ lable: '当前版本', show: true },{ lable: '上次更新时间', show: true },{ lable: '正式服', show: true },{ lable: '展示时间', show: true },{ lable: '开服时间', show: true },{ lable: '合服时间', show: true },{ lable: '合服次数',  show: true },{ lable: '前端版本', show: true },{ lable: '开服天数', show: false },{ lable: '游戏服等级', show: false },
])  const checkedColumns:any = ref([]) //选中的数据
const isIndeterminate = ref(false)
// 点击弹出框的“全选”按钮
const handleCheckAllChange = (val:any)=> {let columnsValueList:any = [];columns.value.map(item => columnsValueList.push(item.lable));checkedColumns.value = val ? columnsValueList : [];isIndeterminate.value = false;
}
// 点击弹出框的选择展示菜单的复选框
const handleCheckedColumnsChange=(value:any) =>{let checkedCount = value.length;checkAll.value = checkedCount === columns.value.length;isIndeterminate.value = checkedCount > 0 && checkedCount < columns.value.length;
}
//表格实例
const tableRef:any = ref(null)
// 弹出框打开时触发
const showPopover =()=> {// 选中目前已展示的字段值checkedColumns.value = [];columns.value.map((item:any) => {if(item.show) {checkedColumns.value.push(item.lable);}});// 如果目前展示的是全部字段,则需要勾选上“全选”按钮if(columns.value.length == checkedColumns.value.length) {checkAll.value = true;isIndeterminate.value = false;}// 重新渲染表格nextTick(() => {tableRef.value.doLayout();})
}// 表格列是否显示的方法
const showColumn = (currentColumn:string)=>{const column = columns.value.find((item: any) => item.lable === currentColumn);if (column) {return column.show;} else {console.error(`Column with label "${currentColumn}" not found.`);return undefined; // 或者您可以选择抛出一个错误}
}
// 点击弹出框的“确定”按钮
const confirmPopover = ()=> {//选择不能没有,需要可放开注释// if(checkedColumns.value.length == 0) {//   ElMessage.error('请选择需要展示的表格字段')//     return;// }// 根据选择结果,遍历修改列是否展示的属性值columns.value.forEach(item => {if(checkedColumns.value.some((el:any) => el == item.lable)) {item.show = true;} else {item.show = false;}})popoverVisible.value = false;// 重新渲染表格nextTick(() => {tableRef.value.doLayout();})
}
效果: 

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

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

相关文章

家政预约小程序用例图分析

在和客户进行需求沟通的时候&#xff0c;除了使用常规的问答的形式&#xff0c;我还使用图形化工具更深入的沟通。比如借助UML的用例图来开展系统分析&#xff0c;并且按照角色详细拆解了家政预约小程序的各个用例。在分析阶段思考的越多&#xff0c;沟通的越多&#xff0c;在系…

详解 scanf 和 printf(占位符、printf、scanf的返回值、printf的输出格式、scanf的输入格式)

一、printf 1.基本用法 •printf 的作⽤是将参数⽂本输出到屏幕 •printf print f &#xff0c;printf 代表输出打印&#xff0c;f代表 format &#xff08;格式化&#xff09;&#xff0c;format表⽰可以定制输出⽂本的格式,所以详细来说printf是将数据按照指定格式打印 …

爬蟲動態IP代理與數據採集穩定性

對於從事爬蟲開發的人來說&#xff0c;IP代理的使用直接影響了爬蟲的效率和穩定性。爬蟲的本質是模擬用戶訪問網站&#xff0c;通過抓取網頁內容來獲取所需數據。但大多數網站為了保護自己的數據或防止濫用&#xff0c;會設置諸如IP訪問頻率限制、登錄驗證甚至封禁等反爬蟲措施…

js之原型及原型链

如果js没有构造函数 首先不考虑构造函数这个鬼东西&#xff0c;当他不存在。 这个时候&#xff0c;创建对象的方式就是 <script type"text/javascript">var dog {name: hachi,age: 3}</script> 然后在浏览器上观察该对象&#xff0c;可以看到该对象包…

Xcode 运行真机失败

错误提示&#xff1a; iPhone xxx is not available because it is unpaired. Pair with the device in the Xcode Devices Window, and respond to any pairing prompts on the device. 处理方法&#xff1a; 把Xcode关闭&#xff0c;手机断开数据线&#xff0c;打开终端&…

BIO、NIO、AIO、Netty从简单理解到使用

Java编程中BIO、NIO、AIO是三种不同的I/O&#xff08;输入/输出&#xff09;模型&#xff0c;它们代表了不同的I/O处理方式。 Netty就是基于Java的NIO&#xff08;New Input/Output&#xff09;类库编写的一个高性能、异步事件驱动的网络应用程序框架&#xff0c;用于快速开发可…

import模块到另一个文件夹报错:ModuleNotFoundError: No module named xxx

1. 问题 打开项目文件夹my_code&#xff0c;将bb.py的函数或者类import到aa.py中&#xff0c;然后运行aa.py文件&#xff0c;可能会报错ModuleNotFoundError: No module named xxx。 ‪E:\Desktop\my_code ├── a │ ├── train.sh │ └── aa.py └── b└── b…

怎么写C#命令行参数程序,及控制台带参数案例(程序完整源码)下载

C#命令行参数解析控制台带参数编写案例&#xff08;程序完整源码&#xff09;下载链接 https://download.csdn.net/download/luckyext/90434790 在CMD命令窗口&#xff0c;输入ping 、ipconfig等这样的命令&#xff0c;大家应该都知道&#xff0c;但很多同学可能不知道怎么写…

JAVA安全—手搓内存马

前言 最近在学这个内存马&#xff0c;就做一个记录&#xff0c;说实话这个内存马还是有点难度的。 什么是内存马 首先什么是内存马呢&#xff0c;顾名思义就是把木马打进内存中。传统的webshell一旦把文件删除就断开连接了&#xff0c;而Java内存马则不同&#xff0c;它将恶…

算法 并查集

目录 前言 一 并查集的思路 二 并查集的代码分析 三 实操我们的代码 四 并查集的代码优化 总结 前言 并查集主要是用来求解集合问题的&#xff0c;用来查找集合还有就是合并集合&#xff0c;可以把这个运用到最小生成树里面 一 并查集的思路 1 并查集的相关的操作…

vulnhub靶场之【digitalworld.local系列】的development靶机

前言 靶机&#xff1a;digitalworld.local-devt-improved&#xff0c;IP地址为192.168.10.10 攻击&#xff1a;kali&#xff0c;IP地址为192.168.10.6 kali采用VMware虚拟机&#xff0c;靶机选择使用VMware打开文件&#xff0c;都选择桥接网络 这里官方给的有两种方式&…

Stiring-PDF:开源免费的PDF文件处理软件

Stiring-PDF是一款开源免费且比较好用的PDF文件处理工具。 Stiring-PDF官网网址为&#xff1a;https://www.stiringpdf.com/。Stiring-PDF是一款专业的PDF文件处理工具&#xff0c;支持Windows和macOS操作系统&#xff1b;提供丰富的PDF编辑和转换功能&#xff0c;适用于日常工…

SpringCloud系列教程(十二):网关配置动态路由

除了token以外&#xff0c;还有一个很实用的功能就是把网关的路由配置放到nacos上&#xff0c;并且修改路由配置的时候&#xff0c;网关服务可以动态的更新&#xff0c;这样我们在调整网络配置的时候&#xff0c;就不用重启服务了。所以我们需要用到两个重要的类&#xff1a;Na…

R JSON 文件

R JSON 文件 引言 在当今的数据分析和处理领域&#xff0c;R语言作为一种功能强大的统计计算和图形展示工具&#xff0c;被广泛应用于各种数据分析任务中。随着大数据时代的到来&#xff0c;数据的格式和结构变得越来越多样化。JSON&#xff08;JavaScript Object Notation&a…

ES6 特性全面解析与应用实践

1、let let 关键字用来声明变量&#xff0c;使用let 声明的变量有几个特点&#xff1a; 1) 不允许重复声明 2) 块儿级作用域 3) 不存在变量提升 4) 不影响作用域链 5) 暂时性死区 6&#xff09;不与顶级对象挂钩 在代码块内&#xff0c;使用let命令声明变量之前&#x…

如何使用 Ollama 的 API 来生成聊天

如何使用 Ollama 的 API 来生成聊天 简介 生成聊天 生成聊天的示例 加载模型 卸载模型 简介 Ollama 提供了一个 RESTful API&#xff0c;允许开发者通过 HTTP 请求与 Ollama 服务进行交互。这个 API 覆盖了所有 Ollama 的核心功能&#xff0c;包括模型管理、运行和监控。本…

【学Rust写CAD】10 加法器

源码 // src/matrix/adder.rs/** 说明&#xff1a;连加计算中&#xff0c;为提高运行期效率&#xff0c;用该结构增加一个Const变量&#xff0c;方便单独合并所有Const类型&#xff0c;最后一步才有可能出现Const与Val的加法计算*/use std::ops::Add;use super::constant::{Co…

学到什么记什么(25.3.3)

Upload-labs 今日重新做了一下文件上传漏洞&#xff0c;这里第一题之前采用直接抓包改后缀名.jpg为.php&#xff0c;再写入一句话<?php phpinfo();?>然后放行&#xff0c;得到图片地址&#xff08;可复制&#xff09;&#xff0c;本来直接访问图片地址即可得到敏感信息…

el-table input textarea 文本域 自适应高度,切换分页滚动失效处理办法

场景&#xff1a; el-table 表格 需要 input类型是 textarea 高度是自适应&#xff0c;第一页数据都是单行数据 不会产生滚动条&#xff0c;但是第二页数据是多行数据 会产生滚动条&#xff0c; bug: 第一页切换到第二页 第二页滚动条无法展示 解决办法&#xff1a;直接修改样…

[杂学笔记] 封装、继承、多态,堆和栈的区别,堆和栈的区别 ,托管与非托管 ,c++的垃圾回收机制 , 实现一个单例模式 注意事项

文章目录 1.封装、继承、多态2. 堆和栈的区别3.指针和引用的区别4. 托管与非托管5. c的垃圾回收机制6. 实现一个单例模式 注意事项 1.封装、继承、多态 封装就是将数据和内部的方法封装到一个类中&#xff0c;对外隐藏内部实现细节&#xff0c;但是留下了公共接口提供给外部使…