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

JavaScript - Search Filter And Sort Using AngularJS

Bootlegger

Waifu Strategist
B Rep
0
0
0
Rep
0
B Vouches
0
0
0
Vouches
0
Posts
37
Likes
60
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 400 XP
In this tutorial we will create a Search Filter And Sort Using AngularJS. This code can filter and sort an array of data within the html table. The code use angular directives to sort and filter array element by binding the textbox in ng-model then a adding a filter function in the ng-repeat with the same value as the model and also by providing an string argument to getSortClass() in the table th. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

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 it 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"

    >

    JavaScript - Search Filter And Sort Using AngularJS</

    h3

    >
  17. <hr

    style

    =

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

    /

    >
  18. <div

    class

    =

    "col-md-2"

    ></

    div

    >
  19. <div

    class

    =

    "col-md-8"

    >
  20. <div

    class

    =

    "form-inline"

    >
  21. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "search"

    placeholder=

    "Search here..."

    /

    >
  22. </

    div

    >
  23. <br

    /

    >
  24. <table

    class

    =

    "table table-bordered"

    >
  25. <thead

    class

    =

    "alert-info"

    >
  26. <tr

    >
  27. <th

    ng-click=

    "sortData('firstname')"

    >

    Firstname<div

    ng-class

    =

    "getSortClass('firstname')"

    ></

    div

    ></

    th

    >
  28. <th

    ng-click=

    "sortData('lastname')"

    >

    Lastname<div

    ng-class

    =

    "getSortClass('lastname')"

    ></

    div

    ></

    th

    >
  29. <th

    ng-click=

    "sortData('gender')"

    >

    Gender<div

    ng-class

    =

    "getSortClass('gender')"

    ></

    div

    ></

    th

    >
  30. </

    tr

    >
  31. </

    thead

    >
  32. <tbody

    >
  33. <tr

    ng-repeat=

    "member in members | orderBy:sortColumn:reverseSort | filter: search"

    >
  34. <td

    >

    {{member.firstname}}</

    td

    >
  35. <td

    >

    {{member.lastname}}</

    td

    >
  36. <td

    >

    {{member.gender}}</

    td

    >
  37. </

    tr

    >
  38. </

    tbody

    >
  39. </

    table

    >
  40. </

    div

    >
  41. </

    div

    >
  42. <script

    src

    =

    "js/angular.js"

    ></

    script

    >
  43. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  44. </

    body

    >
  45. </

    html

    >

Creating the Script

This code contains the main function of the application. This code will sort and filter the table data when the user manipulate the inputs. 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"

    ,

    gender:

    "Male"

    }

    ,
  5. {

    firstname:

    "Claire"

    ,

    lastname:

    "Temple"

    ,

    gender:

    "Female"

    }

    ,
  6. ]

    ;

  7. $scope.members

    =

    members;
  8. $scope.reverseSort

    =

    false

    ;

  9. $scope.sortData

    =

    function

    (

    column)

    {
  10. $scope.reverseSort

    =

    (

    $scope.sortColumn

    ==

    column)

    ?

    !

    $scope.reverseSort

    :

    false

    ;
  11. $scope.sortColumn

    =

    column;

  12. }

  13. $scope.getSortClass

    =

    function

    (

    column)

    {
  14. if

    (

    $scope.sortColumn

    ==

    column)

    {
  15. return

    $scope.reverseSort

    ?

    'down-arrow'

    :

    'up-arrow'

    ;
  16. }
  17. return

    ''

    ;
  18. }
  19. }

    )

    ;

There you have it we successfully created a Search Filter And Sort 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 the hidden content.
 

452,496

329,696

329,704

Top