在开发中使用el-table宽度不一致出现抖动问题

1. 问题

点击来回切换时,由于表格列数不一样,会出现抖动的情况
在这里插入图片描述

2. 源代码

<template>
  <div class="check-list">
    <section class="fbetween">
      <div>
        <el-radio-group v-model="type" @change="(value)=>{ handleSearch() }">
          <el-radio-button :label="0">审核中</el-radio-button>
          <el-radio-button :label="2">审核失败</el-radio-button>
        </el-radio-group>
      </div>
    </section>

    <!-- 表格 -->
    <div class="option-table mgt_20" v-loading="loading">
      <el-table :data="tableData" border @selection-change="tableSelect" ref="i-table">
        <el-table-column type="selection" width="50" align="center"></el-table-column>
        <el-table-column v-for="item in column" :key="item.id" align="center" :prop="item.prop" :label="item.label" :min-width="item.width" show-overflow-tooltip>
          <template slot-scope="{ row }">
            <template v-if="item.prop == 'businessLicenseUrl'">
              <div class="license-box fcc">
                <el-image class="img" :src="row[item.prop] || ''" :preview-src-list="[row[item.prop]]"></el-image>
                <div class="pre-icon pointer">
                  <i class="el-icon-zoom-in icon"></i>
                </div>
              </div>
            </template>
            <template v-else-if="item.prop == 'auditStatus'">
              <span>{{auditStatusList[row[item.prop]]}}</span>
            </template>
            <span v-else>{{row[item.prop] || '-'}}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" fixed="right" width="220">
          <template slot-scope="{row}">
            <el-button type="text">查看详情</el-button>
            <el-button type="text">通过</el-button>
            <el-button type="text" class="rejectBtn">拒绝</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
export default {
  components: { InsDetail, RejectDialog, Pagination },
  data() {
    return {
      type: 0,
      tableData: [],
      loading: false,
      selectedIds: [],
    }
  },
  computed: {
    column() {
      const res = [
        { id: 1, label: '机构ID', prop: 'id', width: '120', type: [0, 2] },
        { id: 2, label: '机构名称', prop: 'name', width: '120', type: [0, 2] },
        { id: 3, label: '营业执照', prop: 'businessLicenseUrl', width: '150', type: [0, 2] },
        { id: 4, label: '联系人', prop: 'userName', width: '120', type: [0, 2] },
        { id: 5, label: '联系电话', prop: 'userPhone', width: '200', type: [0, 2] },
        { id: 6, label: '联系人邮箱', prop: 'userEmail', width: '200', type: [0, 2] },
        { id: 7, label: '行业技术领域', prop: 'domainField', width: '150', type: [0, 2] },
        { id: 8, label: '提交入驻申请时间', prop: 'createTime', width: '200', type: [0, 2] },
        { id: 9, label: '审核状态', prop: 'auditStatus', width: '150', type: [0, 2] },
        { id: 10, label: '审核失败回复', prop: 'remark', width: '150', type: [2] },
      ]
      return res.filter(item => item.type.includes(this.type));
    }
  },
  
  mounted() {
    this.getData();
  },
  methods: {
     async getData() {
     //通过接口获取表格数据
     }
  }

3.解决方法

  //解决表格抖动问题
  beforeUpdate() {
    this.$nextTick(() => {
      this.$refs['i-table'].doLayout()
    })
  },

使用Element UI的`el-table`和`el-descriptions`组件时,可能会遇到`el-table`的宽度无法自适应的问题。这通常是由于`el-descriptions`的布局限制导致的。以下是一些可能的解决方案: 1. **使用`width`属性**: 确保`el-table`设置了`width`属性,并且该属性值是一个百分比或者是一个具体的像素值。例如: ```html <el-descriptions> <el-descriptions-item label="描述"> <el-table :data="tableData" style="width: 100%;"> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </el-descriptions-item> </el-descriptions> ``` 2. **使用`max-width`属性**: 如果`width`属性无法解决问题,可以尝试使用`max-width`属性来限制表格的最大宽度,并结合`overflow`属性来处理溢出的内容。 ```html <el-descriptions> <el-descriptions-item label="描述"> <el-table :data="tableData" style="max-width: 100%; overflow: auto;"> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </el-descriptions-item> </el-descriptions> ``` 3. **使用`flex`布局**: 通过设置父容器的`display`属性为`flex`,并结合`flex`相关的属性来调整子元素的行为。 ```html <el-descriptions> <el-descriptions-item label="描述" style="display: flex; flex-direction: column;"> <el-table :data="tableData" style="flex: 1;"> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </el-descriptions-item> </el-descriptions> ``` 4. **使用`el-row`和`el-col`**: 通过`el-row`和`el-col`组件来布局表格,使其在同屏幕尺寸下自适应。 ```html <el-descriptions> <el-descriptions-item label="描述"> <el-row> <el-col :span="24"> <el-table :data="tableData"> <el-table-column prop="date" label="日期" width="180"></el-table-column> <el-table-column prop="name" label="姓名" width="180"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </el-col> </el-row> </el-descriptions-item> </el-descriptions> ``` 通过以上方法,可以尝试解决`el-table`在`el-descriptions`中无法自适应宽度问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值