0

我一直在使用https://vuetifyjs.com/en/components/data-tables/#crud-actions中的 Vuetify CRUD 示例。

我想为每条记录添加一个唯一的 ID。我已经拿了他们的原始代码笔并将其复制到这里导入 uuid 库。下面是一个 js 示例。 https://codepen.io/joomkit/pen/MWExOGK?editors=1011

import * as uuid from "https://cdn.skypack.dev/uuid@8.3.2";
console.log(uuid.v4())
new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: () => ({
    dialog: false,
    dialogDelete: false,
    headers: [
      {id: 'ID', value: 'id'},
      {
        text: 'Dessert (100g serving)',
        sortable: false,
        value: 'name',
      },
      { text: 'Calories', value: 'calories' },
      { text: 'Fat (g)', value: 'fat' },
      { text: 'Carbs (g)', value: 'carbs' },
      { text: 'Protein (g)', value: 'protein' },
      { text: 'Actions', value: 'actions', sortable: false },
    ],
    desserts: [],
    editedIndex: -1,
    editedItem: {
      id: uuid.v4(),      
      name: '',
      calories: 0,
      fat: 0,
      carbs: 0,
      protein: 0,
    },
    defaultItem: {
      id: uuid.v4(),      
      name: '',
      calories: 0,
      fat: 0,
      carbs: 0,
      protein: 0,
    },
  }),

  computed: {
    formTitle () {
      return this.editedIndex === -1 ? 'New Item' : 'Edit Item'
    },
  },

  watch: {
    dialog (val) {
      val || this.close()
    },
    dialogDelete (val) {
      val || this.closeDelete()
    },
  },

  created () {
    this.initialize()
  },

  methods: {
    initialize () {
      this.desserts = [
        {
          id: '1',
          name: 'SFrozen Yogurt',
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
        },
        {
          id: '2',
          name: 'Ice cream sandwich',
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
        },
        {
          id: '3',          
          name: 'Eclair',
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
        },
      ]
    },

    editItem (item) {
      this.editedIndex = this.desserts.indexOf(item)
      this.editedItem = Object.assign({}, item)
      this.dialog = true
    },

    deleteItem (item) {
      this.editedIndex = this.desserts.indexOf(item)
      this.editedItem = Object.assign({}, item)
      this.dialogDelete = true
    },

    deleteItemConfirm () {
      this.desserts.splice(this.editedIndex, 1)
      this.closeDelete()
    },

    close () {
      this.dialog = false
      this.$nextTick(() => {
        this.editedItem = Object.assign({}, this.defaultItem)
        this.editedIndex = -1
      })
    },

    closeDelete () {
      this.dialogDelete = false
      this.$nextTick(() => {
        this.editedItem = Object.assign({}, this.defaultItem)
        this.editedIndex = -1
      })
    },

    save () {
      if (this.editedIndex > -1) {
        Object.assign(this.desserts[this.editedIndex], this.editedItem)
      } else {
        this.desserts.push(this.editedItem)
      }
      this.close()
    },
  },
})

当我在模式对话框中添加新项目时,会为添加的行创建一个新的 uuid。但是,如果您添加超过 2 个新项目,则 uuid 会重复。

这里的内置数据表索引有问题吗?

4

1 回答 1

1

启动页面时,uuid 被分配给“editedItem”和“defaultItem”并且不再调用。

    save () {
      if (this.editedIndex > -1) {
        Object.assign(this.desserts[this.editedIndex], this.editedItem)
      } else {
        this.desserts.push(this.editedItem)
        this.defaultItem.id = uuid.v4();
      }
      this.close()
    },

如上修改,下一项的uuid会在保存新项时重新设置。


代码还有另一个问题。

当第一次调用“NEW ITEM”关闭弹出窗口而不保存时,close()函数中“editedItem”的uuid被覆盖为“defaultItem”的uuid。

将“defaultItem”和“editedItem”的 uuid 设置为相同的值。

于 2022-01-17T11:40:00.367 回答