我有一个表,其中一列存储输入
<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/