1

I have a v-data-table that I highlight a selected row by adding a selectedRow class to the table row. After an update in another component the v-data-table is refreshed. I want it to automatically scroll back to the selected row. I'm getting an error that the element cannot be found. It seems to not be finding the CSS selector I am trying to pass as the target.

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

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

I've tried using next tick but same error occurs.

Connie DeCinko
  • 802
  • 2
  • 15
  • 32
  • You add the .selectedRow class when you select a row. So after refresh it would be lost and hence you get the `element cannot be found` error. You might want to use a unique id instead, for the row you have selected (that does not change after refresh) and go back to the row using the id instead. – nizantz Jul 24 '20 at 21:02
  • After refresh the .selectedRow class is there. I can scroll down and see the highlight and inspect and see the class is on the row. Is goTo trying too soon to scroll? – Connie DeCinko Jul 24 '20 at 21:06

1 Answers1

1

For current version=v2.3.6 now, The CSS class for selected row is v-data-table__selected, so changes that line to this.$nextTick(() => this.$vuetify.goTo('.v-data-table__selected')).

Below is one demo based on the snippet in official site:

PS: hit 'Goto' button at the end of the fiddle then you will see the effect.

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>
Sphinx
  • 10,519
  • 2
  • 27
  • 45
  • I'm not actually selecting the row, in the way you select normally. I am only adding a class to the row(s) that is used to highlight the row. I added a button to try and test that this isn't a timing issue. The datatable is shown, a row is highlighted. I click the button and the method fires but the card never scrolls. I even tried to target a div at the bottom of the card. Still no scroll. – Connie DeCinko Jul 28 '20 at 17:57
  • Please see the updated snippet, I tried the way adding the class manually, it seems work fine. if still doesn't meet your requirements, could you provide one fiddle so we can reproduce the issue you met? – Sphinx Jul 28 '20 at 18:17