From c2d3635f6ab6a53133c6c2a12ac965ba7bd4a198 Mon Sep 17 00:00:00 2001 From: troggendorf Date: Mon, 6 Mar 2017 09:03:50 +0100 Subject: [PATCH] added filter method support --- array-datasource.js | 7 ++++++- data-table-column.html | 33 ++++++++++++++++++++++++++++++--- iron-data-table.html | 25 +++++++++++++------------ 3 files changed, 49 insertions(+), 16 deletions(-) diff --git a/array-datasource.js b/array-datasource.js index a326521..c6a4c1d 100644 --- a/array-datasource.js +++ b/array-datasource.js @@ -6,7 +6,12 @@ function ArrayDataSource(arr) { return Array.prototype.filter.call(items, function(item, index) { for (var i = 0; i < filter.length; i++) { - var value = Polymer.Base.get(filter[i].path, item); + if(filter[i].method) { + if(filter[i].method(item, index, items)) continue; + else return false; + } + if(!filter[i].path) continue; + var value = Polymer.Base.get(filter[i].path, item); if ([undefined, null, ''].indexOf(filter[i].filter) > -1) { continue; } else if ([undefined, null].indexOf(value) > -1 || diff --git a/data-table-column.html b/data-table-column.html index b343c32..58704ee 100644 --- a/data-table-column.html +++ b/data-table-column.html @@ -40,6 +40,13 @@ */ filterValue: String, + /** + * Filter is a function or the name of a function of the host. This is similar + * to the filter property of Polymer's dom-repeat + * (https://www.polymer-project.org/1.0/docs/api/dom-repeat) + */ + filter: {}, + /** * Minimum width of the column */ @@ -129,8 +136,9 @@ observers: [ '_alignRightChanged(table, alignRight)', - '_filterValueChanged(table, filterValue, filterBy)', + '_filterChanged(table, filter)', '_filterByChanged(table, filterBy)', + '_filterValueChanged(table, filterValue)', '_flexChanged(table, flex)', '_headerTemplateChanged(table, headerTemplate)', '_hiddenChanged(table, hidden)', @@ -186,11 +194,30 @@ _filterByChanged: function(table, filterBy) { this._notifyTable(table, 'filterBy', filterBy); + this._columnFilterChanged(); }, - _filterValueChanged: function(table, filterValue, filterBy) { + _filterValueChanged: function(table, filterValue) { this._notifyTable(table, 'filterValue', filterValue); - this.fire('column-filter-changed', {value: filterValue, filterBy: filterBy}); + this._columnFilterChanged(); + }, + + _filterChanged: function(table, filter) { + this._notifyTable(table, 'filter', filter); + this._columnFilterChanged(); + }, + + _columnFilterChanged: function(table, filter, filterValue, filterBy) { + var siblings = this.parentNode.childNodes, idx; + for(var i = 0; i < siblings.length; i++) { + if(siblings[i] === this) idx = i; + } + this.fire('column-filter-changed', { + index:idx, + filter:this.filter, + value: this.filterValue, + filterBy: this.filterBy + }); } }); diff --git a/iron-data-table.html b/iron-data-table.html index a4466af..3b4fb03 100644 --- a/iron-data-table.html +++ b/iron-data-table.html @@ -659,6 +659,7 @@ _columnsChanged: function(columns, oldColumns) { if (oldColumns) { oldColumns.forEach(function(column) { + this.unlisten(column, 'column-filter-changed'); this.unlisten(column, 'filter-value-changed'); }.bind(this)); } @@ -666,32 +667,32 @@ if (columns) { columns.forEach(function(column) { column.table = this; - this.listen(column, 'filter-value-changed', '_onColumnFilterChanged'); + this.listen(column, 'column-filter-changed', '_onColumnFilterChanged'); }.bind(this)); } }, _onColumnFilterChanged: function(e) { + var filter = { + index:e.detail.index, + method: (typeof(e.detail.filter) === "string") ? this.domHost[e.detail.filter] : e.detail.filter, + filter:e.detail.value, + path: e.detail.filterBy + }; for (var i = 0; i < this.filter.length; i++) { - if (this.filter[i].path === e.detail.filterBy) { - this.set('filter.' + i + '.filter', e.detail.value); + if (this.filter[i].index === e.detail.index) { + this.set('filter.' + i, filter); // selectedItems.filter is actually already set at this point when // clearSelection is called after filter is set. - this.set('selectedItems.filters.' + i + '.filter', e.detail.value); + this.set('selectedItems.filters.' + i, filter); return; } } - this.push('filter', { - path: e.detail.filterBy, - filter: e.detail.value - }); + this.push('filter', filter); - this.push('selectedItems.filters', { - path: e.detail.filterBy, - filter: e.detail.value - }); + this.push('selectedItems.filters', filter); }, _resizeCellContainers: function() {