当前位置: 首页 > 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.dtcms.com/a/321750.html

相关文章:

  • 从引导加载程序到sysfs:Linux设备树的完整解析与驱动绑定机制
  • 您与此网站之间建立的连接不安全
  • 智慧园区漏检率↓82%:陌讯多模态融合算法实战解析
  • 防御保护09
  • 【从0到1制作一块STM32开发板】6. PCB布线--信号部分
  • 手机拍照识别中模糊场景准确率↑37%:陌讯动态适配算法实战解析
  • 二、k8s 1.29 之 网络
  • OpenAI 的 GPT-5 来了
  • GO的启动流程(GMP模型/内存)
  • 要写新项目了,运行老Django项目找找记忆先
  • Redis(②-持久化)
  • 写一个redis客户端软件,参考 Another Redis Desktop Manager 的设计风格。
  • 【沉浸式解决问题】pycharm关闭科学模式
  • Docker Compose 实战指南:从配置到多容器联动的全流程解析
  • Linux系统编程Day9 -- 理解计算机的软硬件管理
  • Dijkstra?spfa?SPstra?
  • 01Vue3
  • 增长强势 成果丰硕 | Fortinet发布2025年第二季度财报
  • GPT-5正式发布:与Claude 4、Gemini 2.5等主流大模型谁更胜一筹?
  • Java中重写和重载有哪些区别
  • 大模型——部署体验gpt-oss-20b
  • 写论文助手Zotero 的使用
  • Scrapy返回200但无数据?可能是Cookies或Session问题
  • electron 静默安装同时安装完成后自动启动(nsis)
  • 【vLLM 学习】Load Sharded State
  • VB网际探针:零依赖轻量爬虫实战
  • GPT-5 is here
  • STM32 输入捕获,串口打印,定时器,中断综合运用
  • centos系统配置防火墙
  • DDR-怎么计算存储空间-什么是预取(Pre-fetch)