AngularJS Filter Search Tutorial Image

AngularJS Filter Search Tutorial

Table Of Contents

Links:

In this tutorial I’ll be showing you how you can create a tool which allows you to type in a search query in an input box and see all matches below that input box without having to reload the page and we’ll be leveraging AngularJS’s filter mechanic to do so.

To get us started we’ll start with mocking up what our page will look like, this is just going to be a basic no-frills design to get us started but feel free to modify it to suit.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!DOCTYPE html>
<html ng-app="myApp">
<head>
  <title>Google Search Like Functionality</title>
  <!-- Latest compiled and minified CSS -->
  <link rel="stylesheet" href="bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
  <link rel="stylesheet" href="style.css">
  
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script>
</head>
<body ng-controller="SearchController">
    
  <div class="search-box">
      <div class="row">
          <div class="col-lg-4"></div>
          <div class="col-lg-4">
              <h2>TutorialEdge Search: {{query}}</h2>
              <form class="form">
                  <input class="form-control" type="text" ng-model="query" ng-change="updateValue()">
              </form>
          </div>
          <div class="col-lg-4"></div>
      </div>
  </div>

  <div class="search-results">
      <div class="container">          
          <div class="result" ng-repeat="result in results | filter: query">
              <a href="#"><h2>{{result.title}}</h2></a>
              <p class="url">{{result.link}}</p>
              <p>{{result.content}}</p>
          </div>
      </div>
  </div>
  
  <div class="footer">
      <div class="container">
          <p class="info">
              Full Source Code for this example can be found here: Github Link
          </p>
          <p class="info">
              Original Tutorial Link: http://tutorialedge.net
          </p>
      </div>
  </div>
  
  <script src="AppController.js"></script>
</body>
</html>

Our CSS

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
.search-box{
    background-color: #f1f1f1;
    border-bottom: 1px solid #E5E5E5;
    margin-bottom: 20px;
    padding-top: 20px;
    padding-bottom: 20px;
    text-align: center;
}

.search-box h2{
    color: #4285F4;
    margin: 0;
    padding: 0;
    margin-bottom: 10px;
}

.search-box input {
    border-radius: 0;
}

.result h2{
    font-size: 18px;
        display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    -webkit-text-overflow: ellipsis;
    white-space: nowrap;
}

.result p{
    color: #545454;
    font-size: small;
    font-family: arial,sans-serif;
}
.result{
    padding-bottom: 20px;
    border-bottom: 1px solid #E5E5E5;
}

.result p.url{
        color: #006621;
    font-style: normal;
        font-size: 15px;
        font-weight: bold;
        padding-top: 0px;
        margin-bottom: 5px;
        padding-bottom: 2px;
}

This is just basic html so far but let’s add some angularjs functionality to it and get all of our results listing in a google like manner. We’ll skip pagination as that’s going to be the subject of my next tutorial.

To get us started we’ll just be listing everything from the following JSON array, however you could easily hook this up to a REST api that returns meaningful results if you wished too. We’ll create our own controller for this one-page application:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
var myApp = angular.module('myApp',[]);

myApp.controller('SearchController', ['$scope', function($scope) {
    
    $scope.results = [
        { title : "Cars", link: "http://tutorialedge.net", content: "lorem ipsum doler fox pixel"},
        { title : "Boats", link: "http://tutorialedge.net", content: "lorem ipsum doler cat pixel"},
        { title : "Vans", link: "http://tutorialedge.net", content: "lorem ipsum doler pig pixel"},
        { title : "Limos", link: "http://tutorialedge.net", content: "lorem ipsum doler pixel"}
    ];
    
}]);

Now that we’ve implemented that you should hopefully see the everything in our array listing on our search results page:

Ok so now that we’ve got everything we need to display our search results we can move onto implementing our search box. This will essentially take in our search query string and then filter the results that we’ve listed to show only those that present a match. The match could be present in either the title, the url or the content of the list we are using so it’s kind of similar to the way google shows you pages for things that contain information about specific topics.

The Filter Code:

In order for us to make this list filter-able we simply have to add the following to our ng-repeat statement.

1
2
3
4
5
<div class="result" ng-repeat="result in results | filter: query">
  <a href="#"><h2>{{result.title}}</h2></a>
  <p class="url">{{result.link}}</p>
  <p>{{result.content}}</p>
</div>
Was This Post Helpful?
Submit a PR: Edit on Github