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,
})

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

相关推荐
chushiyunen8 天前
vue el-pagination实现分页
javascript·vue.js·elementui
一坨阿亮9 天前
使用e-tree开发树形穿梭框
javascript·vue.js·elementui
北极星日淘12 天前
可买免税店货物与安耐晒——特殊商品代购技术方案
javascript·vue.js·elementui
chushiyunen13 天前
vue插件element ui,element plus,ElMessage消息框,ref,动态绑定语法
vue.js·ui·elementui
流浪码农~13 天前
Element Plus DatePicker 动态设置每周起始日
前端·vue.js·elementui
依托偶尔宁16 天前
element-plus:el-table设置展开图标所在列的位置
前端·elementui
吠品17 天前
PyTorch 踩坑:libtorch_cpu.so 找不到 iJIT_NotifyEvent 符号
前端·vue.js·elementui
zhangyao9403301 个月前
开发pc端时,表格的高度怎么设置才能铺满页面
前端·javascript·elementui
mengqudoh1 个月前
elementui el-table 表头固定功能
javascript·vue.js·elementui
陪小甜甜赏月1 个月前
ElementPlus 多个并列 Table 独立全选/取消全选 (适配嵌套表格业务)
前端·vue.js·elementui