vue表格显示字符串过长问题解决

本文介绍了在Vue项目中处理表格内字符串过长的三种方法:1) 使用el-collapse,但样式可能不佳;2) 通过el-tooltip,限制div宽度,样式有所改善;3) 结合el-tooltip和字符串截取,提供更好的展示效果。推荐使用第三种方案,适用于多种场景。
摘要由CSDN通过智能技术生成

vue表格显示字符串过长问题解决

做项目时,通常我们会遇到字符串过长导致样式不好看的问题,这里有三种方式处理,第三种为最佳方案。

第一种

首先我们看下未做处理的样式:
在这里插入图片描述
可以看到学院字段过长,当然我引用的el-table自带的样式给自动换行了,但如果自己写的table会导致样式很丑。我们可以用el-collapse来进行字符串处理,代码如下:

<el-table-column prop="collegeName"
                           align="center"
                           width="150"
                           label="学院">
                        <template slot-scope="scope">
                            <el-collapse v-if="scope.row.collegeName !== null">
                                <el-collapse-item :title="scope.row.collegeName.substring(0,6)">
                                    <div>{{scope.row.collegeName}}</div>
                                </el-collapse-item>
                            </el-collapse>
                        </template>
                    </el-table-column>

但是这个样式非常丑
在这里插入图片描述
在这里插入图片描述

第二种

用el-tooltip处理,但是这种样式有限制,必须给定div宽度,否则不能实现隐藏,而且完整字段的位置会出现偏移,还是原字符串长度的中部,但是样式比第一种好看一些。

<div style="width: 100px;height: 20px;float: left">
                                        <el-tooltip class="item" effect="dark" :content="scope.row.product.name" placement="top-start">
                                            <span style="max-width: 6em;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">{{scope.row.product.name}}</span>
                                        </el-tooltip>
                                    </div>

在这里插入图片描述

第三种

终极方案,依旧使用el-tooltip,只是结合一下一二种方式。代码如下:

<el-table-column prop="collegeName"
                                     align="center"
                                     width="150"
                                     label="学院">
                        <template slot-scope="scope">
                            <el-tooltip class="item" effect="dark" :content="scope.row.collegeName" placement="top-start">
                                <span>{{scope.row.collegeName.substring(0,8)}}...</span>
                            </el-tooltip>
                        </template>
                    </el-table-column>

我们可通过控制substring的第二个参数来控制显示字符串的长度,结果如下:
在这里插入图片描述
ok,以上就是三种处理方式,推荐使用第三种,适用多个场景,不仅是表格。

如果你需要在 Vue 表格中对字符串进行操作,可以使用 `computed` 计算属性来实现。 例如,假设我们有一个表格列 `description`,需要将其中的字符串中的大写字母转换为小写字母,并去掉空格和特殊字符。可以这样写: ```html <template> <div> <table> <thead> <tr> <th>Description</th> </tr> </thead> <tbody> <tr v-for="item in items" :key="item.id"> <td>{{ descriptionFormatted(item.description) }}</td> </tr> </tbody> <tfoot> <tr> <td>{{ descriptionSummary }}</td> </tr> </tfoot> </table> </div> </template> <script> export default { data() { return { items: [ { id: 1, description: 'This is a test' }, { id: 2, description: 'Another Test!' }, { id: 3, description: 'Testing 123' }, ], } }, computed: { descriptionSummary() { let summary = '' this.items.forEach((item) => { summary += item.descriptionFormatted.replace(/\s+/g, '') + ' ' }) return summary }, }, methods: { descriptionFormatted(description) { return description.toLowerCase().replace(/\s+/g, '').replace(/[^a-zA-Z0-9]/g, '') }, }, } </script> ``` 这里使用了 `descriptionFormatted` 方法来格式化 `description` 字符串,将其中的大写字母转换为小写字母,并去掉空格和特殊字符。在模板中使用 `{{ descriptionFormatted(item.description) }}` 来渲染表格数据。 另外,在表格的汇总列中,可以使用 `computed` 计算属性来计算汇总结果。在上面的例子中,我们使用 `descriptionSummary` 计算属性来计算所有 `description` 字符串的格式化结果,并将其拼接在一起。在模板中使用 `{{ descriptionSummary }}` 来渲染汇总结果。 注意,如果需要对字符串进行复杂的操作,建议使用正则表达式来实现。上面的例子中,我们使用了 `/[^a-zA-Z0-9]/g` 正则表达式来匹配除了字母和数字以外的字符,并将其替换为空字符串
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

我超爱写bug

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

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

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

打赏作者

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

抵扣说明:

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

余额充值