【问题标题】:How to highlight rows in v-data-table on click in Vuetify (version >= 2.0)?如何在 Vuetify(版本> = 2.0)中单击时突出显示 v-data-table 中的行?
【发布时间】:2019-12-20 17:51:51
【问题描述】:

我使用v-data-table 来管理电子邮件。用户可以单击一行,然后会出现一个包含电子邮件详细信息的弹出窗口。

我想要的东西:
单击这些行后,我希望将行标记为“已读”(因此 css 粗体/非粗体)。

问题:
我已经在这里找到了一些示例(但仅适用于较旧的 Vuetify 版本):Vuetify - How to highlight row on click in v-data-table

这个示例(以及我发现的所有其他示例)使用 v-data-table 的扩展代码 - 比如:

<v-data-table :headers="headers" :items="desserts" class="elevation-1">
  <template v-slot:items="props">
    <tr @click="activerow(props.item)" :class="{'primary': props.item.id===selectedId}">
      <td>{{ props.item.name }}</td>
      <td class="text-xs-right">{{ props.item.calories }}</td>
      <td class="text-xs-right">{{ props.item.fat }}</td>
      <td class="text-xs-right">{{ props.item.carbs }}</td>
      <td class="text-xs-right">{{ props.item.protein }}</td>
      <td class="text-xs-right">{{ props.item.iron }}</td>
    </tr>
  </template>
</v-data-table>

所以扩展代码是:

<template v-slot:items="props">
  <tr @click="activerow(props.item)" :class="{'primary': props.item.id===selectedId}">
    <td>{{ props.item.name }}</td>
    <td class="text-xs-right">{{ props.item.calories }}</td>
    <td class="text-xs-right">{{ props.item.fat }}</td>
    <td class="text-xs-right">{{ props.item.carbs }}</td>
    <td class="text-xs-right">{{ props.item.protein }}</td>
    <td class="text-xs-right">{{ props.item.iron }}</td>
  </tr>
</template>

但是自从我使用 Vutify 版本 2 之后

&lt;template slot="headers" slot-scope="props"&gt;&lt;template slot="items" slot-scope="props"&gt; 里面 &lt;v-data-table&gt; 似乎被忽略了。

Vuetify 2 提供了一些新的插槽,但我还没有找到如何在这个示例中使用它们。

谁能提供 Vuetify >= 2.0 的示例? 相信我,目前还没有更高版本可用 - 在 CodePen 或 JSFiddle 等任何开发环境中都没有。

【问题讨论】:

  • 你也许可以使用click:row事件,但是如果你想在刷新页面时保持点击状态,你可能需要使用v-slot方案。
  • 嗨,Flame,我也使用click:row,但还没有解决方案。如果您能在 CodePen 中提供一个可行的示例,那就太好了。
  • 尝试以下方式:@click="$event.target.classList.add('clicked')" 为您刚刚单击的元素添加 clicked

标签: javascript css vue.js vuetify.js


【解决方案1】:

对我来说,它通过替换表体 (v-slot:body) 并手动定义 trtd 与 Vuetify 2.X 一起使用。它有助于研究Slot Example

这样就可以添加点击事件并像这样设置css类:

<template v-slot:body="{ items }">
    <tbody>
      <tr v-for="item in items" :key="item.name" @click="selectItem(item)" :class="{'selectedRow': item === selectedItem}">
        <td>{{ item.name }}</td>
        <td>{{ item.age }}</td>
      </tr>
    </tbody>
</template>

(旁注:这样您就不能再在v-data-table 上使用事件click:row。因为它不会通过覆盖行而被触发...但是直接在tr 上定义@click 效果很好也是。)

在方法selectItem中,我们将项目保存在数据字段selectedItem中。

data () {
  return {
    selectedItem: null,
    ...
  }
},
methods: {
    selectItem (item) {
      console.log('Item selected: ' + item.name)
      this.selectedItem = item
    }
}

点击行时我们设置的 CSS 类。所以背景发生了变化,文字变得粗体

<style scoped>
.selectedRow {
    background-color: red;
    font-weight: bold;
}
</style>

【讨论】:

  • 这不是一个很好的解决方案。当您定义自定义标题、项目或正文插槽时,它将完全接管组件的内部呈现,这将需要您重新实现选择和扩展等功能。
【解决方案2】:

我添加了接收项目的方法selectRow,并为其添加了isSelected 属性。然后在template 中,如果项目具有属性isSelected,我分配类.primary

注意:此方法还会从先前选定的项目中删除 isSelected 属性。这样只能同时突出显示一个&lt;tr&gt;

new Vue({
  el: "#app",
  methods: {
    selectRow(item) {
      // remove isSelected from already selected item
      // const prevItem = this.desserts.find(dessert => dessert.isSelected);
      // if (prevItem) this.$delete(prevItem, 'isSelected');
      this.$set(item, "isSelected", true)
    }
  },
  data() {
    return {
      headers: [{
          text: 'Dessert (100g serving)',
          align: 'left',
          sortable: false,
          value: 'name'
        },
        {
          text: 'Calories',
          value: 'calories'
        },
        {
          text: 'Fat (g)',
          value: 'fat'
        },
        {
          text: 'Carbs (g)',
          value: 'carbs'
        },
        {
          text: 'Protein (g)',
          value: 'protein'
        },
        {
          text: 'Iron (%)',
          value: 'iron'
        }
      ],
      desserts: [{
          name: 'Frozen Yogurt',
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: '1%'
        },
        {
          name: 'Ice cream sandwich',
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          iron: '1%'
        },
        {
          name: 'Eclair',
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
          iron: '7%'
        },
        {
          name: 'Cupcake',
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: 4.3,
          iron: '8%'
        },
        {
          name: 'Gingerbread',
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: 3.9,
          iron: '16%'
        },
        {
          name: 'Jelly bean',
          calories: 375,
          fat: 0.0,
          carbs: 94,
          protein: 0.0,
          iron: '0%'
        },
        {
          name: 'Lollipop',
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: 0,
          iron: '2%'
        },
        {
          name: 'Honeycomb',
          calories: 408,
          fat: 3.2,
          carbs: 87,
          protein: 6.5,
          iron: '45%'
        },
        {
          name: 'Donut',
          calories: 452,
          fat: 25.0,
          carbs: 51,
          protein: 4.9,
          iron: '22%'
        },
        {
          name: 'KitKat',
          calories: 518,
          fat: 26.0,
          carbs: 65,
          protein: 7,
          iron: '6%'
        }
      ]
    }
  },
})
.primary,
.primary:hover {
  /** avoid using !important, added just for example**/
  background-color: red !important;
}

.as-console-wrapper {
  display: none !important;
}
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900|Material+Icons" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/vuetify@1.x/dist/vuetify.min.css" rel="stylesheet">
<div id="app">
  <v-data-table :headers="headers" :items="desserts" class="elevation-1">
    <template v-slot:items="props">
        <tr @click="selectRow(props.item)" :class="{ 'primary': props.item.isSelected }">
        <td>{{ props.item.name }}</td>
        <td>{{ props.item.calories }}</td>
        <td>{{ props.item.fat }}</td>
        <td>{{ props.item.carbs }}</td>
        <td>{{ props.item.protein }}</td>
        <td>{{ props.item.iron }}</td>
        </tr>
      </template>
  </v-data-table>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vuetify@1.x/dist/vuetify.js"></script>

【讨论】:

  • 您好 Java,您的示例完全适用于 Vuetify 1.5,但不幸的是不适用于较新的版本 > 2.0。这就是我试图找出的 - 它如何与最新版本一起使用。
  • @Varathron,我可以使用 Vuetify 2.x 来补充我的答案。
【解决方案3】:

我知道这是一个有点老的问题,但是您可以使用 vuetify 的 item-class@click:row 属性来处理行点击并设置自定义行类。

【讨论】:

    猜你喜欢
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 2019-07-08
    • 2021-05-08
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 2021-05-20
    相关资源
    最近更新 更多