• We just launched and are currently in beta. Join us as we build and grow the community.

Sort HTML Table Using AngularJS

xKush

Doujinshi Creator
X Rep
0
0
0
Rep
0
X Vouches
0
0
0
Vouches
0
Posts
183
Likes
173
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create Sort HTML Table using AngularJS. The code will sort the table data when you click the header. This code will Feel free to use this code and modify as you learn from it. To learn more about this, just follow the steps below.

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 i used for the layout design 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.
  1. <!DOCTYPE html>
  2. <html

    lang

    =

    "en"

    ng-app=

    "myModule"

    >
  3. <head

    >
  4. <meta

    charsert=

    "UTF-8"

    name

    =

    "viewport"

    content

    =

    "width=device-width, initial-scale=1"

    /

    >
  5. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/bootstrap.css"

    /

    >
  6. <link

    rel

    =

    "stylesheet"

    type

    =

    "text/css"

    href

    =

    "css/style.css"

    >
  7. </

    head

    >
  8. <body

    ng-controller=

    "myController"

    >
  9. <nav class

    =

    "navbar navbar-default"

    >
  10. <div

    class

    =

    "container-fluid"

    >
  11. <a

    class

    =

    "navbar-brand"

    href

    =

    "https://sourcecodester.com"

    >

    Sourcecodester</

    a

    >
  12. </

    div

    >
  13. </

    nav>
  14. <div

    class

    =

    "col-md-3"

    ></

    div

    >
  15. <div

    class

    =

    "col-md-6 well"

    >
  16. <h3

    class

    =

    "text-primary"

    >

    Sort HTML Table Using AngularJS</

    h3

    >
  17. <hr

    style

    =

    "border-top:1px dotted #ccc;"

    /

    >
  18. <div

    class

    =

    "col-md-8"

    >
  19. <table

    class

    =

    "table table-bordered"

    >
  20. <thead

    class

    =

    "alert-info"

    >
  21. <tr

    >
  22. <th

    ng-click=

    "sortData('firstname')"

    >

    Firstname<div

    ng-class

    =

    "getSortClass('firstname')"

    ></

    div

    ></

    th

    >
  23. <th

    ng-click=

    "sortData('lastname')"

    >

    Lastname<div

    ng-class

    =

    "getSortClass('lastname')"

    ></

    div

    ></

    th

    >
  24. <th

    ng-click=

    "sortData('address')"

    >

    Address<div

    ng-class

    =

    "getSortClass('address')"

    ></

    div

    ></

    th

    >
  25. </

    tr

    >
  26. </

    thead

    >
  27. <tbody

    >
  28. <tr

    ng-repeat=

    "member in members | orderBy:sortColumn:reverseSort"

    >
  29. <td

    >

    {{member.firstname}}</

    td

    >
  30. <td

    >

    {{member.lastname}}</

    td

    >
  31. <td

    >

    {{member.address}}</

    td

    >
  32. </

    tr

    >
  33. </

    tbody

    >
  34. </

    table

    >
  35. </

    div

    >
  36. <div

    class

    =

    "col-md-4"

    >
  37. <form

    >
  38. <div

    class

    =

    "form-group"

    >
  39. <label

    >

    Firstname</

    label

    >
  40. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "newMember.firstname"

    /

    >
  41. </

    div

    >
  42. <div

    class

    =

    "form-group"

    >
  43. <label

    >

    Lastname</

    label

    >
  44. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "newMember.lastname"

    /

    >
  45. </

    div

    >
  46. <div

    class

    =

    "form-group"

    >
  47. <label

    >

    Address</

    label

    >
  48. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "newMember.address"

    /

    >
  49. </

    div

    >

  50. <center

    ><button

    type

    =

    "button"

    class

    =

    "btn btn-primary"

    ng-click =

    "saveMember()"

    ><span

    class

    =

    "glyphicon glyphicon-save"

    ></

    span

    >

    Save</

    button

    ></

    center

    >
  51. </

    form

    >
  52. </

    div

    >
  53. </

    div

    >
  54. <script

    src

    =

    "js/angular.js"

    ></

    script

    >
  55. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  56. </

    body

    >
  57. </

    html

    >

Creating the Main Function

This code contains the main function of the application. This code will sort the data when user click the table header. To that just kindly copy and write these block of codes inside the text editor, then save it inside the js folder as script.js
  1. var

    app =

    angular.module

    (

    "myModule"

    ,

    [

    ]

    )
  2. .controller

    (

    "myController"

    ,

    function

    (

    $scope)

    {
  3. $scope.members

    =

    [
  4. {

    firstname:

    "John"

    ,

    lastname:

    "Wick"

    ,

    address:

    "New York"

    }

    ,
  5. {

    firstname:

    "Steve"

    ,

    lastname:

    "Roger"

    ,

    address:

    "Japan"

    }

    ,
  6. ]

    ;

  7. $scope.newMember

    =

    {

    }

    ;

  8. $scope.saveMember

    =

    function

    (

    )

    {
  9. $scope.members

    .push

    (

    $scope.newMember

    )

    ;
  10. $scope.newMember

    =

    {

    }

    ;
  11. }

    ;

  12. $scope.reverseSort

    =

    false

    ;

  13. $scope.sortData

    =

    function

    (

    column)

    {
  14. $scope.reverseSort

    =

    (

    $scope.sortColumn

    ==

    column)

    ?

    !

    $scope.reverseSort

    :

    false

    ;
  15. $scope.sortColumn

    =

    column;

  16. }

  17. $scope.getSortClass

    =

    function

    (

    column)

    {
  18. if

    (

    $scope.sortColumn

    ==

    column)

    {
  19. return

    $scope.reverseSort

    ?

    'down-arrow'

    :

    'up-arrow'

    ;
  20. }
  21. return

    ''

    ;
  22. }
  23. }

    )

    ;

There you have it we successfully created a Sort HTML Table 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!!


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,348

323,357

Top