在Ionic中按多个字段过滤

时间:2018-01-29 17:29:27

标签: angularjs cordova ionic-framework filter

我已经进入了半完整的Ionic Cordova项目,我想通过product_name或product_code搜索搜索功能,它目前只通过产品名称进行过滤。

<input type="text" ng-model="search.search"  placeholder="Scan/Enter product" ng-change="setFocus()"/>

<div class="odd" ng-if="focus" ng-repeat="mydata in data | filter:{product_name:search.search, product_code:search.text}">

TIA

1 个答案:

答案 0 :(得分:0)

您应该创建自定义orFilter

angular.module('app', []).controller('ctrl', function($scope){
  $scope.data  = [
    {product_name:'Prod1', product_code:'1112'},
    {product_name:'Prod2', product_code:'2223'},
    {product_name:'Prod3', product_code:'3334'}
  ]
}).filter('orFilter', function(){
  return function(data, props, search){
    if(!search)
      return data;
    
    search = search.toLocaleLowerCase();
    return data.filter(function(x){ 
      for(var prop of props)
        if(x[prop].toLocaleLowerCase().indexOf(search) != -1)
         return true;
      return false;
    });
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">
</script>

<div ng-app='app' ng-controller='ctrl'>
  <input type='text' ng-model='search' ng-init='search="2"'/>
  <ul>
    <li ng-repeat='item in data | orFilter: ["product_name", "product_code"]: search'>
      {{item | json}}
    </li>
  </ul>
</div>