【发布时间】:2017-10-17 11:04:15
【问题描述】:
我正在计算估计成本。选择产品获取有关产品的详细信息并在输入框中显示其描述和价格。然后单击添加按钮后,将出现一个新行以供另一个选择。但问题是新行与旧行数据一起出现。更改单行会影响所有其他行。到目前为止,这是我的代码:
<tbody v-for="row in rows" :key="index">
<tr>
<td>
<select @change="selected" v-model="product_id" class="form-control" name="product_id" id="product_id">
<option value="">Select Product</option>
<option v-for="item in items" :value="item.id" v-text="item.product_name"></option>
</select>
</td>
<td>
<textarea type="text" v-model="product.product_details" name="product_details" id="product_details" class="form-control" rows="1" placeholder="Product Details"></textarea>
</td>
<td>
<input v-model.number="product.product_sellPrice" type="number" step="any" class="form-control" name="rate" id="rate" placeholder="Rate">
</td>
<td>
<input v-model.number="product.product_tax" type="number" step="any" class="form-control" name="tax" id="tax" placeholder="Tax">
</td>
<td>
<input v-model="quantity" type="number" class="form-control" name="quantity" id="quantity" placeholder="Quantity">
</td>
<td>
<input :value="total" type="number" step="any" class="form-control" name="total" id="total" placeholder="Total Price">
</td>
<td>
<button class="btn btn-secondary" v-on:click="addrow" >
<i class="fa fa-plus" aria-hidden="true"></i>
</button>
<button v-show="length > 1" class="btn btn-secondary" @click.prevent="removerow(index)">
<i class="fa fa-minus" aria-hidden="true"></i>
</button>
</td>
</tr>
</tbody>
Vue部分
<script>
export default{
props: ['products'],
data() {
return {
rows: [{
}],
items: this.products,
product: '',
product_id: '',
quantity: '',
index: '',
total_price: '',
}
},
methods: {
addrow: function (event) {
event.preventDefault();
this.rows.push({
});
},
removerow: function (index) {
this.rows.splice(index, 1);
},
selected(e){
var id = this.product_id;
console.log(id);
axios.get('/estimate/product/' + id)
.then((response)=>{
this.product = '';
this.product = response.data;
})
.catch((error) => {
console.log(error);
});
},
}
}
</script>
我哪里做错了?
【问题讨论】:
-
这是我项目的一部分。但我认为如果不从项目中的数据库中获取数据,它就没有其他连接。这是我这部分的完整代码,我认为我在选择行时做错了
-
获取运行在 jsfiddle.net 中的代码并在此处发布,它会更容易帮助您。如何创建最小、完整和可验证的示例:stackoverflow.com/help/mcve
-
它在 JSFiddle 中不起作用。但我把代码缩短了。
-
通过创建一个最小的示例可以帮助故障排除,代码不必反映您的实际服务。我确实认为一个问题是,当您添加一个完全为空的新行时,添加已定义但为空的参数的新行,如下所示: this.rows.push({'Param1':''});