css - Vuetify 数据表组件 - 使用 CSS 截断单元格中的文本
问题描述
在下面的例子中,有没有办法使用 CSS 来截断单元格中的文本(而不是让它环绕或溢出)?理想情况下,CSS 应该如下所示:
.truncate {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
但正如您在下面看到的,这只会导致单元格占用所有空间:
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<style>
.truncate {
display: inline-block;
/* width: 200px; */
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<div id="app">
<v-app>
<v-content>
<v-data-table :headers="headers" :items="items">
<template v-slot:item.name="{ item }">
<span class="truncate">{{ item.name}}</span>
</template>
</v-data-table>
</v-content>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
<script>
new Vue({
el: '#app',
vuetify: new Vuetify(),
data: {
headers: [
{ text: 'Name', value: 'name', width: "75%" },
{ text: 'Calories', value: 'calories', width: "25%" },
],
items: [
{ name: 'Frozen Yogurt', calories: 159, },
{ name: 'Ice cream sandwich with a really, really, really long name that keeps going on and on and on forever so there is no space left', calories: 237, },
{ name: 'Eclair', calories: 262, },
], }
})
</script>
</body>
</html>
通过指定以像素为单位的固定宽度可以实现所需的效果,但我希望表格及其列能够响应。
解决方案
如果您改为将截断应用于 td,并应用魔术技巧max-width: 1px
,您将获得所需的结果。
在下面的示例中,为了将类应用于td
您必须使用项目槽并自己创建行。
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/font@4.x/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, minimal-ui">
<style>
.truncate {
max-width: 1px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
</style>
</head>
<body>
<div id="app">
<v-app>
<v-content>
<v-data-table :headers="headers" :items="items">
<template v-slot:item="{ item }">
<tr>
<td class="truncate">{{ item.name}}</td>
<td>{{ item.calories}}</td>
</tr>
</template>
</v-data-table>
</v-content>
</v-app>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>
<script>
new Vue({
el: '#app',
vuetify: new Vuetify(),
data: {
headers: [{
text: 'Name',
value: 'name',
width: "75%"
},
{
text: 'Calories',
value: 'calories',
width: "25%"
},
],
items: [{
name: 'Frozen Yogurt',
calories: 159,
},
{
name: 'Ice cream sandwich with a really, really, really long name that keeps going on and on and on forever so there is no space left',
calories: 237,
},
{
name: 'Eclair',
calories: 262,
},
],
}
})
</script>
</body>
</html>
推荐阅读
- python - Python不读取文件夹的图片
- windows - 为什么我的应用程序窗口在部署时会缩小?
- python-3.x - numpy ndarray dtype 转换失败
- ios - 在命名类、函数和变量时使用冠词 (the/a/an) (Swift)
- curl - 向googleapis发送请求,响应为空
- mysql - 获取别名 sql 的总和
- reactjs - 如何将 d3 与 reactJS / nextJS 一起使用?
- reactjs - 如何在 ReactJS 中使用 ref 更新输入值
- http - x/net/http2:ConnState 无法跟踪
- laravel - 如何使用控制器和复选框在 laravel 8 中轻松创建过滤器