Skip to main content

JavaScript - Simple Sorting Data Using AngularJS


In this tutorial we will create a Simple Sorting Data Using AngularJS. AngularJS is a structural framework for dynamic web apps. It is a kind of template that extends HTML to a new level of coding techniques. It is mostly used by other well known site for creating a template.

Getting started:

First you will need to download & install the AngularJS here's the link

And this is the link for the bootstrap that has been used for the layout of the application

Creating the Main Interface

This code contains the interface of the application. This code will render application and display the form. To do that just kindly write these block of code inside the text editor and save this as 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="container-fluid">
  10. <a class="navbar-brand" href="">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 - Simple Sorting Data Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="form-inline">
  18. <label>Order By:</label>
  19. <select class="form-control" ng-model="sort">
  20. <option ng-repeat="x in keyword">{{x}}</option>
  21. </select>
  22. <button class="btn btn-primary" ng-click="sortBy()">Sort</button>
  23. </div>
  24. <br /><br />
  25. <table class="table table-bordered">
  26. <thead class="alert-info">
  27. <tr>
  28. <th>Firstname</th>
  29. <th>Lastname</th>
  30. <th>Age</th>
  31. <th>Gender</th>
  32. </tr>
  33. </thead>
  34. <tbody style="background-color:#fff;">
  35. <tr ng-repeat="member in members | orderBy: sortKey">
  36. <td>{{member.firstname}}</td>
  37. <td>{{member.lastname}}</td>
  38. <td>{{member.age}}</td>
  39. <td>{{member.gender}}</td>
  40. </tr>
  41. </tbody>
  42. </table>
  43. </div>
  44. <script src="js/angular.js"></script>
  45. <script src="js/script.js"></script>
  46. </body>
  47. </html>

Creating the Script

This code contains the main function of the application. This code will sorted the data base on the selected value. To that just kindly copy and write these block of codes inside the text editor, then save it inside the js directory as script.js.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3. var members = [
  4. {firstname: "John", lastname: "Smith", age: "15", gender: "Male"},
  5. {firstname: "Brent", lastname: "Slay", age: "18", gender: "Male"},
  6. {firstname: "Mica", lastname: "Uy", age: "21", gender: "Female"},
  7. {firstname: "Jane", lastname: "Laurel", age: "17", gender: "Female"},
  8. {firstname: "Jack", lastname: "Curls", age: "22", gender: "Male"}
  9. ];
  11. $scope.members = members;
  13. var keyword = ["firstname", "lastname", "age", "gender"];
  15. $scope.keyword = keyword;
  17. $scope.sortBy = function(){
  18. $scope.sortKey = $scope.sort;
  19. }
  20. });

There you have it we successfully created a Simple Sorting Data Using AngularJS. I hope that this very 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

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.