Connie DeCinko
Connie DeCinko

Reputation: 902

Scroll to first selected row in v-data-table

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.

Upvotes: 1

Views: 3823

Answers (1)

Sphinx
Sphinx

Reputation: 10729

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/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/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/[email protected]/dist/vue.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>

Upvotes: 1

Related Questions