简体   繁体   English

Vue.js - 动态生成 v-model 名称

[英]Vue.js - Dynamically generate v-model name

this is my first use of Vue.js so please bear with me.这是我第一次使用 Vue.js,所以请耐心等待。 I have a section in my app where users can dynamically add (up to 5) entries and also remove entries.我的应用程序中有一个部分,用户可以在其中动态添加(最多 5 个)条目并删除条目。 Each entry consists of four input tags that correspond to product id, description, quantity, and unit price.每个条目由四个输入标签组成,分别对应于产品 ID、描述、数量和单价。 There is also an "X" icon at the end so that users can choose whether or not to remove that entry row before saving it.最后还有一个“X”图标,以便用户可以在保存之前选择是否删除该条目行。 So visually, it would look something like this:所以在视觉上,它看起来像这样:

  • 1 Tomatoes 40 $2.50 X 1 个西红柿 40 美元 2.50 美元 X
  • 2 Pears 50 $1.39 X 2 梨 50 $1.39 X
  • 3 Celery 12 $1.60 X 3 芹菜 12 $1.60 X

I am unsure how to dynamically generate v-model names that correspond to each piece of data that I want to save.我不确定如何动态生成与我要保存的每条数据相对应的v-model名称。 In other words, I need four input tags and the X icon for each entry that a user wants to enter.换句话说,对于用户想要输入的每个条目,我需要四个输入标签和 X 图标。 Therefore, I'd want the Vue.js state to look something like:因此,我希望 Vue.js 状态看起来像:

    data: {
        numEntries: 2,
        entries: [
            {
                productId: "",
                description: "",
                qty: "",
                price: "",
            },
            {
                productId: "",
                description: "",
                qty: "",
                price: "",
            },
            // There will be 'n' of these objects depending on how many entries there are.
        ]
    }

And I would like the v-model to be something like "productId1" to refer to entries[0].productId and "productId2" to refer to entries[1].productId , etc. My code is shown below:我希望v-model类似于“productId1”来引用entries[0].productId和“productId2”来引用entries[1].productId等。我的代码如下所示:

HTML HTML

<div id="app">
    ...
    <div v-for="n in numEntries" class="inventory-section">
        <input type="text" class="id-input" placeholder="Product Id" v-model="productId" />
        <input type="text" class="description-input" placeholder="Description" v-model="description" />
        <input type="text" class="qty-input" placeholder="Qty" v-model="qty" />
        <input type="text" class="price-input" placeholder="Price" v-model="price" />
        <span class="x-sign" v-on:click="removeEntry">X</span>
    </div>
    ...
</div>

Vue JS VueJS

var app = new Vue({
    el: '#app',
    data: {
        numEntries: 1,
        entries: [
            {
                productId: "",
                description: "",
                qty: "",
                price: "",
            }
        ]
    },
    methods: {
        addEntry: function () {
            if (this.numEntries < 12)
                this.numEntries += 1;
        },
        removeEntry: function () {
            if (this.numEntries > 1)
                this.numEntries -= 1;
        }
    }
})

In addition, when clicking the X on a row, how do I determine which row to remove?另外,当点击一行的X时,如何确定要删除哪一行? Currently my removeEntry function is very bare bones.目前我的removeEntry函数非常简单。

Any help would be greatly appreciated.任何帮助将不胜感激。

Instead of using v-for="n in numEntries" use it as v-for="entry in entries".而不是使用 v-for="n in numEntries" 将其用作 v-for="entry in entry"。 in this way, "entry" will be your scoped object in that div.这样,“条目”将成为该 div 中的作用域对象。 and you can use v-model="entry.productId"你可以使用 v-model="entry.productId"

Vue loop code: Vue循环代码:

<div v-for="(itm,ind) in entries" class="inventory-section">
    <input type="text" class="id-input" placeholder="Product Id" v-model="itm.productId" />
    <input type="text" class="description-input" placeholder="Description" v-model="itm.description" />
    <input type="text" class="qty-input" placeholder="Qty" v-model="itm.qty" />
    <input type="text" class="price-input" placeholder="Price" v-model="itm.price" />
    <span class="x-sign" @click="removeEntry(ind)">X</span>
    </div>

And remove item from array并从数组中删除项目

removeEntry: function (i) {
this.entries.splice(i,1)
}

you can loop through entries using v-for="(entry, index) in entries" and you can use v-model="entry.productId" and so on您可以使用v-for="(entry, index) in entries"遍历entries v-for="(entry, index) in entries"并且可以使用v-model="entry.productId"等等

   <div id="app">
        ...
        <div v-for="(entry, index) in entries" class="inventory-section">
            <input type="text" class="id-input" placeholder="Product Id" v-model="entry.productId" />
            <input type="text" class="description-input" placeholder="Description" v-model="entry.description" />
            <input type="text" class="qty-input" placeholder="Qty" v-model="entry.qty" />
            <input type="text" class="price-input" placeholder="Price" v-model="entry.price" />
            <span class="x-sign" v-on:click="removeEntry(index)>X</span>
        </div>
        ...
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM