首页 > 解决方案 > 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>

通过指定以像素为单位的固定宽度可以实现所需的效果,但我希望表格及其列能够响应。

标签: cssvue.jsvuetify.js

解决方案


如果您改为将截断应用于 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>


推荐阅读