1

I would like to programmatically checkmark a row in a v-data-table when an external listener notifies me of a particular value.

As an example, here is a Vuetify selectable table: https://vuetifyjs.com/en/components/data-tables#selectable-rows

In the example, If I were passed the value of 'Gingerbread' after the table and its data have already been instantiated, how would I programmatically select that corresponding row?

2 Answers 2

5

You can do this by pushing your values in your model like this:

HTML:

<div id="app">
  <v-app id="inspire">
    <v-btn @click="select">button</v-btn>
    <v-data-table
      v-model="selected"
      :headers="headers"
      :items="desserts"
      :single-select="singleSelect"
      item-key="name"
      show-select
      class="elevation-1"
    >
      <template v-slot:top>
        <v-switch v-model="singleSelect" label="Single select" class="pa-3"></v-switch>
      </template>
    </v-data-table>
  </v-app>
</div>

VueJS:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  methods: {
    select: function() {
      let result = this.desserts.find((dessert) => {
        return dessert.name == 'Gingerbread'
      })

      this.selected.push(result)
    }
  },
  data () {
    return {
      singleSelect: false,
      selected: [],
      headers: [
        { text: 'Dessert (100g serving)', value: 'name' },  
        { text: 'Calories', value: 'calories' },
      ],
      desserts: [
        { name: 'Gingerbread', calories: 356 },
        { name: 'Jelly bean', calories: 375 }
      ],
    }
  },
})
Sign up to request clarification or add additional context in comments.

1 Comment

0

The v-data-table component has a property called filteredItems which you can use to add items to the selected array

<v-data-table v-model="selected":items="itemsArray" ref="table"></v-data-table>

function selectAll() {
    this.$refs.table.filteredItems.map(item => {
        if(...some condition...) {
            this.selected.push(item)
        }
    })
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.