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

JavaScript - Filter JSON Data In AngularJS

alibt

System Dependency Auditor
A Rep
0
0
0
Rep
0
A Vouches
0
0
0
Vouches
0
Posts
73
Likes
87
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial we will create a Filter JSON Data In AngularJS. This code will filter the json data when the user enter a keyword in the form. The code itself use an AngularJS directives that has a several built-functionalities that can filter a data in the table by binding the textbox with ng-model and adding a filter parameter in the ng-repeat. This is a free source code you can modify it and use it on your working programs.

We will be using AngularJS as a JavaScript-based open-source front-end web application framework . 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 have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Lastly, you will need to download the AngularJS here's the link https://angularjs.org/.

Note: This code will only work if run in a local server.

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

    =

    "https://sourcecodester.com"

    >

    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 - Filter JSON Data In AngularJS</

    h3

    >
  16. <hr

    style

    =

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

    /

    >

  17. <div

    class

    =

    "col-md-8"

    >
  18. <div

    class

    =

    "form-inline pull-right"

    >
  19. <input

    type

    =

    "text"

    class

    =

    "form-control"

    ng-model=

    "filter"

    placeholder=

    "Search here..."

    /

    >
  20. </

    div

    >
  21. <br

    /

    ><br

    /

    >
  22. <table

    class

    =

    "table table-bordered"

    >
  23. <thead

    class

    =

    "alert-info"

    >
  24. <tr

    >
  25. <th

    >

    Firstname</

    th

    >
  26. <th

    >

    Lastname</

    th

    >
  27. <th

    >

    Address</

    th

    >
  28. </

    tr

    >
  29. </

    thead

    >
  30. <tbody

    >
  31. <tr

    ng-repeat=

    "member in members | filter: filter"

    >
  32. <td

    >

    {{member.firstname}}</

    td

    >
  33. <td

    >

    {{member.lastname}}</

    td

    >
  34. <td

    >

    {{member.address}}</

    td

    >
  35. </

    tr

    >
  36. </

    tbody

    >
  37. </

    table

    >
  38. </

    div

    >
  39. </

    div

    >
  40. <script

    src

    =

    "js/angular.js"

    ></

    script

    >
  41. <script

    src

    =

    "js/script.js"

    ></

    script

    >
  42. </

    body

    >
  43. </

    html

    >

Creating the Main Function

This code contains the main function of the application. This code will filter the JSON data from a JSON file. To that just kindly copy and write these block of codes inside the text editor, then save it as shown below.

member.json

[
{"firstname": "John", "lastname": "Smith", "address": "New York"},
{"firstname": "Claire", "lastname": "Temple", "address": "Racoon City"}
]

script.js
Note: Save this file inside the js directory as script.js to make the program work.
  1. var

    app =

    angular.module

    (

    'myModule'

    ,

    [

    ]

    )
  2. .controller

    (

    'myController'

    ,

    function

    (

    $scope,

    $http)

    {
  3. $http.get

    (

    'member.json'

    )

    .then

    (

    function

    (

    response)

    {
  4. $scope.members

    =

    response.data

    ;
  5. }

    )

    ;

  6. }

    )

    ;

There you have it we successfully created a Filter JSON Data In 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

332,845

332,853

Top