【问题标题】:V-model is not binding as expectedV-model 没有按预期绑定
【发布时间】:2016-12-07 15:24:10
【问题描述】:

我有一张表,它遍历产品列表。对于表中的每个产品,我都有一个输入,用户可以在其中选择他想要该行中产品的数量。然后我在表格中有另一个单元格,该单元格是根据用户数量乘以单价计算得出的。这是我的桌子。

         <table class="table table-striped table-borderd">
                <template v-for="(c, catIndex) in products">
                    <tr>
                        <th>{{c.category}}</th>
                        <th>{{c.bulkSize}}</th>
                        <th>Price</th>
                        <th>Quantity</th>
                        <th>Total</th>
                    </tr>
                    <tr v-for="(p, productIndex) in c.productList">
                        <td>{{p.description}}</td>
                        <td>{{p.productSize}}</td>
                        <td>{{p.productPrice}}</td>
                        <td>
                            <input v-on:keyup='calculateTotal(catIndex, productIndex)' type="text" v-model="p.quantity" placeholder="Quantity">
                        </td>
                        <td>
                            <input 
                            type="text"
                            readonly 
                            v-model="p.total">
                        </td>
                    </tr>
                </template>
            </table>

然后在我的 JavaScript 中计算 keyup 的总数。这是代码。

calculateTotal: function(categoryIndex, productIndex) {
        let currentCategory = this.products[categoryIndex];
        let currentProduct = currentCategory.productList[productIndex];
        currentProduct.total = currentProduct.quantity * currentProduct.productPrice;
        console.log(this.products[categoryIndex].productList[productIndex].total);
    }

console.log() 向我显示了正确的值,但绑定到 total 的单元格永远不会更新。

我应该指出,数量键和总键都是通过 v-model 添加到产品对象中的。

我哪里出错了?

编辑: 根据下面的答案,我添加了这段代码来尝试解决这个问题,但它没有用。

created: function() {
    $.get('some endpoint', data => {
        this.customers = data.customers;
        this.locations = data.locations;
        this.products = data.products;
        this.products.forEach(cat => {
            cat.productList.forEach(product => {
                product.total = undefined;
            })
        })
    });
},

【问题讨论】:

  • 请创建 JSFiddle 或类似的。我们会提供帮助
  • 好的,现在开始工作。
  • 尝试:data.products.forEach(cat => { cat.productList.forEach(product => { product.total = undefined; }) this.products = data.products; })

标签: javascript vue.js


【解决方案1】:

您可以通过将total 属性添加到您的产品来修复它。它可能是0 甚至是undefined,但是,它必须在那里。这是设计使然。 github上的相关问题:https://github.com/vuejs/vue/issues/1797https://github.com/vuejs/vue/issues/3732

【讨论】:

  • 有趣的是你说它必须先声明。我的 qty 密钥没有先声明,但它可以工作。
  • @usedToBeFat 好的,我对它进行了更多试验,发现未初始化属性的绑定只有一种方式起作用 - 当您输入值时。这就是为什么它适用于qty 并且它适用于total 的方式相同。但是,当属性在开始时不存在时,输出绑定中断。您可以尝试在文本中显示{{p.qty}}(例如在输入旁边),即使输入的值传递给模型,您也不会看到任何内容。
  • 这很有意义,并且与我修复它的方式完全一致。
【解决方案2】:

固定:http://jsbin.com/nilutegexo/1/edit?html,js,console,output

这是因为您完全动态地创建列表,并且导致反应性问题。你基本上必须启动total: 0,它工作得很好。

【讨论】:

  • 好的,所以它确实在 jsbin 中工作,但它在我的应用程序中不起作用。你能在我上面的代码中发现一些可能有问题的地方吗?
  • this.products.push({id: 1, name: 'foo', total: 0});
  • 对,我在真正的应用程序模式下添加了它,但没有工作。我将用那段代码编辑我的问题。
猜你喜欢
  • 2017-08-20
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
  • 2015-06-12
  • 1970-01-01
  • 1970-01-01
  • 2017-10-01
  • 2018-07-29
相关资源
最近更新 更多