Angularjs - 过滤器不过滤一列但过滤其他列

时间:2014-03-01 04:22:14

标签: javascript angularjs filter

我从未在angularjs中广泛使用过滤器,我只看到了我的一个项目的问题..

我应用于表的过滤器正在过滤除了与转发器中两个项的值相乘的列之外的所有列。

1。这是否意味着以这种方式工作?

2。有没有办法过滤将数字相乘或将字符串相加的列?

以下是带有工作(不工作)示例的plunker http://plnkr.co/edit/pmCjQL39BWWowIAgj9hP?p=preview

我有一个过滤器,我应用于ng-repeater ......

<input type="text" ng-model="tableSearch">

然后在表格中..

<tr ng-repeat="items in tableData | filter:tableSearch">
<td> {{ items.qty }} </td>
<td> {{ items.price }} </td>

// hm.....
<td> {{ items.qty*items.price }} </td>

</tr>

更新

感谢@Chandermani的指导,我解决了编写自定义过滤器的问题。这是更新的插件:http://plnkr.co/edit/pmCjQL39BWWowIAgj9hP?p=preview

    $scope.tableFilter = function(item) {
        for (key in item) {
            var m = item[key].toString();
            var s = (item.qty * item.price).toString();
            if( 
                s.indexOf($scope.tableSearch) != -1 ||
                m.indexOf($scope.tableSearch) != -1 ||
                $scope.tableSearch == undefined
            ){
                return true;
            };
        }
        return false;
    }

然后像这样使用过滤器:

<tr ng-repeat="items in tableData | filter:tableFilter">

1 个答案:

答案 0 :(得分:1)

过滤模型数据的工作,在这种情况下是这个数据

 {
        "id": 1,
        "name": "Apples",
        "price": 19.99,
        "qty": 1
    },
    {
        "id": 2,
        "name": "Oranges",
        "price": 29.99,
        "qty": 1
    },

最后一列已在html中生成。您必须编写自定义过滤器函数来支持该函数,或者还将最后一列值添加到模型中。

对于自定义过滤功能,您可以执行类似的操作

$scope.tableFilter=function(item) {
    //should return true for the item to show up.
}

请参阅过滤器http://docs.angularjs.org/api/ng/filter/filter

的文档
相关问题