el-table中文字过程时隐藏文字设置

  • 问题描述:el-table中有时候会存在当前行的某列文字过长导致表格拉伸影响美观。因此需要隐藏过长的文字,点击详情时可以展开所有文字内容,点击收起时会隐藏。
    实现效果如下:
    在这里插入图片描述
    点击详情后效果如下:
    在这里插入图片描述

具体实现过程

方式一:
首先我这里设定的是备注的长度大于30个字符串时才显示收起和详情,这里可以根据自己需求进行修改。

<el-table-column prop="memo" label="备注" align="center" min-width="200px" sortable>
      <template slot-scope="scope">
        <!-- v-if备注大于30个字符串的时候,才显示收起和详情 -->
        <div v-if="scope.row.memo.length > 30">
          <div class="ellipsis" v-text="ellipsisText(scope.row.memo, scope.$index)"></div>
          <span v-show="showDetail[scope.$index]"> {{ scope.row.memo }} </span>
          <el-link type="primary" @click="toggleDetail(scope.$index)">
            {{ showDetail[scope.$index] ? '收起' : '详情' }}
          </el-link>
        </div>
        <div v-else>{{ scope.row.memo }}</div>
      </template>
 </el-table-column>

处理方法

export default {
  data() {
    return {
      showDetail: [] // 控制全部文本显隐状态的数组}
    }
  },
  methods: {
    //备注文字过长时显示问题
    ellipsisText(text, index) {
      if (this.showDetail[index]) {
        return '';
      } else if (text.length > 30) { // 文本长度大于 30 时,截取前 30 个字符
        return `${text.slice(0, 30)}...`;
      } else {
        return text;
      }
    },
    toggleDetail(index) {
      this.$set(this.showDetail, index, !this.showDetail[index]); // 利用 Vue.set 设置 showDetail 数组中当前行的显隐状态
    },
  }
}

样式

<style lang="scss" scope>
.ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
}
</style>

方式二:
根据el-table中的show-overflow-tooltip绑定为:show-overflow-tooltip=‘true’。
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

deku-yzh

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值