Hi,
How can i just select the dropdown and display it in the table without saving into database in angularjs?
The link below is the image as i expected.
After I click ADD button, the selected dropdown will display in the table but not save it into database.
here is my code
View
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $filter) {
$scope.Materials = [];
$scope.material = "0";
$scope.quantity = "0";
$scope.Add = function () {
var material = {};
if ($scope.material != "0") {
material.Material = $scope.material;
material.Quantity = $scope.quantity != undefined ? $scope.quantity : 0;
var materialExist = $filter('filter')($scope.Materials, { Material: $scope.material }, true);
if (materialExist.length > 0) {
for (var i = 0; i < $scope.Materials.length; i++) {
if ($scope.Materials[i].Material == $scope.material) {
if ($scope.quantity != undefined) {
$scope.Materials[i].Quantity = parseInt($scope.Materials[i].Quantity) + parseInt($scope.quantity);
}
break;
}
}
} else {
$scope.Materials.push(material);
}
}
};
});
</script>
<div ng-app="MyApp" ng-controller="MyController">
<table>
<tr>
<td>Material</td>
<td>
<select ng-model="material">
<option value="0">Select</option>
@foreach (var item in ViewBag.material)
{
<option value='@item.ID'>@item.Material</option>
}
</select>
</td>
<td>Quantity</td>
<td>
<select ng-model="quantity">
@{
for (int i = 1; i <= 100; i++)
{
<option value="i">@i</option>
}
}
</select>
</td>
<td> <span><input type="submit" value="Add Material" class="btn btn-primary" ng-click="Add()" /></span></td>
</tr>
</table>
<table cellpadding="1" cellspacing="1">
<tr>
<th>Material</th>
<th>Quantity</th>
<th></th>
</tr>
<tbody ng-repeat="m in Materials">
<tr>
<td>{{m.Material}}</td>
<td>{{m.Quantity}}</td>
<td><a href="#">Update</a> | <a href="#">Delete</a></td>
</tr>
</tbody>
</table>
</div>
Controller for ViewBag.material
public ActionResult FormRequest()
{
var material = db.MaterialLists.ToList();
ViewBag.material = material;
return View();
}