Skip to main content

JavaScript - Simple Search Box Using AngularJs

Language


In this tutorial we will create a Simple Search Box 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.

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 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 - Simple Search Box Using AngularJs</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="form-inline">
  18. <input type="text" class="form-control" ng-model="search" placeholder="Search here..."/>
  19. </div>
  20. <br />
  21. <table class="table table-bordered">
  22. <thead class="alert-success">
  23. <tr>
  24. <th>Firstname</th>
  25. <th>Lastname</th>
  26. <th>Gender</th>
  27. <th>Address</th>
  28. </tr>
  29. </thead>
  30. <tbody style="background-color:#fff;">
  31. <tr ng-repeat="student in students | orderBy: 'lastname' | filter: search">
  32. <td>{{student.firstname}}</td>
  33. <td>{{student.lastname}}</td>
  34. <td>{{student.gender}}</td>
  35. <td>{{student.address}}</td>
  36. </tr>
  37. </tbody>
  38. </table>
  39. </div>
  40. <script src="js/angular.js"></script>
  41. <script src="js/script.js"></script>
  42. </body>
  43. </html>

Creating the Script

This code contains the main function of the application. This code will display data and will filter the content base on the input text box. 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 students = [
  4. {firstname: "John", lastname: "Smith", gender: "Male" , address: "Japan"},
  5. {firstname: "San", lastname: "Juan", gender: "Male" , address: "China"},
  6. {firstname: "Kim", lastname: "Fang", gender: "Female" , address: "USA"},
  7. {firstname: "Stan", lastname: "Lou", gender: "Male" , address: "New York"},
  8. {firstname: "Mike", lastname: "Roger", gender: "Male" , address: "Germany"},
  9. {firstname: "Joan", lastname: "Lia", gender: "Female" , address: "France"},
  10. {firstname: "Mikaela", lastname: "Clars", gender: "Female" , address: "Spain"},
  11. {firstname: "Robert", lastname: "Stark", gender: "Male" , address: "Africa"}
  12. ];
  13.  
  14. $scope.students = students;
  15. });

There you have it we successfully created a Simple Search Box 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!!

 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.