【问题标题】:Vuetify text field in data table item slotVuetify 数据表项插槽中的文本字段
【发布时间】:2020-02-21 19:42:15
【问题描述】:

我正在克隆一个数组并使用map 为数组中的每个对象添加一个属性。出于某种原因,当我这样做时,添加的属性在v-model 中使用时没有更新。任何想法为什么会发生这种情况?

在下面的代码中,在文本字段中键入不会更新项目数量。如何更改代码才能做到这一点?

<template>
  <v-data-table
    :headers="headers"
    :items="items"
  >
    <template v-slot:item.quantity="props">
      <v-text-field v-model="props.item.quantity"></v-text-field>
    </template>
  </v-data-table>
</template>

<script>
export default {
  props: {
    customer: {
      type: Object,
      required: true
    }
  },
  data: () => ({
    headers: [
      { text: 'ID', value: 'id' },
      { text: 'Description', value: 'description' },
      { text: 'Quantity', value: 'quantity' },
    ],
    items: []
  }),
  created() {
    this.initialize()
  },
  methods: {
    initialize() {
      const items = [...this.customer.items]
      items.map(item => {
        item.quantity = ''
      })
      this.items = items
    }
}
</script>

客户是来自 API 的 JSON 响应

{
  "id": 1,
  "name": "Customer",
  "items": [
    {
      "id": 1,
      "product": 1707,
      "contract": null,
      "plu": "709000",
      "description": "ABC",
      "unit": 1,
      "price": "5.20"
    }
  ],
}

更新

Link to codepen - 在qty2 字段中键入不会更新数据,因为它是使用map 添加到对象中的。

【问题讨论】:

  • 试试v-slot:item.quantity="{ item }" v-model="item.quantity"
  • 您能否分享this.customer.items的示例对象,有问题,这真的有助于解决您的问题
  • @azeós 也不起作用
  • @chans 问题已更新
  • 当您更改文本框中的值时,上述方法将更新对象

标签: javascript vue.js vuejs2 vuetify.js


【解决方案1】:

上面的代码按预期工作,我在codepen中重现了相同的场景

在这里找到工作代码:https://codepen.io/chansv/pen/gOOWJGJ?editors=1010

尝试更改数据表中的第一个文本框以进行测试,并检查控制台它在键入时更新项目对象

<div id="app">
  <v-app id="inspire">
    <v-data-table
      :headers="headers"
      :items="desserts"
      :items-per-page="5"
      class="elevation-1"
    >
      <template v-slot:item.protein="props">
        <v-text-field
          v-model="props.item.protein"
          name="quantity"
          outlined
          @input="getdata"
          type="number"
        ></v-text-field>
      </template>
    </v-data-table>
  </v-app>
</div>


new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    return {
      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: [
        {
          name: 'Frozen Yogurt',
          calories: 159,
          fat: 6.0,
          carbs: 24,
          protein: 4.0,
          iron: '1%',
        },
        {
          name: 'Ice cream sandwich',
          calories: 237,
          fat: 9.0,
          carbs: 37,
          protein: 4.3,
          iron: '1%',
        },
        {
          name: 'Eclair',
          calories: 262,
          fat: 16.0,
          carbs: 23,
          protein: 6.0,
          iron: '7%',
        },
        {
          name: 'Cupcake',
          calories: 305,
          fat: 3.7,
          carbs: 67,
          protein: 4.3,
          iron: '8%',
        },
        {
          name: 'Gingerbread',
          calories: 356,
          fat: 16.0,
          carbs: 49,
          protein: 3.9,
          iron: '16%',
        },
        {
          name: 'Jelly bean',
          calories: 375,
          fat: 0.0,
          carbs: 94,
          protein: 0.0,
          iron: '0%',
        },
        {
          name: 'Lollipop',
          calories: 392,
          fat: 0.2,
          carbs: 98,
          protein: 0,
          iron: '2%',
        },
        {
          name: 'Honeycomb',
          calories: 408,
          fat: 3.2,
          carbs: 87,
          protein: 6.5,
          iron: '45%',
        },
        {
          name: 'Donut',
          calories: 452,
          fat: 25.0,
          carbs: 51,
          protein: 4.9,
          iron: '22%',
        },
        {
          name: 'KitKat',
          calories: 518,
          fat: 26.0,
          carbs: 65,
          protein: 7,
          iron: '6%',
        },
      ],
    }
  },
  methods: {
    getdata() {
      console.log(this.desserts[0].protein);
    }
  },
})

【讨论】:

  • 如果这符合您的要求,请告诉我
  • 问题出在地图上。如果我使用地图向对象添加属性,则无法更新它
  • 我刚刚使用深拷贝解决了 - 感谢您的帮助
【解决方案2】:

解决办法是在使用map添加新的对象属性之前,需要对customer.items做一个深拷贝。

initialize() {
  let items = JSON.parse(JSON.stringify(this.customer.items))
  items.map(item => {
    item.quantity = '5'
    return item
  })
  this.items = items
}

【讨论】:

    猜你喜欢
    • 2020-08-27
    • 2020-04-12
    • 2022-01-16
    • 2021-05-18
    • 2020-12-22
    • 2022-11-15
    • 2020-01-21
    • 2020-11-14
    • 2020-10-01
    相关资源
    最近更新 更多