With List of objects how can the `filter` filter know which fields to match on?

风格不统一 提交于 2019-12-24 07:22:08

问题


I was using filter:query on a json response to filter. Everything worked fine. When I turned the json response into proper classes the filter stopped working. What is the approach to use query to match fields on a class? Is this the situation where I should then create my own custom @NgFilter?


回答1:


For the AngularDart filter filter you specify which fields to match on by passing the field names as the keys to a map.

Let us use the AngularDart tutorial Recipe class as an example:

  class Recipe { String id; String name; String category; ... }

You can filter, e.g., on Recipe.name using the string ctrl.nameFilterString write:

  <li ng-repeat="recipe in ctrl.recipes | filter:{name:ctrl.nameFilterString}">

Here is an excerpt from the filter API doc that explains how filter interprets its argument. I.e. in

  v | filter:x

the argument x can be a

  • String, bool and num:  Only items in the List that directly match this expression, items that are Maps with any value matching this item and items that are Lists containing a matching items are returned.

  • Map:  This defines a pattern map.  Filters specific properties on objects contained in the input List.  For example {name:"M", phone:"1"} predicate will return a list of items which have property name containing "M" and property phone containing "1".  A special property name, $, can be used (as in {$: "text"}) to accept a match against any property of the object. That's equivalent to the simple substring match with a String as described above.




回答2:


I'm not sure what you mean by 'filter:query'. I guess it is the filter filter.

filter seems to convert the items in the collection to string and compares them with the search text. If the toString() doesn't contain the field you want to compare it won't match.

A custom filter is probably the best option.
Example:
angular.dart.tutorial / Chapter_07 / lib / filter / category_filter.dart



来源:https://stackoverflow.com/questions/21766393/with-list-of-objects-how-can-the-filter-filter-know-which-fields-to-match-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!