I have been working on creating a form that includes a feature to dynamically append new rows. While I am able to successfully create new rows dynamically, I am facing an issue with displaying the initial values in my textboxes.
Below is a snippet of my code:
<div class="row" ng-app="product_list">
<div class="table-responsive" ng-controller="productList">
<table class="table table-bordered table-hovered">
<thead>
<tr>
<td class="text-left">Product Name</td>
<td class="text-left">Quantity</td>
<td class="text-left">Price</td>
<td class="text-left">Subtotal</td>
<td class="text-center"><button type="button" class="btn btn-default" ng-click="addNewRow()">Add Row</button></td>
</tr>
</thead>
<tbody>
<tr ng-repeat="value in product.item">
<td class="text-left">
<input type="text" ng-model="item.name" class="form-control" />
</td>
<td class="text-center">
<input type="number" ng-model="item.quantity" class="form-control" />
</td>
<td class="text-right">
<input type="number" ng-model="item.price" class="form-control text-right" value="{{ value.price }}" />
</td>
<td>{{ item.price * item.quantity | currency }}</td>
<td class="text-center">
<button type="button" class="btn btn-danger" ng-click="removeRow($index)">Remove</button>
</td>
</td>
</tbody>
<tfoot>
<tr>
<td colspan="3">
<td colspan="3" class="text-left">
<label>Subtotal: </label>
<input type="text" class="form-control text-right" value="{{ total() | currency }}" readonly />
</td>
</tr>
</tfoot>
</table>
</div>
</div>
The JavaScript Code:
<script type="text/javascript">
var appList = angular.module('product_list', []);
appList.controller('productList', function($scope){
$scope.product = {
item: [
{
product_name: 'Test name 1',
quantity: 5,
price: 99.9,
}
]
};
$scope.addNewRow = function() {
$scope.product.item.push({
product_name: '',
quantity: 1,
price: 0
});
}
$scope.removeRow = function(index) {
$scope.product.item.splice(index, 1);
}
$scope.total = function() {
var total = 0.00;
angular.forEach($scope.product.item, function(item){
total += item.quantity * item.price
});
return total;
}
});
</script>
Here's the plnkr link for reference: http://plnkr.co/edit/6vAVPw?p=preview