vuetify data table行上移下移

废话不多讲,先上代码。

<template>
  <v-container>
    <v-card>
      <v-card-text>
        <v-data-table :items="books" :headers="headers">
          <template v-slot:item.name="{ item }">
            <v-chip :color="item.color">{{ item.name }}</v-chip>
          </template>
          <template v-slot:item.actions="{ item }">
            <v-btn icon @click="move(item, -1)">
              <v-icon>mdi-arrow-up</v-icon>
            </v-btn>
            <v-btn icon @click="move(item, +1)">
              <v-icon>mdi-arrow-down</v-icon>
            </v-btn>
          </template>
        </v-data-table>
      </v-card-text>
    </v-card>
  </v-container>
</template>
<script>
export default {
  name: 'MoveUpAndDown',
  data() {
    return {
      books: [],
      headers: [
        { text: 'Name', value: 'name' },
        { text: 'Price', value: 'price' },
        { text: 'Actions', value: 'actions', width: '50%', align: 'end' },
      ],
    };
  },
  methods: {
    async getBooks() {
      await new Promise((resolve) => {
        setTimeout(() => {
          resolve();
        }, 0);
      });
      this.books = [
        { name: 'Book1', price: 100, color: 'primary' },
        { name: 'Book2', price: 120, color: 'secondary' },
        { name: 'Book3', price: 60, color: 'warn' },
        { name: 'Book4', price: 90, color: 'error' },
      ];
    },
    move(item, n) {
      let i = this.books.indexOf(item);
      console.log(
        'before: ',
        this.books.map((x) => {
          return x.name;
        })
      );
      let j = i + n;
      if (j >= 0 && j < this.books.length) {
        let tmp = this.books[j];
        this.$set(this.books, j, this.books[i]);
        this.$set(this.books, i, tmp);
      }
      console.log(
        'after: ',
        this.books.map((x) => x.name)
      );
    },
  },
  beforeMount() {
    this.getBooks();
  },
};
</script>

主要的问题就是Array变化后的数据不刷新问题,需要用vue.set()方法设置。设置数组使用vue.set(array, index, item)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值