15-el-table表头自适应宽度

原文地址:https://www.cnblogs.com/amnesia999/p/18439754

因某些需求,表格数据需要循环展示,表格宽度需要根据表头自适应,可使用getTextWidth方法

<template>
  <el-table
    :key="tableLoading"
    :data="tableData"
    v-loading="tableLoading"
  >
    <template v-for="(item,index) in classList">
      <el-table-column v-else :key="index" align="center" :label="item.dataName" :width="getTextWidth(item.dataName)">
        <template slot-scope="scope">
           {{scope.row.statisticalQuantityNumberVOS[index].number}}
        </template>
      </el-table-column>
      </template>
  </template>
</el-table>
export default{
  methods:{
    getTextWidth(text){
      const span = document.createElement('span');
      span.style.visibility = 'hidden';
      span.style.position = 'absolute';
      span.style.top = '-9999px';
      span.style.whiteSpace = 'nowrap';
      span.innerText = text;
      document.body.appendChild(span);
      const width = span.offsetWidth+20;
      document.body.removeChild(span);
      if(width<80) return 80
      return width;
    },
  }
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容