JavaScript - Simple Sorting Data Using AngularJS
Submitted by razormist on Friday, January 25, 2019 - 15:37.
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.
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!!
Getting started:
First you will need to download & install the AngularJS here's the link https://angularjs.org/. And this is the link for the bootstrap that has been used for the layout of the application https://getbootstrap.com/.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.- <!DOCTYPE html>
- <html lang="en" ng-app="myModule">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body ng-controller="myController">
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="form-inline">
- <select class="form-control" ng-model="sort">
- </select>
- </div>
- <table class="table table-bordered">
- <thead class="alert-info">
- <tr>
- </tr>
- </thead>
- <tbody style="background-color:#fff;">
- <tr ng-repeat="member in members | orderBy: sortKey">
- </tr>
- </tbody>
- </table>
- </div>
- </body>
- </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.- var app = angular.module("myModule", [])
- .controller("myController", function($scope){
- var members = [
- {firstname: "John", lastname: "Smith", age: "15", gender: "Male"},
- {firstname: "Brent", lastname: "Slay", age: "18", gender: "Male"},
- {firstname: "Mica", lastname: "Uy", age: "21", gender: "Female"},
- {firstname: "Jane", lastname: "Laurel", age: "17", gender: "Female"},
- {firstname: "Jack", lastname: "Curls", age: "22", gender: "Male"}
- ];
- $scope.members = members;
- var keyword = ["firstname", "lastname", "age", "gender"];
- $scope.keyword = keyword;
- $scope.sortBy = function(){
- $scope.sortKey = $scope.sort;
- }
- });
Add new comment
- 24 views