简体   繁体   English

ngRepeat按deep属性过滤

[英]ngRepeat Filter by deep property

If I have a complex object with objects as property values, how can I filter by one of the nested properties? 如果我有一个以对象作为属性值的复杂对象,如何按嵌套的属性之一进行过滤?

Can this be done with the OOB ng-repeat filter? 可以使用OOB ng-repeat过滤器完成此操作吗?

Data 数据

{
  Name: 'John Smith',
  Manager: {
     id: 123,
     Name: 'Bill Lumburg'
  }
}

ngRepeat ngRepeat

<li ng-repeat="e in emps | filter:Manager.Name">{{ e.Name }}</li>

You need to pass in the argument to filter by: 您需要传递参数以过滤:

<input ng-model="filter.key">
<ul>
  <li ng-repeat="e in list | filter: {Manager: {Name: filter.key}}">
    {{e.Name}}  (Manager: {{e.Manager.Name}})
  </li>
</ul>

Example on Plunker 柱塞示例

If you are filtering multiple properties then the syntax would be similar to below. 如果要过滤多个属性,则语法将类似于以下内容。

<ul>
  <li ng-repeat="item in list | {filter: top_object_property_name: value, top_object_property_with_nested_objects_name: {nested_object_property_name: value}}">
       ...
  </li>
</ul>

eg: 例如:

        var employees = [name: 'John', roles: [{roleName: 'Manager'},{roleName: 'Supervisor'}]];

        <li ng-repeat="staff in employees | {filter: name: 'John', roles: {roleName: 'Manager'}}">
              ...
        </li>

In latest version of angularjs nested obj filter implemented by default.can use filter normally. 在最新版本的angularjs中,嵌套obj过滤器默认实现。可以正常使用过滤器。 It for angular 1 only 仅适用于角度1

To filter with multiple deep property we need to create custom filter. 要使用多个深度属性进行过滤,我们需要创建自定义过滤器。 What i mean we need to create our own function to filter the data in object and return the required object(filtered object). 我的意思是我们需要创建自己的函数来过滤对象中的数据并返回所需的对象(已过滤的对象)。

For example i need to filter data from below object - 例如我需要从下面的对象过滤数据-

[
{
   "document":{
      "documentid":"1",
      "documenttitle":"test 1",
      "documentdescription":"abcdef"
       }
},
{
   "document":{
      "documentid":"2",
      "documenttitle":"dfjhkjhf",
      "documentdescription":"dfhjshfjdhsj"
       }
}
]

In HTML we use ng-repeat to show document list - 在HTML中,我们使用ng-repeat来显示文档列表-

<div>
   //search input textbox
   <input ng-model="searchDocument" placeholder="Search">
 </div>
<div ng-repeat="document in documentList | filter: filteredDocument">
   //our html code 
</div>

In Controller we write filter function to return filtered object by using two properties of object which are "documenttitle" and "documentdescription", code example is as below - 在Controller中,我们编写过滤器函数以使用对象的两个属性“ documenttitle”和“ documentdescription”返回过滤后的对象,代码示例如下-

function filterDocuments(document)
        {
            if($scope.searchDocument)
            {
                     if(document.documentTitle.toLowerCase().indexOf($scope.searchDocument.toLowerCase()) !== -1 || document.document.shortDescription.toLowerCase().indexOf($scope.searchDocument.toLowerCase()) !== -1)
                {
                    //returns filtered object
                    return document
                }
            }else {
               return document;
            }
        }

Where $scope.searchDocument is the scope variable which binded to the search textbox (HTML input tag) in which user can input the text to search. 其中$ scope.searchDocument是绑定到搜索文本框(HTML输入标签)的范围变量,用户可以在其中输入要搜索的文本。

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

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