vue3实现elementui表格操作栏宽度自适应

根据表格的Table-column 属性, 我们可以在操作栏中添加 :render-header="renderHeader"

然后通过js获取当前操作栏存在多少个按钮,去动态计算需要设置列宽为多少;

<el-table-column fixed="right" :label="t('table.operation')"  :render-header="renderHeader" > 
      <template #default="scope">
        <div class="optionDiv" style="white-space: nowrap; display: inline-block">
         <el-button type="success" size="small" :loading="scope.row.loading"
           @click="prmContractC.onDetails(scope.row, false)">
           详情
         </el-button>
         <el-button type="primary" size="small" :loading="scope.row.loading" @click="submitBtn(scope.row.id)"
           v-if="(scope.row.workflowState == '0' || scope.row.workflowState == '6' || scope.row.workflowState == '11') && userId == scope.row.createBy">
           提交审批
         </el-button>
         <el-button type="primary" size="small" :loading="scope.row.loading" @click="removeBtn(scope.row.id)"
           v-if="scope.row.work && scope.row.work.data && scope.row.work.data.length > 0 && scope.row.work.data[0].canCancel">
           撤回
         </el-button>
         <el-button type="primary" size="small" :loading="scope.row.loading"
           v-if="scope.row.work && scope.row.work.data && scope.row.work.data.length > 0 && scope.row.work.data[0].canSubmit"
           @click="prmContractC.onDetails(scope.row, true)">
           审核
         </el-button>
       </div>
     </template>
   </el-table-column>
import {h} from "vue";//引入h
export const renderHeader = ({column}: any) => {
  const divElement = document.querySelectorAll('.optionDiv');
  // 初始化最大按钮数量
  let maxButtons = -1;
  // 初始化包含最多按钮的元素
  let divWithMostButtons;
  divElement.forEach((item, index) => {
    // 获取当前 元素中的所有 button 元素
    const buttons = item.querySelectorAll('button');
    // 如果当前中的 button 数量大于已记录的最大数量
    if (buttons.length > maxButtons) {
      // 更新最大数量
      maxButtons = buttons.length;
      // 记录包含最多按钮的 
      divWithMostButtons = item;
    }
  });
  if(divWithMostButtons){
    let widthArr = [];
    const buttons = divWithMostButtons.querySelectorAll('button');
    buttons.forEach(function (item) {
      if (item.innerText) {
        widthArr.push(item.offsetWidth);
      }
    });
    // 重新设置列标题及宽度属性
    if (widthArr.length == 1) {
      column.width = widthArr.reduce((accumulator, currentValue) => accumulator + currentValue, 0)+20;
      return h('span', column.label);
    }else if (widthArr.length > 1) {
      column.width = widthArr.reduce((accumulator, currentValue) => accumulator + currentValue, 0) + 30;
      return h('span', column.label);
    } else {
      column.width = 0;
      return h('span', column.label);
    }
  }
};

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue2.0和ElementUI2.0中使用el-table表格组件时,自适应高度可以通过以下步骤来实现: 1. 确定数据源 首先,我们需要确定用于填充表格的数据源。在el-table中,数据通常以数组的形式传递,其中每个数组元素代表表格中的一行数据。 2. 计算表格高度 在Vue中,我们可以使用计算属性来动态计算表格的高度。计算属性会根据数据源的变化自动更新表格高度。例如,我们可以使用下面的代码来计算表格高度: computed: { tableHeight: function() { // 获取窗口可见区域高度 var viewportHeight = window.innerHeight; // 计算表格头和分页组件的高度 var tableHeaderHeight = this.$refs.table.$el.querySelector('.el-table__header-wrapper').offsetHeight; var paginationHeight = this.$refs.pagination.$el.offsetHeight; // 计算表格内容区域的高度 var contentHeight = viewportHeight - tableHeaderHeight - paginationHeight - 20; return contentHeight; } } 在上面的代码中,我们首先获取窗口可见区域的高度,然后根据表格头和分页组件的高度计算表格内容区域的高度。最后,将计算出的高度返回。 3. 绑定表格高度 我们将计算属性中计算出的表格高度绑定到el-table组件中的height属性上,以实现自适应高度。例如,我们可以使用下面的代码将表格高度绑定到el-table组件上: <el-table :data="tableData" ref="table" :height="tableHeight"> 在上面的代码中,我们首先使用data属性传递数据源,然后使用ref属性给el-table组件添加一个引用,最后使用height属性将计算属性中计算出的表格高度绑定到组件上。 通过上述方法,我们可以在Vue2.0和ElementUI2.0中实现自适应高度的el-table表格组件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值