选择Vuetify数据表中的行

uyto3xhc  于 2023-01-09  发布在  Vue.js
关注(0)|答案(3)|浏览(201)

我想知道如何通过点击Vuetify数据表中的一行来选择/取消选择一行。如果我可以通过使用shift+点击或shift+箭头来选择一个范围,那就更好了,但这是另一个问题了。
我已经尝试更新发送到参数“value”中的数组,但是在用户界面中从未选中这些行。

<template>
  <v-data-table
    :headers="headers"
    :items="desserts"
    item-key="id"
    class="elevation-1"
    :value="selectedRows"
    @click:row="rowClicked"
  ></v-data-table>
</template>

<script>
export default {
  data() {
    return {
      selected: [],
      selectedRows: [],
      headers: [
        {
          text: "Dessert (100g serving)",
          align: "left",
          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: [
        {
          id: 1,
          name: "Frozen Yogurt",
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: "1%"
        },
        {
          id: 2,
          name: "Ice cream sandwich",
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          iron: "1%"
        },
        {
          id: 3,
          name: "Eclair",
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
          iron: "7%"
        },
        {
          id: 4,
          name: "Cupcake",
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: 4.3,
          iron: "8%"
        },
        {
          id: 5,
          name: "Gingerbread",
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: 3.9,
          iron: "16%"
        },
        {
          id: 6,
          name: "Jelly bean",
          calories: 375,
          fat: 0.0,
          carbs: 94,
          protein: 0.0,
          iron: "0%"
        },
        {
          id: 7,
          name: "Lollipop",
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: 0,
          iron: "2%"
        },
        {
          id: 8,
          name: "Honeycomb",
          calories: 408,
          fat: 3.2,
          carbs: 87,
          protein: 6.5,
          iron: "45%"
        },
        {
          id: 9,
          name: "Donut",
          calories: 452,
          fat: 25.0,
          carbs: 51,
          protein: 4.9,
          iron: "22%"
        },
        {
          id: 10,
          name: "KitKat",
          calories: 518,
          fat: 26.0,
          carbs: 65,
          protein: 7,
          iron: "6%"
        }
      ]
    };
  },
  methods: {
    rowClicked(row) {
      this.swapSelectionStatus(row.id);
      this.log(row);
    },
    swapSelectionStatus(keyID) {
      if (this.selectedRows.includes(keyID)) {
        this.selectedRows = this.selectedRows.filter(
          selectedKeyID => selectedKeyID !== keyID
        );
      } else {
        this.selectedRows.push(keyID);
      }
    },
    select(e) {
      this.log(e);
    },
    log(logItem) {
      /* eslint-disable no-console */
      console.log(logItem);
    }
  }
};
</script>
wvyml7n5

wvyml7n51#

我认为您应该使用v-model来代替...

<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
您还可以使用Vuetify Data Table select函数来切换所选行,这比使用自定义方法来处理选择逻辑要容易。
Demo 2

rekjcdws

rekjcdws2#

这样做,它就会工作得很好。

<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>

请注意两点:v-modelshow-select接下来,在脚本的数据部分添加selected数组

data(){
  return {
    selected:[{user_id: "FOR_INSTANCE"}]
  }
}
puruo6ea

puruo6ea3#

您可以更改它,并删除single-select属性,最后,您还需要通过在row.select(true)中使用row.isSelected而不是true来使行可切换

<div id="app">  
  <v-app id="inspire">
    <div>{{selectedId}}</div> 
    <v-data-table
      @click:row="rowClick" 
      item-key="name" 
      single-select
      :headers="headers"
      :items="desserts"
      :items-per-page="5"
    ></v-data-table>
  </v-app>
</div>

您可以在下面找到完整的解决方案,对于单选,请查看顶部的链接https://codepen.io/deadManN/pen/RwBKLpv
我也要感谢Nicolai Nikolai的回答,因为没有它,我就不知道有一个行参数,所以我不能调试它以查看它的额外属性

    • 注意:**如答案所示,如果您使用的是<style scoped>,则还需要在scoped CSS样式中使用/deep/::v-deep,以便能够覆盖Vuetify样式

相关问题