【问题标题】:Vuetify data table component - Truncate text in cell using CSSVuetify 数据表组件 - 使用 CSS 截断单元格中的文本
【发布时间】:2020-07-16 05:30:39
【问题描述】:

在下面的例子中,有没有办法使用 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>

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

【问题讨论】:

    标签: css vue.js vuetify.js


    【解决方案1】:

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

    【讨论】:

    • 令人印象深刻!我见过max-width: 1px hack,但我不确定如何在这种情况下应用它。它可靠吗?我猜没有办法解决它?
    • 说实话,我不知道。我记得我的一个旧项目中的 hack。
    • 很公平!我将把这个问题留待更长时间,看看是否有人知道没有“黑客”的方法。尽管如此,这是获得我迄今为止所看到的所需行为的最佳方式......
    • 没关系。很高兴自己看到替代品!
    • 好吧,我发现max-width: 1px hack 在实践中运行良好,而且似乎没有更好的方法来实现所需的行为,所以我将接受这个答案.
    【解决方案2】:

    通过将 css max-with 值设置为响应式 vw 值,它对我有用:

        .truncate {
           max-width: 50vw;
           white-space: nowrap;
           overflow: hidden;
           text-overflow: ellipsis;
        }
    

    【讨论】:

      【解决方案3】:

      如果您只是将 SPAN 更改为 DIV 并取消注释 css 中的宽度,您的代码就可以工作。

      <!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 }">
              <div class="truncate">{{ item.name}}</div>
            </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>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-07
        • 1970-01-01
        • 1970-01-01
        • 2015-10-26
        • 2019-02-23
        • 1970-01-01
        • 2021-02-21
        • 2020-08-31
        相关资源
        最近更新 更多