如何在 Vuetify(版本 >= 2.0)中单击时突出显示 v-data-table 中的行?

新手上路,请多包涵

我使用 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

<template slot="headers" slot-scope="props"><template slot="items" slot-scope="props"> 里面 <v-data-table> 似乎被忽略了。

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

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

原文由 Varathron 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 487
1 个回答

对我来说,它通过替换表体( v-slot:body )与 Vuetify 2.X 一起工作,并手动定义 trtd 它有助于研究 插槽示例

这样就可以添加点击事件并像这样设置 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>

(旁注:这样你就不能在 click:row v-data-table 了。因为它不会通过覆盖行而被触发……但是定义 @click 直接在 tr 上工作也很好。)

在方法 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>

原文由 Nick 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题