在控制器中执行过滤器以搜索具有OR条件的字段

时间:2015-09-02 06:44:35

标签: angularjs angularjs-scope angularjs-ng-repeat angular-filters

我有一个数据列表说

$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"}
];

现在我在HTML中应用了一个过滤器: -

<input type="text" ng-model="searchText" ng-change="filterData()">

并在控制器中将filterData()定义为: -

$scope.filterData = function() {
    $scope.finalData = $filter("filter")($scope.data, { buyername:$scope.searchText, status:$scope.searchText });
};

现在我想要的是买家名称,状态,即在searchText,如果我输入&#39; kdsingh1002&#39;那么这些行应该返回: - {&#34; ID&#34;:1,&#34; BUYERNAME&#34;:&#34; kamaldeep282&#34;&#34;状态&#34;:&#34; kdsingh1002&#34;&# 34;用户&#34;:&#34;亚太区首席技术官Matt&#34;}, {&#34; ID&#34:4,&#34; BUYERNAME&#34;:&#34; kdsingh1002&#34;&#34;状态&#34;:&#34; RELEASED&#34;&# 34;用户&#34;:&#34;亚太区首席技术官Matt&#34;},

注意: 我不想在所有领域进行搜索。

1 个答案:

答案 0 :(得分:0)

不确定$filter的OR情况,但您可以使用angular.forEach来过滤记录。

这是working plunker

filterData 功能:

$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);   
        }
   })
};