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

标签 javascript css vue.js vuetify.js

我使用 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 等任何开发环境中都没有。

最佳答案

对我来说,它通过替换表体 (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>

关于javascript - 如何在 Vuetify(版本 >= 2.0)中单击时突出显示 v-data-table 中的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57490177/

相关文章:

JavaScript 文档.write : Uncaught SyntaxError: Unexpected end of input

javascript - 单击删除按钮时从无序列表中删除特定列表项

javascript - IE 不喜欢 URL 变量?

javascript - Vue.js "npm run build"但 Vue.js 未绑定(bind)到 DOM/工作

javascript - nginx:将所有请求发送到单个 html 页面

javascript - 动态改变弹出窗口的位置

css - Bootstrap - 容器中的中心选项卡

css - 为什么我的 @media 中的 div 高度不会改变?

vue.js - 无法阻止 Vue JS (v2) 重用组件

javascript - 对我生成的每个索引创建不同的 V-MODEL