Hi everyone, I’m new to Meteor so I try to learn it by building a restaurant management webapp. One feature in this app is to add a daily sales for each products. I want to be able to add multiple sales data at a time and here is what I’ve accomplished so far
Here is my code
<template name="salesAdd">
<div class="row clearfix">
table...
<tbody>
{{#each salesInputs}}
{{> salesItem}}
{{/each}}
</tbody>
</table>
</div>
</div>
</template>
<template name="salesItem">
<tr class="salesItem" salesId="{{salesId}}">
<td><input type="text" name="name" id="name" placeholder="Product Name.." ></td>
<td><input type="number" name="sales-price" id="sales-price"></td>
<td><input type="number" name="sales-quantity" id="sales-quantity"></td>
<td><input type="number" name="sales-total" id="sales-total"></td>
<td><a type="button" href="#" class="btn btn-success btn-large add-btn">+</a>
<a type="button" href="#" class="btn btn-danger btn-large delete-btn">-</a>
</td>
</tr>
</template>
Template.salesAdd.onCreated(function() {
Session.set('salesInputs', [{salesId: 1}]);
});
Template.salesAdd.events({
'click .add-btn': function() {
var salesInputs = Session.get('salesInputs');
var salesId = Math.floor(Math.random() * 1000);
salesInputs.push({salesId: salesId});
Session.set('salesInputs', salesInputs);
},
});
Template.salesItem.events({
'click .delete-btn': function() {
var salesId = Template.instance().$('.salesItem').attr('salesId');
var salesInput = Session.get('salesInputs');
salesInput = _.reject(salesInput, function(x) {
return x.salesId == salesId;
});
Session.set('salesInputs', salesInput);
},
});
Template.salesAdd.helpers({
salesInputs: function() {
return Session.get('salesInputs');
},
});
But I came across some problem. When I delete the input row, the row is gone but the value of the deleted row still carry on to other row. In the example below, I deleted 2nd row (name: 722). The row is gone but its value(Quantity 2 and Total 2) passed on to 3rd row (name:978) and so on when delete other row.
Ideally when I delete the row, the value and the row itself should be gone. Is there a better way to this?
Thank you so much
** The original code is from here