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

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.xdnf.cn/news/17242.html

相关文章:

  • 并发编程(五)ThreadLocal
  • 智慧工业设备缺陷检测准确率↑32%:陌讯多模态融合算法实战解析
  • 微软XBOX游戏部门大裁员
  • 6.Linux 系统上的库文件生成与使用
  • 谷粒商城:检索服务
  • 解决Ollama外部服务器无法访问:配置 `OLLAMA_HOST=0.0.0.0` 指南
  • 深度剖析主流AI大模型的编程语言与架构选择:行业实践与技术细节解读
  • 苹果iPhone 17系列将发售,如何解决部分软件适配问题引发讨论
  • 《Hive、HBase、StarRocks、MySQL、OceanBase 全面对比:架构、优缺点与使用场景详解》
  • k8s调度问题
  • Charles中文版抓包工具功能解析,提升API调试与网络性能优化
  • ArgoCD 与 GitOps:K8S 原生持续部署的实操指南
  • 微软披露Exchange Server漏洞:攻击者可静默获取混合部署环境云访问权限
  • 31-数据仓库与Apache Hive-Insert插入数据
  • 悬赏任务系统网站兼职赚钱小程序搭建地推抖音视频任务拉新源码功能详解二开
  • 人工智能与交通:出行方式的革新
  • Ubuntu 22.04 安装 Docker 完整指南
  • [激光原理与应用-183]:测量仪器 - 光束型 - 光束参数乘积(BPP)的本质与含义,聚焦能力与传输稳定性的物理矛盾。
  • 深入解析C++流运算符(>>和<<)重载:为何必须使用全局函数与友元机制
  • 【开源工具】网络交换机批量配置生成工具开发全解:从原理到实战(附完整Python源码)
  • AI赋能6G网络安全研究:智能威胁检测与自动化防御
  • 【新启航】旋转治具 VS 手动翻转:三维扫描中自动化定位如何将单件扫描成本压缩 75%
  • WinForm利用 RichTextBox组件实现输出各种颜色字体日志信息
  • React 原生部落的生存现状:观察“Hooks 猎人“如何用useEffect设陷阱反被依赖项追杀
  • HarmonyOS 设备自动发现与连接全攻略:从原理到可运行 Demo
  • FreeRTOS入门知识(初识RTOS)(二)
  • Latex中公式部分输入正体的字母\mathrm{c}
  • A100用transformers推理gpt-oss
  • Dijkstra?spfa?SPstra?
  • 【Rust】多级目录模块化集成测试——以Cucumber为例