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
}
}
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
}
}
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>
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.
Check out these:
Similar question: ng-repeat :filter by single field
Documentation: https://docs.angularjs.org/api/ng/filter/filter
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>