Var*_*ron 8 javascript css vue.js vuetify.js
我使用 av-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>
Run Code Online (Sandbox Code Playgroud)
所以扩展代码是:
<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>
Run Code Online (Sandbox Code Playgroud)
但是,由于我使用 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)tr并td手动定义和 来与 Vuetify 2.X 一起使用。它有助于研究插槽示例。
这样就可以添加点击事件并像这样设置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>
Run Code Online (Sandbox Code Playgroud)
(附注:这样你就不能使用该事件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
}
}
Run Code Online (Sandbox Code Playgroud)
我们在单击行时设置的 CSS 类。所以背景变化和文本变得粗体:
<style scoped>
.selectedRow {
background-color: red;
font-weight: bold;
}
</style>
Run Code Online (Sandbox Code Playgroud)
我添加了selectRow接收项目并向isSelected其添加属性的方法。然后在template我分配类中.primary,如果 item 有 property isSelected。
注意:此方法还会删除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%'
}
]
}
},
})Run Code Online (Sandbox Code Playgroud)
.primary,
.primary:hover {
/** avoid using !important, added just for example**/
background-color: red !important;
}
.as-console-wrapper {
display: none !important;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13476 次 |
| 最近记录: |