【问题标题】:using v-model in a v-for directive to create a new data object在 v-for 指令中使用 v-model 来创建新的数据对象
【发布时间】:2020-05-05 19:28:33
【问题描述】:

我想通过以下方式使用 v-for 指令创建一个 items 对象:

<v-row v-for="(item, index) in items" v-bind:key="index">
            <v-col cols="12" md="4">
              <v-autocomplete
                v-model="item.product_uid"
                :items="product_list"
                item-text="[attributes][name]"
                item-value="id"
                label="Product name"
                outlined
              ></v-autocomplete>
            </v-col>
            <v-col cols="12" md="4">
              <v-text-field
                v-model="item.qty"
                label="Quantity"
                outlined
              ></v-text-field>
            </v-col>
            <v-col cols="12" md="4">
              <v-text-field
                v-model="item.net_unit_price"
                label="Unit price"
                outlined
              ></v-text-field>
            </v-col>
          </v-row>
          <v-col>
            <v-btn rounded color="primary" dark @click="addItem()">Add</v-btn>
          </v-col>

data () {
      return {
        items: [],
        product_list: [],
      }
    },
    methods: {
      addItem: function () {
        this.items.push({ product_uid: '', qty: '', net_unit_price: '',  });
      }
}

如何根据从 product_list 对象中选择的“产品名称”获取“单价”值 (product_list[attributes][price]),然后将其添加到新的 items 数组中?

product_list 对象来自服务器的 http 调用,我想将这些数据用作表单中的默认值,并且 - 如有必要 - 更改这些值并将它们全部添加到新的 item 对象中。

非常感谢您的帮助!

【问题讨论】:

  • 您正在尝试遍历空的项目。然后您尝试单击将项目推送到项目的“添加”。但是您不能单击“添加”,因为您的项目是空的。很可能您应该遍历 product_list 而不是 items。
  • 使用“添加”按钮,我插入了我用 product_list 对象中的值填充的空行。我的目标是,如果我从 product_list 中选择一个产品,则 Unit price 字段会使用来自同一 product_list 对象的匹配值进行更新。
  • 我理解那一刻,但你不能循环项目并使用“添加”添加/删除它,因为你将无法看到该列表,因为项目是空的,所以你的“添加”按钮无法呈现。您应该遍历 product_list。也许,我错过了什么。
  • 我被代码缩进弄糊涂了。我没有看到在“添加”按钮之前关闭 。现在循环遍历项目是有意义的。

标签: vue.js vuetify.js


【解决方案1】:

如果我对您的理解正确,您希望在从自动完成列表中选择商品时“自动完成”数量和单价。所以这里有一个样本。假设您的 api 数据如下所示:

product_list: [
  {product_uid: 1, name: 'name1', qty: 1, net_unit_price: 2},
  {product_uid: 2, name: 'name2', qty: 2, net_unit_price: 3},
  {product_uid: 3, name: 'name3', qty: 3, net_unit_price: 4}
],

然后从自动完成中删除v-model 和item-value,然后添加return-object,以便我们从自动完成中获取完整的对象以供使用。添加 vuetify 自动完成事件 change,以便我们可以传递选择的对象和索引,以便我们可以将值修补到您的数组中:

模板:

<v-autocomplete 
  :items="product_list"
  item-text="name" <!-- add correct prop -->
  label="Product name"
  outlined
  return-object <!-- add this and below -->
  @change="setValue(index, $event)"
></v-autocomplete>

所以现在当从自动完成中选择一个项目时,我们调用setValue,并为它修补正确的值:

setValue: function(index, item) {
  // if you want the complete object, else choose which props you want
  this.$set(this.items, index, { ... item })
}

CODEPEN

【讨论】:

  • 看起来不错,非常感谢!我还不能尝试,但我想知道如果 product_list 和 items 对象具有不同的结构,这个解决方案是否也有效?
  • 如果它们的结构不同...这就是为什么我添加了这两个 cmets:add correct prop 和 if you want the complete object, else choose which props you want 也涵盖了这种情况。我会假设 id 总是唯一的,并且两个列表都应该有具有相同 id 的对象,即使其他道具有不同的名称。但是尝试一下,如果您遇到特定的代码问题,请大声喊叫,我会尽力提供帮助 :)
  • 所以如果对象属性不匹配,你可以用this.$set(this.items, index, { ... item })代替...this.$set(this.items, index, { product_uid: item.myProductUidPropertyName, qty: item.myProductQtyPropertyName, net_unit_price: item.myUnitPricePropertyName })你明白了:)
  • 再次感谢您的帮助!我会试试看,希望我不能再打扰你;-)
【解决方案2】:

很可能,您应该使用computed 属性,如文档https://vuejs.org/v2/guide/computed.html 中所述。

您的product_list 也是空的。在获得product_list[attributes][price] 之前,您应该在其中添加一些内容或借助计算属性或其他内容获得过滤结果。另外,我不明白你想用那行代码做什么:items="product_list" 你试图为所有组件的items 属性设置相同的值。

很难理解你到底想做什么,但可能你还需要某种过滤方法https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter。

【讨论】:

  • 感谢您的回答!我已经编辑了我的帖子以使其更易于理解。
  • 我已经对您的问题添加了评论,但如果我在您更改后立即理解,我仍然可以回答关于使用计算属性的答案。
猜你喜欢
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2021-02-09
  • 1970-01-01
  • 2016-03-11
  • 2019-12-16
  • 1970-01-01
相关资源
最近更新 更多