2

How can I use AngularJS filter for search functionality using this json structure?

$scope.jsonObj = {
    idNo1: {
       name: "Diljish",
       age: 24
    },
    idNo2: {
       name: "Shemeem",
       age: 28
    }
}
2
  • 1
    how do you want this to be filtered? Commented Nov 25, 2016 at 12:16
  • Depending on name Commented Nov 25, 2016 at 12:18

4 Answers 4

2

No JavaScript code is required.

You should create input for data to filter:

Filter: <input type="text" ng-model="yourFilter.name"/>

Then, in ng-repeat:

<table>
  <tr>
      <th>Name</th>
      <th>Age</th>
  </tr>
  <tr ng-repeat="person in persons | filter:yourFilter | orderBy:'name'">
     <td>{{ person.name | uppercase}}</td>
     <td>{{ person.age | lowercase}}</td>
   </tr>
</table>

Where persons is your json object.

(function()
{        
    var yourController=function($scope) { 
             $scope.persons= [];

             function init() {
                $scope.persons={
                       idNo1: {
                            name: "Diljish",
                            age: 24
                       },
                       idNo2: {
                            name: "Shemeem",
                            age: 28
                   }}
             }        
             init();
    };    


    yourController.$inject=['$scope'];   

    angular.module('yourApp').controller('yourController',         
                                                yourController);

}());

Update:

It remains the same, if you use another json object:

<body ng-init="people=[{ name:'Shemmem' }, { name:'Diljish' }]">
    Filter: <input type="text" ng-model="yourFilter.name"/>
    <table>
        <tr>
            <td>Name</td>
            <td>City</td>
        </tr>
        <tr ng-repeat="person in people | filter:yourFilter">
            <td>{{ person.name }}</td>
            <td>{{ person.city }}</td>
        </tr>
    </table>
</body>
Sign up to request clarification or add additional context in comments.

2 Comments

<body> <div id="content" ng-controller="MainCtrl"> <input type='text' ng-model='searchText' placeholder="Search a name like : Soheil or something " /> <ul> <li ng-repeat='person in people | filter:searchText'> {{person.name}} </li> </ul> </div> using json $scope.people = [ { name:'Shemmem' }, { name:'Diljish' } ] But in my question json is different
@Diljish please, see my updated section in an answer.
1

If you want to filter it in a ng-repeat you can use a filter with a pipe "|":

<div ng-repeat="person in people | filter: customFilter">
 </div>

Then, in the controller you define the customFilter:

                $scope.customfilter = function (person) {
                    return (person.name == $scope.nameToBeFiltered)}
                        
                

where "nameToBeFiltered" is the name you want to filter (you can ng-model that scope variable to an input in the view).

Now, if you want to filter somewhere else, maybe you are looking for a "Javascript: Find value in Json" rather than AngularJS.

Comments

0

Check out these:

Similar question: ng-repeat :filter by single field

Documentation: https://docs.angularjs.org/api/ng/filter/filter

Comments

0

Please find working code.

Had filter for "name"

(function(angular) {
  'use strict';
angular.module('app', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.filtertext="";
    $scope.jsonObj = {
    idNo1: {
       name: "Diljish",
       age: 24
    },
    idNo2: {
       name: "Shemeem",
       age: 28
    }
}
    
    $scope.filterByName = function(items,filtertext) {
    var result = {};
    angular.forEach(items, function(value, key) {
     
        if  (value.name.toLowerCase().indexOf(filtertext) > -1) {
            result[key] = value;
        }
    });
    return result;
    }
    
    
    
 }]);
})(window.angular);
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example - example-select-with-default-values-production</title>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
  
</head>
<body ng-app="app">
  <div ng-controller="ExampleController">
  <input type="text" ng-model="filtertext"/>
  <hr>
 <div ng-repeat="(k,v) in filterByName(jsonObj,filtertext)">
    {{k}} {{v.name}}
</div>
    
</div>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.