Skip to content

Commit

Permalink
Table: clearFilter can clear the filters of specified columns
Browse files Browse the repository at this point in the history
  • Loading branch information
elfman committed Oct 26, 2018
1 parent 31d31d9 commit 212a655
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/docs/en-US/table.md
Expand Up @@ -2007,7 +2007,7 @@ You can customize row index in `type=index` columns.
| toggleRowExpansion | used in expandable Table, toggle if a certain row is expanded. With the second parameter, you can directly set if this row is expanded or collapsed | row, expanded |
| setCurrentRow | used in single selection Table, set a certain row selected. If called without any parameter, it will clear selection. | row |
| clearSort | clear sorting, restore data to the original order ||
| clearFilter | clear filter | |
| clearFilter | clear filters for the `columnKey` array passed in. If no params are passed, clear all filters | columnKeys |
| doLayout | refresh the layout of Table. When the visibility of Table changes, you may need to call this method to get a correct layout ||
| sort | sort Table manually. Property `prop` is used to set sort column, property `order` is used to set sort order | prop: string, order: string |

Expand Down
2 changes: 1 addition & 1 deletion examples/docs/zh-CN/table.md
Expand Up @@ -2067,7 +2067,7 @@
| toggleRowExpansion | 用于可展开表格,切换某一行的展开状态,如果使用了第二个参数,则是设置这一行展开与否(expanded 为 true 则展开) | row, expanded |
| setCurrentRow | 用于单选表格,设定某一行为选中行,如果调用时不加参数,则会取消目前高亮行的选中状态。 | row |
| clearSort | 用于清空排序条件,数据会恢复成未排序的状态 ||
| clearFilter | 用于清空过滤条件,数据会恢复成未过滤的状态 | |
| clearFilter | 不传入参数时用于清空所有过滤条件,数据会恢复成未过滤的状态,也可传入由columnKey组成的数组以清除指定列的过滤条件 | columnKey |
| doLayout | 对 Table 进行重新布局。当 Table 或其祖先元素由隐藏切换为显示时,可能需要调用此方法 ||
| sort | 手动对 Table 进行排序。参数`prop`属性指定排序列,`order`指定排序顺序。 | prop: string, order: string |

Expand Down
60 changes: 43 additions & 17 deletions packages/table/src/table-store.js
Expand Up @@ -2,7 +2,7 @@ import Vue from 'vue';
import debounce from 'throttle-debounce/debounce';
import merge from 'element-ui/src/utils/merge';
import { hasClass, addClass, removeClass } from 'element-ui/src/utils/dom';
import { orderBy, getColumnById, getRowIdentity } from './util';
import { orderBy, getColumnById, getRowIdentity, getColumnByKey } from './util';

const sortData = (data, states) => {
const sortingColumn = states.sortingColumn;
Expand Down Expand Up @@ -238,17 +238,24 @@ TableStore.prototype.mutations = {
},

filterChange(states, options) {
let { column, values, silent } = options;
let { column, values, silent, multi } = options;
if (values && !Array.isArray(values)) {
values = [values];
}

const prop = column.property;
const filters = {};

if (prop) {
states.filters[column.id] = values;
filters[column.columnKey || column.id] = values;
if (multi) {
column.forEach(col => {
states.filters[col.id] = values;
filters[col.columnKey || col.id] = values;
});
} else {
const prop = column.property;

if (prop) {
states.filters[column.id] = values;
filters[column.columnKey || column.id] = values;
}
}

let data = states._data;
Expand Down Expand Up @@ -495,7 +502,7 @@ TableStore.prototype.cleanSelection = function() {
}
};

TableStore.prototype.clearFilter = function() {
TableStore.prototype.clearFilter = function(columnKeys) {
const states = this.states;
const { tableHeader, fixedTableHeader, rightFixedTableHeader } = this.table.$refs;
let panels = {};
Expand All @@ -507,17 +514,36 @@ TableStore.prototype.clearFilter = function() {
const keys = Object.keys(panels);
if (!keys.length) return;

keys.forEach(key => {
panels[key].filteredValue = [];
});
if (typeof columnKeys === 'string') {
columnKeys = [columnKeys];
}
if (Array.isArray(columnKeys)) {
const columns = columnKeys.map(key => getColumnByKey(states, key));
keys.forEach(key => {
const column = columns.find(col => col.id === key);
if (column) {
panels[key].filteredValue = [];
}
});
this.commit('filterChange', {
column: columns,
value: [],
silent: true,
multi: true
});
} else {
keys.forEach(key => {
panels[key].filteredValue = [];
});

states.filters = {};
states.filters = {};

this.commit('filterChange', {
column: {},
values: [],
silent: true
});
this.commit('filterChange', {
column: {},
values: [],
silent: true
});
}
};

TableStore.prototype.clearSort = function() {
Expand Down
4 changes: 2 additions & 2 deletions packages/table/src/table.vue
Expand Up @@ -347,8 +347,8 @@
this.store.clearSelection();
},
clearFilter() {
this.store.clearFilter();
clearFilter(columnKeys) {
this.store.clearFilter(columnKeys);
},
clearSort() {
Expand Down
4 changes: 4 additions & 0 deletions packages/table/src/util.js
Expand Up @@ -84,6 +84,10 @@ export const getColumnById = function(table, columnId) {
return column;
};

export const getColumnByKey = function(table, columnKey) {
return table.columns.find(item => item.columnKey === columnKey);
};

export const getColumnByCell = function(table, cell) {
const matches = (cell.className || '').match(/el-table_[^\s]+/gm);
if (matches) {
Expand Down

0 comments on commit 212a655

Please sign in to comment.