Select rows in Vuetify Data Table

后端 未结 2 1108
小蘑菇
小蘑菇 2021-01-24 22:49

I was wondering how can I select/unselect a row by clicking a row in a datatable in Vuetify. It would be even better if I could select a range by using shift+click or shift+arr

相关标签:
2条回答
  • 2021-01-24 23:29

    I think you want to use v-model instead...

    <v-data-table
        :headers="headers"
        :items="desserts"
        item-key="id"
        v-model="selectedRows"
        class="elevation-1">
          <template v-slot:item="{ item }">
            <tr :class="selectedRows.indexOf(item.id)>-1?'cyan':''" @click="rowClicked(item)">
                <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>
        </template>
    </v-data-table>
    

    Demo


    You can also the Vuetify Data Table select function to toggle the selected rows. This is easier than having custom methods to handle the selection logic.

    Demo 2

    0 讨论(0)
  • 2021-01-24 23:37

    Do this and it will work fine.

    <v-data-table
     :headers="getHeaders"
     :items="fetchDisplayData"
     :search="search"
     flat
     class="elevation-0"
     justify="space-around"
     item-key="user_id"
     v-model="selected"
     show-select
     >
    </v-data-table>
    

    Note two things the v-model and show-select Next in the data section of your script add the selected array

    data(){
      return {
        selected:[{user_id: "FOR_INSTANCE"}]
      }
    }
    
    0 讨论(0)
提交回复
热议问题