vue el-table-column 单元表格的 省略号 实现

server/2025/3/2 0:52:33/

要对 el-table-column 的某一列中的每个单元格值进行处理,使其在文本内容超出指定宽度时显示省略号(…),可以通过以下方法实现:

  1. 使用 scoped slots:利用 Element UI 提供的 scoped slots 自定义单元格内容。
  2. CSS 样式控制:使用 CSS 的 text-overflow 属性实现文本溢出时显示省略号。
    示例代码
    下面是一个详细的示例,展示如何在 el-table 中应用这些技术:
javascript"><template><div><el-table :data="tableData" style="width: 100%"><el-table-columnprop="name"label="Name"width="200"></el-table-column><el-table-columnprop="description"label="Description"width="300"><template slot-scope="scope"><div class="overflow-ellipsis">{{ scope.row.description }}</div></template></el-table-column></el-table></div>
</template><script>
export default {data() {return {tableData: [{ name: 'John', description: 'This is a very long description that should be truncated with ellipsis.' },{ name: 'Jane', description: 'Another long text example for testing the ellipsis.' }// other rows...]};}
}
</script>
<style scoped>
.overflow-ellipsis {white-space: nowrap;overflow: hidden;text-overflow: ellipsis;width: 100%; /* Ensure it takes the full width of the column */
}
</style>

例子2

javascript"><el-table-column prop="relationPhoneNum" label="号码总数" width="80" align="left" show-overflow-tooltip>
<template slot-scope="scope"><div class="overflow-ellipsis" v-for="(item,index) in scope.row.relationPhoneNum" :key="index"> {{ item }} </div>
</template></el-table-column>/script>
<style scoped>
.overflow-ellipsis {white-space: nowrap;overflow: hidden;text-overflow: ellipsis;width: 100%; /* Ensure it takes the full width of the column */
}
</style>

解释
Scoped Slots:在 el-table-column 元素中,我们使用 来自定义每个单元格的渲染方式。scope.row.description 用来访问表格当前行的 description 字段。

CSS 样式:

.overflow-ellipsis 类应用了 white-space: nowrap; 防止文本自动换行。
overflow: hidden; 隐藏溢出的文本部分。
text-overflow: ellipsis; 指定文本超出容器时显示省略号。
宽度管理:确保为 el-table-column 设置了 width 属性,以便固定列宽,从而保证 CSS 的生效。

注意事项
样式作用域:如果你希望该样式仅影响特定的表格组件,确保将样式放置在具有 scoped 属性的

宽度的一致性:确保 CSS 样式中设置的宽度与 el-table-column 的宽度保持一致,如果需要,可以将 CSS 宽度设置为 width: 100%;。

这样,你就能在 Vue 应用中使用 Element UI 表格实现指定列内容溢出时自动加省略号,并确保每一行都有效。


http://www.ppmy.cn/server/171665.html

相关文章

【华为OD机考】华为OD笔试真题解析(16)--微服务的集成测试

题目描述 现在有n个容器服务&#xff0c;服务的启动可能有一定的依赖性&#xff08;有些服务启动没有依赖&#xff09;&#xff0c;其次&#xff0c;服务自身启动加载会消耗一些时间。 给你一个 n n n \times n nn的二维矩阵useTime&#xff0c;其中useTime[i][i]10表示服务…

Java集合性能优化面试题

Java集合性能优化面试题 初始化优化 Q1: 如何优化集合的初始化&#xff1f; public class CollectionInitializationExample {// 1. 合理设置初始容量public void initializationOptimization() {// 不好的实践&#xff1a;使用默认容量List<String> badList new Arr…

如何利用爬虫获取淘宝评论API接口:技术解析与实战指南

在电商领域&#xff0c;商品评论数据是商家优化产品、提升用户体验以及进行市场分析的关键资源。淘宝作为国内领先的电商平台&#xff0c;提供了丰富的API接口&#xff0c;允许开发者通过编程方式获取商品评论信息。本文将详细介绍如何利用Python爬虫技术调用淘宝评论API接口&a…

pytest下放pytest.ini文件就导致报错:ERROR: file or directory not found: #

pytest下放pytest.ini文件就导致报错&#xff1a;ERROR: file or directory not found: # 如下&#xff1a; 项目文件目录如下&#xff1a; pytest.ini文件内容&#xff1a; [pytest] addopts -v -s --alluredir ./allure-results # 自动添加的命令行参数&#xff1a;# -…

Deepseek 实战全攻略,领航科技应用的深度探索之旅

想玩转 Deepseek&#xff1f;这攻略别错过&#xff01;先带你了解它的基本原理&#xff0c;教你搭建运行环境。接着给出自然语言处理、智能客服等应用场景的实操方法与代码。还分享模型微调、优化技巧&#xff0c;结合案例加深理解&#xff0c;让你全面掌握&#xff0c;探索科技…

STM32-智能台灯项目

一、项目需求 1. 红外传感器检测是否有人&#xff0c;有人的话实时检测距离&#xff0c;过近则报警&#xff1b;同时计时&#xff0c;超过固定时间则报警&#xff1b; 2. 按键 1 切换工作模式&#xff1a;智能模式、按键模式、远程模式&#xff1b; 3. 智能模式下&#xff0c;根…

本地部署AI大模型之PyTorch:如何使用whl文件安装PyTorch

如果想在本地安装只支持CPU的PyTorch&#xff0c;可以参考这篇博客。 我们需要安装支持CUDA 12.6版本的PyTorch&#xff0c;但是我们在直接使用官网上的指令("pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126")安装…

探索超声波的奥秘——定时器与PCA

超声波技术的诞生灵感来源于大自然中的回声定位现象&#xff0c;尤其是蝙蝠的独特能力。蝙蝠通过发出高频超声波并捕捉回声来精确地探测周围的物体和猎物&#xff0c;即使在漆黑的夜晚也能轻松导航。 在单片机中&#xff0c;也有着超声波这个模块&#xff0c;它在单片机上的标识…