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

时间:2019-08-14 07:39:50

标签: 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等任何开发环境上。

非常感谢!

3 个答案:

答案 0 :(得分:4)

对我来说,它可以通过替换表主体(echo "Please provide the server list: " read list if [ -z "$list" ] then echo "Please provide first the list!" exit else for x in $(cat $list) do os_ver = $(ssh $x cat /etc/redhat-release | awk '{print $7}') if [ $os_ver -eq 6.7 ] then ssh $x '/bin/bash -s' <<-'EOF' mkdir -p /var/adm/crash/ID wget -P /var/adm/crash/ID http://192.x.x.x/redhat/id/Agent-el6-*.rpm rpm -ivh /var/adm/crash/ID/Agent-el6-*.rpm EOF fi done fi )并手动定义v-slot:bodytr与Vuetify 2.X一起使用。它有助于研究Slot Example

这样,可以添加click事件并像这样设置css类:

td

(旁注:这样,您将无法再在<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

我们在单击该行时设置的CSS类。因此背景更改和文本变得粗体

data () {
  return {
    selectedItem: null,
    ...
  }
},
methods: {
    selectItem (item) {
      console.log('Item selected: ' + item.name)
      this.selectedItem = item
    }
}

答案 1 :(得分:1)

我添加了方法selectRow来接收一个项目,并为其添加isSelected属性。然后在template中,如果项目具有属性.primary,则分配类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%'
        }
      ]
    }
  },
})
.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>

答案 2 :(得分:0)

我知道这是一个有点老的问题,但是您可以使用vuetify的item-class@click:row属性来处理行单击并设置自定义行类。