I am working on a Vuetify 3 data tabe and need to implement a "View More" button for description that exceed three lines. I want to use scrollHeight and clientHeight to determine if the text is truncated and provide an option to expand it.
Vuetify Playground: Playground Link
Here is a simplified version of my implementation:
<template>
<v-data-table :headers="headers" :items="items">
<template #item.description="{ item }">
<div>
<div
ref="descriptionRef"
class="three_lines_description"
:style="{ overflow: isExpanded[item.name] ? 'visible' : 'hidden' }"
>
{{ item.description }}
</div>
<v-btn
v-if="isTruncated[item.name]"
@click="toggleExpand(item.name)"
text
>
{{ isExpanded[item.name] ? 'View Less' : 'View More' }}
</v-btn>
</div>
</template>
</v-data-table>
</template>
<script>
import { ref, onMounted } from 'vue';
export default {
data() {
return {
headers: [
{ title: "Name", key: "name" },
{ title: "Email", key: "email" },
{ title: "Description", key: "description" },
],
items: [
// Sample items...
],
isExpanded: {},
isTruncated: {},
};
},
methods: {
toggleExpand(name) {
this.isExpanded[name] = !this.isExpanded[name];
},
checkTruncation() {
// Logic to check if the text is truncated...
}
},
mounted() {
this.checkTruncation();
}
};
</script>
<style scoped>
.three_lines_description {
overflow: hidden;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
}
</style>
Issue
- The "View More" button does not appear for descriptions that exceed three lines.
- I am unsure how to properly use
refsin v-data-table to determine if the text is truncated.
This is the simplified design of my code now which changes the format for the description column only. What is the correct approach to implement this "View More" functionality in a Vuetify data table?
If the solution works, I will accept it as answer and give an upvote.