当前位置: 首页 > news >正文

element plus table 表格操作列根据按钮数量自适应宽度

直接上代码
在utils直接封装autoWidthHeaderRender.js文件

/*** 表格操作列自适应宽度计算函数* @param {string} className - 操作栏容器类名* @param {number} padding - 额外padding宽度* @param {number} minWidth - 最小宽度* @returns {number} 计算后的宽度*/
export const calculateOperationWidth = (className = 'operation-cell', padding = 30, minWidth = 120) => {try {const cells = document.getElementsByClassName(className)let maxWidth = minWidthArray.from(cells).forEach(cell => {if (cell.offsetParent !== null) { // 只计算可见元素const buttons = cell.querySelectorAll('.el-button')let totalWidth = 0buttons.forEach(button => {// 计算每个按钮的宽度(包括margin)const style = window.getComputedStyle(button)const margin = parseFloat(style.marginLeft) + parseFloat(style.marginRight)totalWidth += button.offsetWidth + margin})maxWidth = Math.max(maxWidth, totalWidth + padding)}})return maxWidth} catch (error) {console.error('操作栏宽度计算错误:', error)return minWidth}
}

页面里使用 增加class style


<el-tableref="tableRef"borderv-loading="loading":data="customerList"@selection-change="handleSelectionChange">
<el-table-columnlabel="操作"align="center"class-name="small-padding fixed-width":width="operationWidth"fixed="right"><template #default="scope"><divclass="operation-cell"style="white-space: nowrap; display: inline-block"><el-buttonlinktype="primary"icon="Memo"v-if="scope.row.openAccStatus == 'OPENING_ACC'"@click="handleValid(scope.row)"v-hasPermi="['business:company:valid']">去验证</el-button></div></template></el-table-column></el-table>
import { calculateOperationWidth } from '@/utils/autoWidthHeaderRender'const operationWidth = ref(120)// 计算并更新操作列宽度
const updateOperationWidth = async () => {await nextTick() // 确保DOM已更新operationWidth.value = calculateOperationWidth('operation-cell', 30, 120)
}// 监听数据变化
watch(() => customerList.value, updateOperationWidth, {deep: true,immediate: true,
})

这样就可以自适应宽度了,大体思路是这样

http://www.lryc.cn/news/613861.html

相关文章:

  • 宝龙地产债务化解解决方案二:基于资产代币化与轻资产转型的战略重构
  • (1-9-1) Maven 特性、安装、配置、打包
  • Electron——窗口
  • linux mysql 8.X主从复制
  • 【Linux】从零开始:RPM 打包全流程实战万字指南(含目录结构、spec 编写、分步调试)
  • 避免“卡脖子”!如何减少内存I/O延迟对程序的影响?
  • Function + 异常策略链:构建可组合的异常封装工具类
  • 二叉树、算法
  • 防火墙概述
  • React 原生部落的生存现状:观察“Hooks 猎人“如何用useEffect设陷阱反被依赖项追杀
  • 【Unity3D实例-功能-跳跃】角色跳跃
  • Rocky Linux 10.0下安装使用KVM虚拟机
  • 破界之光:DeepSeek 如何重构AI搜索引擎的文明坐标 || #AIcoding·八月创作之星挑战赛#
  • Mac上安装和配置MySQL(使用Homebrew安装MySQL 8.0)
  • [202403-E]春日
  • 等保测评-Nginx中间件
  • DM8数据库服务正常,但是登录报错 [-70019]:没有匹配的可登录服务器
  • cAdvisor 容器监控软件学习
  • docker下载安装和使用(Hyper-V方式)
  • Socket编程预习
  • AI赋能SEO关键词优化策略
  • 深入理解 robots.txt:网站与搜索引擎的 “沟通协议”
  • sqli-labs通关笔记-第38关 GET字符型堆叠注入(单引号闭合 手工注入+脚本注入两种方法)
  • Dubbo应用开发之基于xml的第一个Dubbo程序
  • 安全扫描:检测到目标站点存在javascript框架库漏洞问题(vue)
  • 13. 搜索引擎-ElasticSearch
  • 深入探索 PDF 数据提取:PyMuPDF 与 pdfplumber 的对比与实战
  • 技术速递|GPT-5 正式上线 Azure AI Foundry
  • 机器学习——06 集成学习
  • AI搜索引擎——DeepSeek崛起 || #AIcoding·八月创作之星挑战赛# || 简单版