This examples is about filtering user by places. Here tag search implemented with auto complete feature by Angular Material UI
Lets implement step by step

JSON Data

This is user information in JSON format
$scope.userData = [{
        "id": "1",
        "name": "Srinivas Dasari",
        "place": "Chennai",
        "pic": "https://lh3.googleusercontent.com/-nBqk0jHahk8/VnL9EvT6IPI/AAAAAAAADdU/4JB3A-1zjTM/s160-Ic42/srinivas_dasari.jpg"
      }, {
        "id": "2",
        "name": "Srinivas Tamada",
        "place": "Atlanta",
        "pic": "https://lh3.googleusercontent.com/-lkebnHGHGcs/VnL9E4nO_FI/AAAAAAAADdY/z2IWGONMG8E/s160-Ic42/srinivas_tamada.jpg"
      }, {
        "id": "3",
        "name": "Sri Harsha",
        "place": "Hyderabad",
        "pic": "https://lh3.googleusercontent.com/-x69ytvh8GkA/VnL9Dqye1jI/AAAAAAAADdM/pBDbJRX8Vxo/s160-Ic42/harsha.jpg"
      }, {
        "id": "4",
        "name": "Lokesh Raghuram",
        "place": "London",
        "pic": "https://lh3.googleusercontent.com/-7XM-i20j7CQ/VnL9EZnDLYI/AAAAAAAADdQ/kR99DUgRhak/s160-Ic42/lokesh.jpg"
      }, {
        "id": "5",
        "name": "Bala Ganesh",
        "place": "Chennai",
        "pic": "https://lh3.googleusercontent.com/-snzuBGu0CJE/VnL9DksYVSI/AAAAAAAADdA/7Y0EvQbWc7I/s160-Ic42/ganesh.jpg"
      }, {
        "id": "6",
        "name": "Arun kumar",
        "place": "Visakhapatnam",
        "pic": "https://lh3.googleusercontent.com/-HiQ4bqPp-fk/VnL9Dqo7u0I/AAAAAAAADdc/NqhwXqgb4Ac/s160-Ic42/arun.jpg"
      }];

Tags Material UI

  1. selectedUsers - it will contain list of tags
  2. selectedItem - it is selected item from auto complete
  3. searchText - user entered search text
  4. querySearch(searchText) - filters user data
  5. transformChip($chip) - convert user data to tags
<md-chips ng-model="selectedUsers" md-autocomplete-snap
         md-transform-chip="transformChip($chip)" class="chipsDiv">
  <md-autocomplete
      md-selected-item="selectedItem"
      md-search-text="searchText"
      md-items="user in querySearch(searchText)"
      md-item-text="user.name"
      placeholder="Search for a user">
    <span md-highlight-text="searchText">{{user.place}}</span>
  </md-autocomplete>
  <md-chip-template>
    <span>
      <strong>{{$chip}}</strong>
    </span>
  </md-chip-template>
</md-chips>

Filter User Data

selectedTags - Custom filter to filter user data
<md-card style="width: 100%;height: 144px;" ng-repeat="user in userData | selectedTags:selectedUsers">
  <md-card-title>
    <md-card-title-text>
      <span class="md-headline">{{user.name}}</span><br>
      <span class="md-subhead">{{user.place}}</span>
    </md-card-title-text>
    <md-card-title-media>
      <div class="md-media-md card-media">
       <img src="{{user.pic}}"/>
      </div>
    </md-card-title-media>
  </md-card-title>
</md-card>

AngularJS Code

Find below for AngularJS code
angular.module('MyApp', ['ngMaterial', 'ngMessages'])
.controller('AppCtrl', function($scope, $mdSidenav) {
    
    // User Information
    $scope.userData = [{
        "id": "1",
        "name": "Srinivas Dasari",
        "place": "Chennai",
        "pic": "https://lh3.googleusercontent.com/-nBqk0jHahk8/VnL9EvT6IPI/AAAAAAAADdU/4JB3A-1zjTM/s160-Ic42/srinivas_dasari.jpg"
      }, {
        "id": "2",
        "name": "Srinivas Tamada",
        "place": "Atlanta",
        "pic": "https://lh3.googleusercontent.com/-lkebnHGHGcs/VnL9E4nO_FI/AAAAAAAADdY/z2IWGONMG8E/s160-Ic42/srinivas_tamada.jpg"
      }, {
        "id": "3",
        "name": "Sri Harsha",
        "place": "Hyderabad",
        "pic": "https://lh3.googleusercontent.com/-x69ytvh8GkA/VnL9Dqye1jI/AAAAAAAADdM/pBDbJRX8Vxo/s160-Ic42/harsha.jpg"
      }, {
        "id": "4",
        "name": "Lokesh Raghuram",
        "place": "London",
        "pic": "https://lh3.googleusercontent.com/-7XM-i20j7CQ/VnL9EZnDLYI/AAAAAAAADdQ/kR99DUgRhak/s160-Ic42/lokesh.jpg"
      }, {
        "id": "5",
        "name": "Bala Ganesh",
        "place": "Chennai",
        "pic": "https://lh3.googleusercontent.com/-snzuBGu0CJE/VnL9DksYVSI/AAAAAAAADdA/7Y0EvQbWc7I/s160-Ic42/ganesh.jpg"
      }, {
        "id": "6",
        "name": "Arun kumar",
        "place": "Visakhapatnam",
        "pic": "https://lh3.googleusercontent.com/-HiQ4bqPp-fk/VnL9Dqo7u0I/AAAAAAAADdc/NqhwXqgb4Ac/s160-Ic42/arun.jpg"
      }];
    
    $scope.selectedUsers = [];
    
    // this method is for filtering places based on searchText
    $scope.querySearch = function(query) {
        var results = [];
        var places = [];
        query = query.toLowerCase();
        angular.forEach($scope.userData, function(user, key) {
            if(places.indexOf(user.place.toLowerCase()) == -1 && user.place.toLowerCase().indexOf(query) > -1) {
                results.push(user); 
                places.push(user.place.toLowerCase())
            }
        });
        return results;
    };
    
    // Display places as tags
    $scope.transformChip = function(chip) {
          return chip.place;
    };
}).filter('selectedTags', function() {
    return function(users, tags) {
        return users.filter(function(user) {
            if (tags.indexOf(user.place) != -1) {
                return true;
            } else if(tags.length == 0) {
                return true;
            }
            return false;

        });
    };
});

1 comment:

Blogroll

Follow this blog by Email

Popular Posts