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 = minWidth
    
    Array.from(cells).forEach(cell => {
      if (cell.offsetParent !== null) { // 只计算可见元素
        const buttons = cell.querySelectorAll('.el-button')
        let totalWidth = 0
        
        buttons.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-table
      ref="tableRef"
      border
      v-loading="loading"
      :data="customerList"
      @selection-change="handleSelectionChange"
    >
<el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        :width="operationWidth"
        fixed="right"
      >
        <template #default="scope">
          <div
            class="operation-cell"
            style="white-space: nowrap; display: inline-block"
          >
            <el-button
              link
              type="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,
})

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

相关推荐
跟着珅聪学java16 小时前
Element UI 的 Tabs 标签页开发教程
javascript·vue.js·elementui
不是山谷.:.2 天前
Axios的【接口防抖 + 请求失败重试 + 弱网提示】三合一高阶版封装
前端·javascript·vue.js·笔记·elementui·typescript
镜宇秋霖丶7 天前
2026.5.12@霖宇博客制作中遇见的问题
前端·vue.js·elementui
镜宇秋霖丶10 天前
2026.5.10@霖宇博客制作中遇见的问题
前端·vue.js·elementui
莫生灬灬10 天前
ElementUI封装 共91个组件 支持易语言/火山/C#/Python
开发语言·c++·python·ui·elementui·c#
镜宇秋霖丶11 天前
2026.5.8@霖宇博客制作中遇见的问题
前端·vue.js·elementui
MXN_小南学前端12 天前
Vue + Element UI 分页器封装:比直接用 el-pagination 更省心的通用方案
javascript·vue.js·elementui
invicinble12 天前
前端框架使用vue-cli( 第二层:工程配置层--elementui需要做的基础配置)
vue.js·elementui·前端框架
周bro15 天前
vue2+element ui 中的el-table表格 选中当前行当前行变色,单选/多选--------续集:表格样式修改整合
vue.js·ui·elementui
T畅N15 天前
审批流设计器(前端)
前端·elementui·vue·html·流程图·js