Simple Sort Row Table Header Using AngularJS
Submitted by razormist on Wednesday, February 1, 2017 - 18:07.
In this tutorial, we will create a Simple Sort Row Table Header 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. So let see how its done.
Creating The Mark-up Form
This form will display the data of all value into the table. To create this first open any kind of text editor(notepad++, etc..), then copy/paste the code below and name it "index.html"
The code above will display the data within the angular directives. The function repeat will loop all the necessary data that avail within the array. The function orderBy will sort the data based on the argument.
Adding Some Style
This will add some design on the HTML table.
To make the design worked make sure you add this inside the head tag.
Creating A Script
This will make the angular directives worked with the HTML table. To do that copy/paste the code below then name it "script.js".
The code above will sort the data when the table header has been clicked. The $scope.sortData function will sort the data when it achieved the required statement. The $scope.getSortClass will call the CSS class to display the appropriate design beside the clickable header.
There you have it, we created a simple sort row table header using angularJS. I hope that this tutorial help you to your projects. For more updates and tutorials, just kindly visit this site. Enjoy Coding!!
- <html lang = "en">
- <head>
- <meta charsert = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1"/>
- <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
- </head>
- <body ng-app = "myModule">
- <nav class = "navbar navbar-default">
- <div class = "container-fluid">
- </div>
- </nav>
- <div class = "row">
- <div class = "col-md-6 well" ng-controller = "myController">
- <hr style = "border-top:1px dotted #000;"/>
- <table class = "table table-bordered">
- <thead>
- <tr>
- </tr>
- </thead>
- <tbody>
- <tr ng-repeat = "PLang in PLangs | orderBy:sortColumn:reverseSort">
- </tr>
- </tbody>
- </table>
- </div>
- </div>
- </body>
- </html>
- <style>
- th{
- cursor:pointer;
- }
- .up-arrow{
- width:0;
- height:0;
- border-left:5px solid transparent;
- border-right:5px solid transparent;
- border-bottom:10px solid #000;
- display:inline-block;
- }
- .down-arrow{
- width:0;
- height:0;
- border-left:5px solid transparent;
- border-right:5px solid transparent;
- border-top:10px solid #000;
- display:inline-block;
- }
- </style>
- var app = angular.module("myModule", [])
- .controller("myController", function($scope){
- var PLangs = [
- {name: "C", creator: "Dennis Ritchie"},
- {name: "C++", creator: "Bjarne Stroustrup"},
- {name: "C#", creator: "Anders Hejlsberg"},
- {name: "Java", creator: "James Gosling"},
- {name: "HTML", creator: "Tim Berners-Lee"},
- {name: "Javascript", creator: "Brendan Eich"},
- {name: "PHP", creator: "Rasmus Lerdorf"},
- {name: "Python", creator: "Guido van Rossum"},
- {name: "Ruby", creator: "Yukihiro Matsumoto"},
- {name: "Perl", creator: "Larry Wall"},
- {name: "Scala", creator: "Martin Odersky"},
- {name: "Bash", creator: "Brian Fox"},
- {name: "Clojure", creator: "Rich Hickey"}
- ];
- $scope.PLangs = PLangs;
- $scope.reverseSort = false;
- $scope.sortData = function(column){
- $scope.reverseSort = ($scope.sortColumn == column) ? !$scope.reverseSort : false;
- $scope.sortColumn = column;
- }
- $scope.getSortClass = function(column){
- if($scope.sortColumn == column){
- return $scope.reverseSort ? 'down-arrow' : 'up-arrow';
- }
- return '';
- }
- });
Add new comment
- 25 views