当前位置: 首页 > news >正文

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

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

  1. 使用 scoped slots:利用 Element UI 提供的 scoped slots 自定义单元格内容。
  2. CSS 样式控制:使用 CSS 的 text-overflow 属性实现文本溢出时显示省略号。
    示例代码
    下面是一个详细的示例,展示如何在 el-table 中应用这些技术:
<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

<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.lryc.cn/news/544950.html

相关文章:

  • 企业微信里可以使用的企业内刊制作工具,FLBOOK
  • 【数据挖掘】Pandas
  • explore与explode词源故事
  • CAM350_安装
  • 51c自动驾驶~合集22
  • games101 作业5
  • 【高并发秒杀系统设计:从Guava到Redis的6级缓存架构演进】
  • 2-程序语言基础知识
  • 【AIGC系列】4:Stable Diffusion应用实践和代码分析
  • 小米火龙CPU和其他几代温度太高的CPU是由谁代工的
  • 在 ASP.NET Core 中压缩并减少图像的文件大小
  • 网络流算法: Dinic算法
  • 【Godot4.3】自定义简易菜单栏节点ETDMenuBar
  • 如何杀死僵尸进程?没有那个进程?
  • Solana 核心概念全解析:账户、交易、合约与租约,高流量区块链技术揭秘!
  • Leetcode-853. Car Fleet [C++][Java]
  • 012 rocketmq事务消息
  • ChatGPT与DeepSeek:开源与闭源的AI模型之争
  • Ollama的底层实现原理分析
  • nginx 动态计算拦截非法访问ip
  • 商业秘密维权有哪些成本开支?
  • 使用UA-SPEECH和TORGO数据库验证自动构音障碍语音分类方法
  • WebSocketHandler 是 Spring Framework 中用于处理 WebSocket 通信的接口
  • Pikachu
  • 如何使用 Jenkins 实现 CI/CD 流水线:从零开始搭建自动化部署流程
  • Vue.js 学习笔记
  • 数据存储:一文掌握RabbitMQ的详细使用
  • 辛格迪客户案例 | 祐儿医药科技GMP培训管理(TMS)项目
  • FreeRtos实时系统: 十六.tickless低功耗模式
  • CSDN博客:Markdown编辑语法教程总结教程(上)