我使用 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 许可协议
对我来说,它通过替换表体(
v-slot:body
)与 Vuetify 2.X 一起工作,并手动定义tr
和td
它有助于研究 插槽示例。这样就可以添加点击事件并像这样设置 css 类:
(旁注:这样你就不能在
click:row
v-data-table
了。因为它不会通过覆盖行而被触发……但是定义@click
直接在tr
上工作也很好。)在方法
selectItem
我们将项目保存在数据字段selectedItem
中。单击该行时我们设置的 CSS 类。所以背景发生变化,文字 变粗: