javascript - Bootstrap Vue 可编辑表格列

标签 javascript vue.js vuex

我有一个表,其中一列存储输入

  <b-table bordered stripped
                         show-empty
                         empty-text="Your cart is empty"
                         class="p-2"
                         :fields="fields"
                         :items="lines">
                    <template slot="quantity" slot-scope="line">
                        <input type="number" class="form-control-sm"
                               style="width:5em"
                               v-model="qvalue"
                               v-on:input="handleQuantityChange($event,line.item)"/>
                    </template>
                    <template slot="product" slot-scope="line">
                        {{line.item.product.name}}
                    </template>
                    <template slot="price" slot-scope="line">
                        {{ line.item.product.price| currency }}
                    </template>
                    <template slot="subtotal" slot-scope="line">
                        {{ (line.item.quantity*line.item.product.price) | currency }}
                    </template>
                    <template slot="remove" slot-scope="line">
                        <b-button size="sm" variant="danger" v-on:click="handleRemove(line)">
                            Remove
                        </b-button>
                    </template>
                </b-table>

问题出在第一列和 qvalue 绑定(bind)上,当我添加多个值时:我在两行中有相同的值。我怎么能有两个不同的值?方法如下:

 methods:{
            ...mapMutations({
                change:"cart/changeQuantity",
                remove: "cart/removeProduct"
            }),
            handleQuantityChange(e,line){
                if (e.target.value >0){
                    this.qvalue = e.target.value;
                } else {
                    this.qvalue = 1;
                    e.target.value = this.qvalue
                }
                this.change({line,quantity:e.target.value})
            },


            handleRemove(line){
                this.remove(line.item);
            }

        }

我明白 v-modelling qvalue 不好,但什么是正确的方法?

最佳答案

您可以使用 :value="line.item.quantity" 而不是 v-model

所以你的输入应该是这样的:

<input type="number" class="form-control-sm"
                           style="width:5em"
                           :value="line.item.quantity"
                           v-on:input="handleQuantityChange($event,line.item)"/>

Here是关于使用 vuex 处理表单的更多信息。

关于javascript - Bootstrap Vue 可编辑表格列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52546850/

相关文章:

javascript - 根据变量改变 React 中组件的顺序?

vue.js - 如何在 vue js 上的循环(超过 1 个循环)完成后调用方法?

javascript - 如何传递和获取模态值

javascript - 在不同的函数上访问 JQuery/AJAX 的返回值

vue.js - 向 Vuetify 标签添加自定义属性

javascript - Shopify 的 vue.js 应用程序中的变量保持为空

javascript - Vue.js 中的循环变量与 AngularJS 类似

javascript - [Vue 警告] : Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders

vue.js - 如何在 Ag Grid 中隐藏列?

javascript - Ext JS - 版本变量增量