Skip to main content

JavaScript - Filter Multiple Properties Using AngularJS


In this tutorial we will create Filter Multiple Properties 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's get started. So Let's do the coding...

Getting started:

First you will need to download & install the AngularJS here's the link

And this is the link for the bootstrap that i used for the layout design

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="">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 Multiple Properties Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="form-inline">
  18. <input type="text" ng-model="filter" class="form-control" placeholder="Search here..."/>
  19. </div>
  20. <br />
  21. <table class="table table-bordered">
  22. <thead class="alert-info">
  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="member in members | filter: search | orderBy: 'lastname'">
  32. <td>{{member.firstname}}</td>
  33. <td>{{member.lastname}}</td>
  34. <td>{{member.gender}}</td>
  35. <td>{{member.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 filter the data base on the properties that have been assigned. To that just kindly copy and write these block of codes inside the text editor, then save it as script.js.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope){
  3. var members = [
  4. {firstname: "John", lastname: "Smith", gender: "Male", address: "USA"},
  5. {firstname: "Pearl", lastname: "Cruz", gender: "Female", address: "France"},
  6. {firstname: "Naruto", lastname: "Uzumaki", gender: "Male", address: "Konoha"},
  7. {firstname: "Ken", lastname: "Kaneki", gender: "Male", address: "Tokyo"},
  8. {firstname: "Sara", lastname: "Moise", gender: "Female", address: "Germany"}
  9. ];
  12. $scope.members = members;
  14. $ = function(keyword){
  15. if($scope.filter == undefined){
  16. return true;
  17. }else{
  18. if(keyword.firstname.toLowerCase().indexOf($scope.filter.toLowerCase()) != -1 || keyword.lastname.toLowerCase().indexOf($scope.filter.toLowerCase()) != -1 || keyword.address.toLowerCase().indexOf($scope.filter.toLowerCase()) != -1){
  19. return true;
  20. }
  21. }
  23. return false;
  24. }
  25. });

There you have it we successfully created a Filter Multiple Properties 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

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