Dynamically Add And Remove Row In AngularJS With ASP.NET MVC

Introduction

 
Before AngularJS, the same thing was performed using jQuery which was very lengthy and time-consuming. The same functionality is performed using Angular now and the process is very quick and much faster than jQuery.
 
Recommended Prerequisites
  • Visual Studio 2017
  • ASP.NET MVC
Open Visual Studio and select "File" >> "New". Then, click on Project. (Remember, don't go with the option 'File->New->Website').
 
Dynamic Add And Remove Row In Angular With ASP.NET MVC
 
Select "Templates" >> Visual C# >> Web then ASP.NET Web Application (.NET Framework), and put appropriate project name.
  • And click the "OK" button. 
Dynamic Add And Remove Row In Angular With ASP.NET MVC
 
Now, choose the project template as MVC.
 
Dynamic Add And Remove Row In Angular With ASP.NET MVC
 
Now, your project will begin to start once you click the OK button.
 
Great! Now first, we will see the project structure in which we will work. So, our solution explorer will look something like this.
 
Dynamic Add And Remove Row In Angular With ASP.NET MVC
 
Now, let's code the amazing design.
 
First, we will begin with HTML file, i.e., Go to View -> Home -> Index.cshtml.
  1. @{ ViewBag.Title = "Home Page"; Layout = null; } <!DOCTYPE html>     
  2. <html>    
  3.    <head>    
  4.       <title>AngularJS</title>    
  5.       <meta charset='utf-8'>    
  6.       <meta name="description" content="AngularJS and Angular Code Example">    
  7.       <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">    
  8.       <link href="~/Content/css/style.css" rel="stylesheet" />    
  9.       <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.min.js"></script>     
  10.       <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">    
  11.       <script src="~/Scripts/app/main.js"></script>     
  12.    </head>    
  13.    <body ng-app="demo" ng-controller="DemoCtrl">    
  14.       <div class="container" width="800px" id="invoice">    
  15.          <div class="row">    
  16.             <div class="col-xs-12 heading"> Dynamic Grid in Angular JS </div>    
  17.          </div>    
  18.          <div class="items-table">    
  19.             <div class="row header">    
  20.                <div class="col-xs-1"> </div>    
  21.                <div class="col-xs-5">Title 1</div>    
  22.                <div class="col-xs-2">Title 2</div>    
  23.                <div class="col-xs-2">Title 3</div>    
  24.             </div>    
  25.             <div class="row invoice-item" ng-repeat="item in invoice.items" ng-animate="'slide-down'">    
  26.                <div class="col-xs-1 remove-item-container"> <a href ng-click="removeItem(item)" class="btn btn-danger"><i class="fa fa-times"></i></a> </div>    
  27.                <div class="col-xs-5 input-container"> <input ng-model="item.description" placeholder="Description" /> </div>    
  28.                <div class="col-xs-2 input-container"> <input ng-model="item.qty" value="1" size="4" ng-required ng-validate="integer" placeholder="Quantity" /> </div>    
  29.                <div class="col-xs-2 input-container"> <input ng-model="item.cost" value="0.00" ng-required ng-validate="number" size="6" placeholder="Cost" /> </div>    
  30.             </div>    
  31.             <div class="row invoice-item">    
  32.                <div class="col-xs-12 add-item-container"> <a class="btn btn-primary" href ng-click="addItem()"><i class="fa fa-plus"></i></a> </div>    
  33.             </div>    
  34.          </div>    
  35.       </div>    
  36.    </body>    
  37. </html>    
Here, we have included the CDN for AngularJS, Bootstrap, and font-awesome.
 
Our Angular controller name is DemoCtrl and app module name is demo.
 
Now, let's code our Angular file and name it as main.js in the location path - Script -> app -> main.js.
  1. angular.module('demo', []).service('LocalStorage', [function () {  
  2.     var Service = {};  
  3.     var hasInvoice = function () {  
  4.         return !(localStorage['invoice'] == '' || localStorage['invoice'] == null);  
  5.     };  
  6.     Service.getInvoice = function () {  
  7.         if (hasInvoice()) {  
  8.             return JSON.parse(localStorage['invoice']);  
  9.         } else {  
  10.             return false;  
  11.         }  
  12.     };  
  13.     return Service;  
  14. }]).controller('DemoCtrl', ['$scope''$http''LocalStorage'function ($scope, $http, LocalStorage) {  
  15.     (function init() {  
  16.         ! function () {  
  17.             var invoice = LocalStorage.getInvoice();  
  18.             $scope.invoice = invoice ? invoice : 0;  
  19.         }();  
  20.     })() $scope.addItem = function () {  
  21.         $scope.invoice.items.push({  
  22.             qty: 0,  
  23.             cost: 0,  
  24.             description: ""  
  25.         });  
  26.     }  
  27.     $scope.removeItem = function (item) {  
  28.         $scope.invoice.items.splice($scope.invoice.items.indexOf(item), 1);  
  29.     };  
  30. }])  
Now finally, we will add some CSS to our View so that it will looks somehow better.
 
So now, move on to Content -> CSS -> style.css.
  1. .items-table .row {  
  2.     border-bottom1px solid #ddd;  
  3.     line-height3em;  
  4. }  
  5.   
  6. .items-table .row:last-child {  
  7.     border-bottomnone;  
  8.     line-height3em;  
  9. }  
  10.   
  11. .items-table .row:nth-child(even) {  
  12.     background#f9f9f9;  
  13. }  
  14.   
  15. .items-table input {  
  16.     line-height1.5em;  
  17. }  
  18.   
  19. input:focus {  
  20.     outline0;  
  21. }  
  22.   
  23. .heading {  
  24.     background-color#357EBD;  
  25.     color#FFF;  
  26.     margin-bottom1em;  
  27.     text-aligncenter;  
  28.     line-height2.5em;  
  29. }  
  30.   
  31. .right input {  
  32.     text-alignright;  
  33. }  
  34.   
  35. .input-container {  
  36.     padding3px 0;  
  37. }  
  38.   
  39. .header.row {  
  40.     font-weightbold;  
  41.     border-bottom1px solid #ddd;  
  42.     border-top1px solid #ddd;  
  43. }  
  44.   
  45. input,  
  46. textarea {  
  47.     border1px solid #FFF;  
  48. }  
  49.   
  50. .container input:hover,  
  51. .container textarea:hover,  
  52. .table-striped>tbody>tr:nth-child(2n+1)>td input:hover,  
  53. .container input:focus,  
  54. .container textarea:focus,  
  55. .table-striped>tbody>tr:nth-child(2n+1)>td input:focus {  
  56.     border1px solid #CCC;  
  57. }  
  58.   
  59. .table-striped>tbody>tr:nth-child(2n+1)>td input {  
  60.     background-color#F9F9F9;  
  61.     border1px solid #F9F9F9;  
  62. }  
  63.   
  64. body {  
  65.     padding20px;  
  66. }  
  67.   
  68. .infos input {  
  69.     width300px;  
  70. }  
  71.   
  72. .align-right input {  
  73.     text-alignright;  
  74.     width300px;  
  75. }  
  76.   
  77. div.container {  
  78.     width800px;  
  79. }  
That's it. Run the project. The output will look somewhat like this. 
 
 

Summary

 
You can leave your feedback/comment/questions to this article. Please let me know how you like and understand this article and how I could improve it. You can get the source code from here.


Similar Articles