1

我有一个 v-data-table,我通过将 selectedRow 类添加到表行来突出显示选定的行。在另一个组件中更新后,v-data-table 会被刷新。我希望它自动滚动回选定的行。我收到一个错误,即找不到该元素。似乎没有找到我试图作为目标传递的 CSS 选择器。

  mounted: function () {
    eventBus.$on("refreshVendors", () => {
      this.getVendors();
      // add scrollTo first selected vendor
      this.$vuetify.goTo(".selectedRow");
    });

    this.clearSelectedVendors();
    this.getVendors();
  },

我尝试使用下一个刻度,但出现同样的错误。

4

1 回答 1

1

对于当前版本=v2.3.6现在,所选行CSS 类v-data-table__selected,因此将该行更改为.this.$nextTick(() => this.$vuetify.goTo('.v-data-table__selected'))

下面是一个基于官方网站片段的演示:

PS:点击小提琴末尾的“Goto”按钮,您将看到效果。

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  methods: {
    gotoSelectedRow: function () {
      this.desserts[1].selected = false
      this.desserts[0].selected = true
      this.$nextTick(() => this.$vuetify.goTo('.selectedRow'))
    }
  },
  data () {
    return {
      headers: [
        {
          text: 'Dessert (100g serving)',
          align: 'start',
          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%',
          selected: true
        },
        {
          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%',
        },
      ],
    }
  },
})
.selectedRow 
{
  color: red;
}
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/babel-polyfill/dist/polyfill.min.js"></script>

<div id="app">
  <v-app id="inspire">
    <v-data-table
      :headers="headers"
      :items="desserts"
      item-key="name"
      class="elevation-1"
    >
      <template v-slot:top>
        <v-switch v-model="singleSelect" label="Single select" class="pa-3"></v-switch>
      </template>
      <template v-slot:body="{ items }">
        <tbody>
          <tr v-for="item in items" :key="item.name" :class="{'selectedRow': item.selected}">
            <td>{{ item.name }}</td>
            <td>{{ item.calories }}</td>
            <td>{{ item.fat }}</td>
            <td>{{ item.carbs }}</td>
            <td>{{ item.protein }}</td>
            <td>{{ item.iron }}</td>
          </tr>
        </tbody>
      </template>
    </v-data-table>
    <button @click="gotoSelectedRow()">Goto</button>
  </v-app>
</div>
  <script src="https://cdn.jsdelivr.net/npm/vue@2.x/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/vuetify@2.x/dist/vuetify.js"></script>

于 2020-07-24T22:45:27.657 回答