Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 0345d02

Browse files
author
Arthur Evans
committed
Merge pull request #1431 from Polymer/filter-api
Fixes #1381, #1414, #1191. Update docs for filter API.
2 parents f8c2332 + 0325a37 commit 0345d02

File tree

1 file changed

+84
-2
lines changed

1 file changed

+84
-2
lines changed

1.0/docs/devguide/templates.md

+84-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ tree. For example:
7272

7373
this.push('employees', { first: 'Jack', last: 'Aubrey' });
7474

75+
You can use the `render` method to force a `dom-repeat` to update (for example,
76+
if you're using a third-party library that mutates the array).
77+
7578
### Handling events in `dom-repeat` templates {#handling-events}
7679

7780
When handling events generated by a `dom-repeat` template instance, you
@@ -134,8 +137,10 @@ model data that generated a given element. (There are also corresponding
134137
To filter or sort the _displayed_ items in your list, specify a `filter` or
135138
`sort` property on the `dom-repeat` (or both):
136139

137-
* `filter`. Specifies a filter callback function following the standard
138-
`Array` [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) API.
140+
* `filter`. Specifies a filter callback function, that takes a single argument
141+
(the item) and returns true to display the item, false to omit it.
142+
Note that this is **similar** to the standard
143+
`Array` [`filter`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) API, but the callback only takes a single argument, the array item. For performance reasons, it doesn't include the `index` argument. See [Filtering on array index](#filtering-on-index) for more information.
139144
* `sort`. Specifies a comparison function following the standard `Array`
140145
[`sort`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) API.
141146

@@ -166,6 +171,83 @@ Changing a `manager.type` field should now cause the list to be re-sorted:
166171

167172
this.set('employees.0.manager.type', 'engineer');
168173

174+
#### Dynamic sort and filter changes
175+
176+
The `observe` property lets you specify item sub-properties to
177+
observe for filtering and sorting purposes. However, sometimes you want to
178+
dynamically change the sort or filter based on another unrelated value. In
179+
this case, you can use a computed binding to _return_ a dynamic filter or
180+
sort function when one or more dependent properties changes.
181+
182+
<dom-module id="employee-search">
183+
184+
<template>{% raw %}
185+
<input value="{{searchString::input}}">
186+
<template is="dom-repeat" items="{{employees}}" as="employee"
187+
filter="{{computeFilter(searchString)}}">
188+
<div>{{employee.lastname}}, {{employee.firstname}}</div>
189+
</template>{% endraw %}
190+
</template>
191+
192+
<script>
193+
Polymer({
194+
is: "employee-search",
195+
computeFilter: function(string) {
196+
if (!string) {
197+
// set filter to null to disable filtering
198+
return null;
199+
} else {
200+
// return a filter function for the current search string
201+
string = string.toLowerCase();
202+
return function(employee) {
203+
var first = employee.firstname.toLowerCase();
204+
var last = employee.lastname.toLowerCase();
205+
return (first.indexOf(string) != -1 ||
206+
last.indexOf(string) != -1);
207+
};
208+
}
209+
},
210+
properties: {
211+
employees: {
212+
type: Array,
213+
value: function() {
214+
return [
215+
{ firstname: "Jack", lastname: "Aubrey" },
216+
{ firstname: "Anne", lastname: "Elliot" },
217+
{ firstname: "Stepehen", lastname: "Maturin" },
218+
{ firstname: "Emma", lastname: "Woodhouse" }
219+
]
220+
}
221+
}
222+
}
223+
});
224+
</script>
225+
</dom-module>
226+
227+
In this example, whenever the value of the `searchString` property changes,
228+
`computeFilter` is called to compute a new value for the `filter` property.
229+
230+
#### Filtering on array index {#filtering-on-index}
231+
232+
Because of the way {{site.project_title}} tracks arrays internally, the array
233+
index isn't passed to the filter function. Looking up the array index for an
234+
item is an O(n) operation. Doing so in a filter function could have
235+
**significant performance impact**.
236+
237+
If you need to look up the array index and are willing to pay the performance penalty,
238+
you can use code like the following:
239+
240+
filter: function(item) {
241+
var index = this.items.indexOf(item);
242+
...
243+
}
244+
245+
The filter function is called with the `dom-repeat` as the `this` value, so
246+
you can access the original array as `this.items` and use it to look up the index.
247+
248+
This lookup returns the items index in the **original** array, which may not match
249+
the index of the array as displayed (filtered and sorted).
250+
169251
### Nesting dom-repeat templates {#nesting-templates}
170252

171253
When nesting multiple `dom-repeat` templates, you may want to access data

0 commit comments

Comments
 (0)