在特定多个表达式角度 JS 中过滤

filter in specific multiple expressions angular js

本文关键字:JS 过滤 表达式      更新时间:2023-09-26

>我只想搜索列名或数字,目前我只搜索列名。

<input type="text" ng-model="test.name" placeholder="start typing..">

我的表情,

  <tr ng-repeat="x in names | filter:test | limitTo:totalDisplayed">
                            <td>{{ x.name }}</td>
                            <td>{{ x.number}}</td>
                            <td>{{ x.city }}</td>
                            <td>{{ x.country }}</td> 
                        </tr>

创建自定义筛选器来完成此操作。

.html

<input type="text" ng-model="test.name" placeholder="start typing..">
        <tr ng-repeat="x in names | myFilter: test">
                       <td>{{ x.name }}</td>
                       <td>{{ x.number}}</td>
                       <td>{{ x.city }}</td>
                       <td>{{ x.country }}</td> 
                   </tr>

JS过滤器

angular.module('myApp').filter('myFilter', function () {
      return function (list, input) {
    //input is test object and list is your current array you want to return a filtered array
    var myArray = [];
    list.forEach(function(o, i){
        if(o.name.indexOf(input.name) > -1 || o.number.indexOf(input.name) > -1 )
            myArray.push(o);
    });
    return myArray
};
});