Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added filter method support #196

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion array-datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ||
Expand Down
33 changes: 30 additions & 3 deletions data-table-column.html
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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)',
Expand Down Expand Up @@ -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
});
}
});
</script>
Expand Down
25 changes: 13 additions & 12 deletions iron-data-table.html
Original file line number Diff line number Diff line change
Expand Up @@ -659,39 +659,40 @@
_columnsChanged: function(columns, oldColumns) {
if (oldColumns) {
oldColumns.forEach(function(column) {
this.unlisten(column, 'column-filter-changed');
this.unlisten(column, 'filter-value-changed');
}.bind(this));
}

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() {
Expand Down