问题
I Have a data list say
$scope.data = [
{"id":1,"buyername":"kamaldeep282","status":"kdsingh1002","user":"Hitesh"},
{"id":2,"buyername":"kamaldeep282","status":"RELEASED","user":"kdsingh1002"},
{"id":3,"buyername":"kamaldeep282","status":"RELEASED","user":"Hitesh"},
{"id":4,"buyername":"kdsingh1002","status":"RELEASED","user":"Hitesh"},
{"id":5,"buyername":"kamaldeep282","status":"DISPUTE","user":"kdsingh1002"}
];
Now I applied a filter in HTML as:-
<input type="text" ng-model="searchText" ng-change="filterData()">
and defined filterData() in controller as:-
$scope.filterData = function() {
$scope.finalData = $filter("filter")($scope.data, { buyername:$scope.searchText, status:$scope.searchText });
};
Now what I want is OR of buyername, status i.e in searchText if I entered 'kdsingh1002' then these rows should be returned:- {"id":1,"buyername":"kamaldeep282","status":"kdsingh1002","user":"Hitesh"}, {"id":4,"buyername":"kdsingh1002","status":"RELEASED","user":"Hitesh"},
Note: I don't want to search in all fields.
回答1:
not sure about OR condition with $filter but you can use angular.forEach to filter your records.
Here is working plunker
filterData function :
$scope.filterData = function() {
$scope.finalData = [];
angular.forEach($scope.data,function(data){
if(data.status.indexOf($scope.searchText) > -1 || data.buyername.indexOf($scope.searchText) > -1){
$scope.finalData.push(data);
}
})
};
来源:https://stackoverflow.com/questions/32346260/perform-filter-in-controller-to-search-in-fields-with-or-condition