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-column
        prop="name"
        label="Name"
        width="200">
      </el-table-column>
      <el-table-column
        prop="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 表格实现指定列内容溢出时自动加省略号,并确保每一行都有效。

Vue 中使用 el-table 实现自适应文本宽度且不显示省略号有两种方法。 ### 方法一:根据标题字数和字体尺寸计算宽度 在 HTML 部分,使用 `el-table` 和 `el-table-column`,并绑定 `:render-header` 到 `linefeed` 方法: ```html <el-table> <el-table-column label="根据标题字数/大小自适应宽度" :render-header="linefeed"></el-table-column> </el-table> ``` 在 JavaScript 的 `methods` 中定义 `linefeed` 方法,通过计算表头字数和字体尺寸来设置表头的最小宽度: ```javascript methods: { linefeed (h,{column,index}) { // column,index都是el-table中自带的 let number = column.label.length; // 表头字数 let size = 16; // 字体尺寸 column.minWidth = number * size; // 计算宽度 return h('div',{class:'table-head',style:{width:'100%'}},[column.label]); } } ``` 此方法的核心是通过表头的字数和预设的字体尺寸计算出表头的最小宽度,避免出现省略号[^1]。 ### 方法二:通过创建临时元素获取实际宽度 在模板部分,同样使用 `el-table` 和 `el-table-column`,并绑定 `:render-header` 到 `renderHeader` 方法: ```html <el-table> <el-table-column label="根据标题字数/大小自适应宽度" :render-header="renderHeader"></el-table-column> </el-table> ``` 在 `methods` 中定义 `renderHeader` 方法,创建一个临时的 `span` 元素,将表头文本放入其中,然后获取该元素的实际宽度,以此来设置表头的最小宽度: ```javascript methods: { // 设置头不换行 renderHeader(h, { column, $index }) { let realWidth = 0; const span = document.createElement('span'); span.innerText = column.label; document.body.appendChild(span); realWidth = span.getBoundingClientRect().width; column.minWidth = column.minWidth > realWidth ? column.minWidth : realWidth; // 可能还有边距/边框等值,需要根据实际情况加上 document.body.removeChild(span); return h('span', column.label); } } ``` 该方法通过创建临时元素并获取其实际宽度,更加精确地设置表头的最小宽度,从而避免省略号的出现[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值