简体   繁体   English

AngularJS过滤器列表中的子元素

[英]AngularJS filter list on sub element in array

I am trying to create a simple AngularJS filter on a list. 我试图在列表上创建一个简单的AngularJS过滤器。 However the element I want to filter on is a sub-element in an array. 但是,我要过滤的元素是数组中的子元素。

( JSFIDDLE ) JSFIDDLE

The objects: 对象:

elements = [{
    'claim': [{value:'foo'}],
    class: 'class1',
    subject: 'name1'
}, {
    'claim':  [{value:'bar'}],
    class: 'class1',
    subject: 'name2'
}, {
    'claim':  [{value:'baz'}],
    class: 'class1',
    subject: 'name2'
}, {
    'claim':  [{value:'quux'}],
    class: 'class1',
    subject: 'name3'
}];

HTML: HTML:

<tr class='claimrow' 
    data-ng-repeat='c in elements | filter:{claim[0].value:srcBoxFilter} | orderBy: "claim[0].value" '>

The order by handles the syntax "claim[0].value" but the filter does not. 处理语句“claim [0] .value”,但过滤器不处理。

This generates an error: 这会产生错误:

Error: Syntax Error: Token '[' is unexpected, expecting [:] 
at column 25 of the expression 
[elements | filter:{claim[0].value:srcBoxFilter} | orderBy: "claim[0].value"] 
starting at [[0].value:srcBoxFilter} | orderBy: "claim[0].value"].

Interesting problem. 有趣的问题。 Especially because the nested array value works for ordering but not filtering. 特别是因为嵌套数组值适用于排序但不适用于过滤。 I had a play with your JSFiddle and couldn't get anything out of the box working. 我和你的JSFiddle有一个游戏,并且无法开箱即用。 You could get around this very easily by writing your own filter. 你可以通过编写自己的过滤器轻松解决这个问题。

I've updated your JSFiddle to show how you could do this. 我已经更新了你的JSFiddle来展示你如何做到这一点。 The only changes I made to your code are shown below: 我对您的代码所做的唯一更改如下所示:

JavaScript JavaScript的

myApp.filter('byFirstClaimValue', function () {
    return function (items, filterText) {
        if(!filterText)
            return items;

        var out = [];
        for (var i = 0; i < items.length; i++) {
            if (items[i].claim[0].value.toLowerCase().indexOf(filterText.toLowerCase()) > -1) {
                out.push(items[i]);
            }
        }
        return out;
    }
});

HTML HTML

<tr class="claimrow" data-ng-repeat="c in elements | byFirstClaimValue:srcBoxFilter | orderBy: 'claim[0].value'">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM