我用v-data-table
来管理电子邮件。用户可以单击一行并显示带有电子邮件详细信息的弹出窗口。
我想要的东西:
我希望在单击这些行之后,将行标记为"readed“(因此css粗体/非粗体)。
问题:
我已经在这里找到了一些例子(但只适用于较旧的Vuetify版本):Vuetify如何突出显示v-data表中单击的行
这个示例(以及我找到的所有其他示例)使用扩展代码来处理类似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提供了一些新的插槽,但我还没有找到如何在这个示例中使用它们。
有谁能为>= 2.0提供一个例子吗?相信我,目前还没有更高版本可用的人--在CodePen或JSFiddle等任何开发环境中都没有。
发布于 2019-08-29 21:35:53
对我来说,它通过替换表体(v-slot:body
)和手动定义tr
和td
来与Vuetify 2.X一起工作。这有助于槽例的研究。
这样就可以添加click事件并设置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
上使用事件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>
发布于 2019-08-14 11:04:00
我添加了接收项的方法selectRow
,并向其添加了isSelected
属性。然后,在template
中,如果item有属性isSelected
,则分配类.primary
。
注意到:此方法还从前面选定的项中移除isSelected
属性。这样只能同时突出显示一个<tr>
。
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>
发布于 2020-12-11 02:28:58
我知道这是一个有点老的问题,但是您可以使用vuetify的item-class
和@click:row
属性来处理行单击和设置自定义行类。
https://stackoverflow.com/questions/57490177
复制