JavaScript - Move Data To Other Table Using AngularJS

In this tutorial we will create a Move Data To Other Table Using AngularJS. This code will dynamically move the targeted data when the user click the move button. The code itself use a AngularJS directives that can move a data by setting the array index position as a parameter in the moveMember(), then it will automatically push the data into a new object and instantly remove it afterwards. This a user-friendly program feel free to modify and use it to your system. We will be using AngularJS as a comprehensive JavaScript framework that extend the browser capabilities. It handles the heavy lifting of DOM manipulation and AJAX request in a whole.

Getting Started:

First you have to download Bootstrap, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/. And, this is the link for the AngularJS https://angularjs.org/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it index.html.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="myModule">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body ng-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="containet-fluid">
  10. <a class="navbar-brand" href="https://www.sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">JavaScript - Move Data To Other Table Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-6">
  18. <table class="table table-bordered">
  19. <thead class="alert-info">
  20. <tr>
  21. <th>Full Name</th>
  22. <th>Address</th>
  23. <th>Action</th>
  24. </tr>
  25. </thead>
  26. <tr ng-repeat="member in members">
  27. <td>{{member.name}}</td>
  28. <td>{{member.address}}</td>
  29. <td><button type="button" class="btn btn-sm btn-success" ng-click="moveMember(member)">Move</button></td>
  30. </tr>
  31. </tbody>
  32. </table>
  33. </div>
  34. <div class="col-md-6">
  35. <table class="table table-bordered">
  36. <thead class="alert-info">
  37. <tr>
  38. <th>Full Name</th>
  39. <th>Address</th>
  40. </tr>
  41. </thead>
  42. <tr ng-repeat="member in members2">
  43. <td>{{member.name}}</td>
  44. <td>{{member.address}}</td>
  45. </tr>
  46. </tbody>
  47. </table>
  48. </div>
  49. </div>
  50. </body>
  51. <script src="js/angular.js"></script>
  52. <script src="js/script.js"></script>
  53. <script src="js/jquery-3.2.1.min.js"></script>
  54. <script src="js/bootstrap.js"></script>
  55. </html>

Creating the Main Function

This code contains the main function of the application. This code move a data when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js.
  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3.  
  4.  
  5. $scope.members = [
  6. {name: "John Smith", address: "New York"},
  7. {name: "Claire Temple", address: "Racoon City"},
  8. {name: "Jack Froze", address: "Iceland"},
  9. {name: "George Flame", address: "Fireland"}
  10. ];
  11.  
  12. $scope.members2 = [];
  13.  
  14. $scope.moveMember = function(member){
  15. $scope.members2.push(member);
  16. $scope.members.splice($scope.members.indexOf(member), 1);
  17.  
  18. };
  19. });
There you have it we successfully created a Move Data To Other Table using AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Add new comment