ellipsisPopover.vue
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<template>
<el-popover
v-if="isTextLong"
placement="top"
popper-class="base-popper"
trigger="hover"
:content="content"
>
<span ref="textRef" slot="reference" class="line-ellipsis">{{ truncatedText }}</span>
</el-popover>
<span v-else>{{ content || '-' }}</span>
</template>
<script>
export default {
name: 'EllipsisPopover',
props: {
content: {}
},
data() {
return {
maxWidth: 0
}
},
computed: {
truncatedText() {
const maxLength = Math.ceil(this.maxWidth / 12);
return this.content?.length > maxLength ? this.content.slice(0, maxLength) + '...' : this.content;
},
isTextLong() {
const maxLength = Math.ceil(this.maxWidth / 12);
return this.content?.length >= maxLength;
},
},
mounted() {
this.$nextTick(() => {
this.maxWidth = this.$refs.textRef?.getBoundingClientRect().width - 30 // 获取元素宽度
})
}
}
</script>
<style scoped>
.line-ellipsis {
display: inline-block;
width: 100%;
white-space: nowrap; /* 不换行 */
overflow: hidden; /* 隐藏超出部分 */
text-overflow: ellipsis; /* 显示省略号 */
}
</style>