feat: 🎸 Support for multiple filters in columns

Multiple inline filters will be applied as AND filters
This commit is contained in:
Faris Ansari 2018-10-16 13:56:10 +05:30
parent ffe0d2a3f5
commit 932afb6cbe
3 changed files with 27 additions and 14 deletions

View File

@ -270,18 +270,19 @@ export default class ColumnManager {
bindFilter() { bindFilter() {
if (!this.options.inlineFilters) return; if (!this.options.inlineFilters) return;
this.appliedFilters = this.appliedFilters || {};
const handler = e => { const handler = e => {
this.$filterCell = $.closest('.dt-cell', e.target); this.$filterCell = $.closest('.dt-cell', e.target);
const { colIndex } = $.data(this.$filterCell); const { colIndex } = $.data(this.$filterCell);
const keyword = e.target.value; const keyword = e.target.value;
this.appliedFilters[colIndex] = keyword;
this.applyFilter(keyword, colIndex); this.applyFilter(this.appliedFilters);
}; };
$.on(this.header, 'keydown', '.dt-filter', debounce(handler, 300)); $.on(this.header, 'keydown', '.dt-filter', debounce(handler, 300));
} }
applyFilter(keyword, colIndex) { applyFilter(filters) {
this.datamanager.filterRows(keyword, colIndex) this.datamanager.filterRows(filters)
.then(({ .then(({
rowsToShow rowsToShow
}) => { }) => {

View File

@ -422,10 +422,8 @@ export default class DataManager {
return column; return column;
} }
filterRows(keyword, colIndex) { filterRows(filters) {
const cells = this.rows.map(row => row[colIndex]); return this.options.filterRows(this.rows, filters)
return this.options.filterRows(keyword, cells, colIndex)
.then(result => { .then(result => {
if (!result) { if (!result) {
result = this.getAllRowIndices(); result = this.getAllRowIndices();

View File

@ -1,14 +1,28 @@
import { isNumber } from './utils'; import { isNumber } from './utils';
export default function filterRows(keyword, cells, colIndex) { export default function filterRows(rows, filters) {
let filter = guessFilter(keyword); let filteredRowIndices = [];
let filterMethod = getFilterMethod(filter);
if (filterMethod) { for (let colIndex in filters) {
return filterMethod(filter.text, cells); const keyword = filters[colIndex];
const filteredRows = filteredRowIndices.length ?
filteredRowIndices.map(i => rows[i]) :
rows;
const cells = filteredRows.map(row => row[colIndex]);
let filter = guessFilter(keyword);
let filterMethod = getFilterMethod(filter);
if (filterMethod) {
filteredRowIndices = filterMethod(filter.text, cells);
} else {
filteredRowIndices = cells.map(cell => cell.rowIndex);
}
} }
return cells.map(cell => cell.rowIndex); return filteredRowIndices;
}; };
function getFilterMethod(filter) { function getFilterMethod(filter) {