How to filter an iron-list in polymer 1.0?

走远了吗. 提交于 2019-12-21 04:31:18

问题


The dom-repeat element offers a filter attribute.

Is there a similar way to filter with iron-list?

For example: Given a list of people, I want to filter the ones born in a specific city.


回答1:


As iron-list unfortunately doesn't offer a filter attribute, there is no declarative pattern making this possible.

You can either implement your own simple list element making use of dom-repeat's filter property. (With element inheritance coming back in future releases, you might extend iron-list).

However, the best practice I currently see is the use of a computed property:

<template>
  <iron-list items="[[filterItems(items)]]" as="item">
    ...
  </iron-list>
</template>

<script>
Polymer({
  ...
  filterItems: function (items) {
    return items.filter(function (item) { // Array.prototype.filter
      return item.priority > 8; // Filter condition
    });
  }
});
</script>


来源:https://stackoverflow.com/questions/33139095/how-to-filter-an-iron-list-in-polymer-1-0

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