Skip to content

Commit

Permalink
fix: 🐛 Comparison now also works with strings
Browse files Browse the repository at this point in the history
Inline filters like >2011/04/01 will work as expected. Works with >, <
and :
  • Loading branch information
netchampfaris committed Dec 28, 2018
1 parent 26a37b5 commit acfe0e4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
18 changes: 12 additions & 6 deletions src/columnmanager.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,8 @@ export default class ColumnManager {

bindFilter() {
if (!this.options.inlineFilters) return;
this.appliedFilters = this.appliedFilters || {};
const handler = e => {
this.$filterCell = $.closest('.dt-cell', e.target);
const { colIndex } = $.data(this.$filterCell);
const keyword = e.target.value;
this.appliedFilters[colIndex] = keyword;
this.applyFilter(this.appliedFilters);
this.applyFilter(this.getAppliedFilters());
};
$.on(this.header, 'keydown', '.dt-filter', debounce(handler, 300));
}
Expand All @@ -290,6 +285,17 @@ export default class ColumnManager {
});
}

getAppliedFilters() {
const filters = {};
$.each('.dt-filter', this.header).map((input) => {
const value = input.value;
if (value) {
filters[input.dataset.colIndex] = value;
}
});
return filters;
}

applyDefaultSortOrder() {
// sort rows if any 1 column has a default sortOrder set
const columnsToSort = this.getColumns().filter(col => col.sortOrder !== 'none');
Expand Down
47 changes: 26 additions & 21 deletions src/filterRows.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ import { isNumber, stripHTML } from './utils';
export default function filterRows(rows, filters) {
let filteredRowIndices = [];

if (Object.keys(filters).length === 0) {
return rows.map(row => row.meta.rowIndex);
}

for (let colIndex in filters) {
const keyword = filters[colIndex];

Expand Down Expand Up @@ -40,7 +44,7 @@ function getFilterMethod(filter) {
greaterThan(keyword, cells) {
return cells
.filter(cell => {
const value = parseFloat(cell.content);
const value = cell.content;
return value > keyword;
})
.map(cell => cell.rowIndex);
Expand All @@ -49,7 +53,7 @@ function getFilterMethod(filter) {
lessThan(keyword, cells) {
return cells
.filter(cell => {
const value = parseFloat(cell.content);
const value = cell.content;
return value < keyword;
})
.map(cell => cell.rowIndex);
Expand All @@ -67,7 +71,7 @@ function getFilterMethod(filter) {
range(rangeValues, cells) {
return cells
.filter(cell => {
const value = parseFloat(cell.content);
const value = cell.content;
return value >= rangeValues[0] && value <= rangeValues[1];
})
.map(cell => cell.rowIndex);
Expand All @@ -80,48 +84,49 @@ function getFilterMethod(filter) {
function guessFilter(keyword = '') {
if (keyword.length === 0) return {};

let compareString = keyword;

if (['>', '<', '='].includes(compareString[0])) {
compareString = keyword.slice(1);
}

if (keyword.startsWith('>')) {
if (isNumber(keyword.slice(1))) {
if (compareString) {
return {
type: 'greaterThan',
text: Number(keyword.slice(1).trim())
text: compareString.trim()
};
}

keyword = keyword.slice(1);
}

if (keyword.startsWith('<')) {
if (isNumber(keyword.slice(1))) {
if (compareString) {
return {
type: 'lessThan',
text: Number(keyword.slice(1).trim())
text: compareString.trim()
};
}

keyword = keyword.slice(1);
}

if (keyword.split(':').length === 2 && keyword.split(':').every(isNumber)) {
return {
type: 'range',
text: keyword.split(':').map(v => v.trim()).map(Number)
};
}

if (keyword.startsWith('=')) {
if (isNumber(keyword.slice(1))) {
if (isNumber(compareString)) {
return {
type: 'equals',
text: Number(keyword.slice(1).trim())
};
}
}

keyword = keyword.slice(1);
if (keyword.split(':').length === 2) {
compareString = keyword.split(':');
return {
type: 'range',
text: compareString.map(v => v.trim())
};
}

return {
type: 'contains',
text: keyword.toLowerCase()
text: compareString.toLowerCase()
};
}

0 comments on commit acfe0e4

Please sign in to comment.