Perform filter in controller to search in fields with OR condition

☆樱花仙子☆ 提交于 2019-12-11 03:13:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!