SINGLE SEARCH FIELD FOR SEARCHING IN MULTIPLE FIELDS
- Create a method in controller to :
- Match the content in specific field with the searched content
- Return result (true / false) on the basis of condition matched
This method will be called for each row and return the flag to display that row
- Call that method with |filter : |filter:<method>
- Method will take current element in iteration as a parameter
JS
var app = angular.module('module1', []);
app.controller("myController", function ($scope) {
var employees = [
{ name : "Shaan", city : "Jhansi", salary : 25000 },
...
]
$scope.employees = employees;
$scope.search = function(item) {
if ($scope.searchText == undefined) {
return true;
} else {
if (item.name.toLowerCase().indexOf(
$scope.searchText.toLowerCase()) != -1 ||
item.city.toLowerCase().indexOf(
$scope.searchText.toLowerCase()) != -1) {
return true;
}
}
return false;
}
});
HTML
<div ng-controller="myController">
<input type="text" placeholder="Search City and Name"
ng-model="searchText" />
<br/><br/>
<table>
<thead>
<tr> <th>Name</th> <th>City</th> <th>Salary</th> </tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees |filter:search">
<td> {{ employee.name }} </td>
<td> {{ employee.city }} </td>
<td> {{ employee.salary }} </td>
</tr>
</tbody>
</table>
</div>
No comments:
Post a Comment
Note: only a member of this blog may post a comment.