【问题标题】:AngularJS: filter by inner array object fieldAngularJS:按内部数组对象字段过滤
【发布时间】:2013-07-19 20:51:47
【问题描述】:

我有以下数据:

organizations:[{
                name: "foo",
                contacts: [{
                             firstName = "John",
                             lastName = "Doe",
                             email = "john.doe@email.com"
                          },...]
               },...]

然后我有一个表,其中列出了所有组织,我想知道是否可以根据 firstName 过滤器或电子邮件过滤器过滤表中的行。

例如我有这个代码:

<input type="text" id="name" ng-model="search.name">

<tr ng-repeat="organization in organizations | filter:search">
    <td>{{organization.name}}</td>
    <td>{{client.contacts[0].firstName}} {{ client.contacts[0].lastName }}</td>
    <td>{{client.contacts[0].email}}</td>
</tr>

它只能通过“名称”字段进行过滤。我尝试过这样的事情:

<input type="text" id="firstName" ng-model="search.contacts">

但它在联系人数组中的对象的所有字段中搜索,但我想专门按名字搜索。我该怎么办?

【问题讨论】:

  • 你试过了吗
  • 我试过了,但我认为不起作用,因为联系人是一个数组。

标签: javascript angularjs


【解决方案1】:

使用像filter:filterBy这样的过滤函数。

示例(依赖 underscore.js):

scope.filterBy = function(row) {
    return !!_.where(row.contacts, {firstName:scope.search.name}).length;
}

【讨论】:

  • 关于过滤器需要注意的一点是它们需要快速。如果由于任何原因您发现过滤器使您放慢了速度,请尝试 $watch-ing 过滤器属性,然后手动过滤您的数组并在模板中对过滤后的数组进行 ng-repeat
  • 确实是@LiviuT。对于少量的行,过滤器应该没问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
  • 2016-08-09
  • 2016-01-21
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 1970-01-01
相关资源
最近更新 更多