我正在计算估计成本。选择产品会获取有关产品的详细信息,并在输入框中显示其描述和价格。然后在点击添加按钮后,会出现一个新的行作为另一个选择。但问题在于新行与旧行数据一起出现。并且更改单个行会影响其他所有行。这是我的代码到目前为止:选择单行是选择Vue.js中的所有其他行
<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>
<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中的代码并在这里发布,它会更容易地帮助你..如何创建一个最小化,完整和可验证的例如:https://stackoverflow.com/help/mcve – StefanE
它不会在JSFiddle中工作。但我已经缩短了代码。 –