在AngularJS数组的长度或嵌套的对象属性过滤器 [英] Filter by array length or nested object property in AngularJS

查看:404
本文介绍了在AngularJS数组的长度或嵌套的对象属性过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像的对象数组:

I have an array with objects looking like that:

record in recordlist {
    date : "02/12/2014"
    time : "00.02.01"
    car : "369"
    pax: [
        {
        name : "Ben"
        chosen : true
        },
        {
        name : "Eric"
        chosen : true
        }
    ]
}

到目前为止,当我列出使用NG重复,我能够按对象进行过滤(记录)属性。

So far, when I list using ng-repeat, I'm able to filter by object (record) property.

过滤器:

<input class="form-control" placeholder="Time" ng-model="search.time">

NG-重复:

<div ng-repeat="record in filteredRecords = (recordlist | filter: search)">

问题是当我想要过滤的嵌套数组( PAX )。我试过这个,但到目前为止没有运气:

The problem comes when I want to filter the nested array (pax). I've tried this, but so far no luck:

<input ng-model="search.pax.name"> // filter by name property

<input ng-model="search.pax.length"> // filter by array length

任何提示?

推荐答案

您可能需要使用自定义过滤器:

You will probably need to use a custom filter:

这样的事情应该做的:

<input ng-model="searchName">

<div ng-repeat="record in recordlist | filter: filterByNested">

和在你的控制器:

$scope.filterByNested = function (record) {
    return record.pax.reduce(function (prev, curr, idx, array) {
        return prev || curr.name == $scope.searchName;
    }, false);
};

基本上你定义一个自定义过滤器,如果你正在寻找的元素是嵌套数组中返回true。

Basically you define a custom filter that returns true if the element you are searching is in the nested array.

这将是微不足道的改变滤波器功能工作的基础上长度了。

It would be trivial to change the filter function to work based on length too.

这篇关于在AngularJS数组的长度或嵌套的对象属性过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆