Skip to content

Commit

Permalink
Add date flag to filters to be allow columsn to be filtered by dates.
Browse files Browse the repository at this point in the history
  • Loading branch information
amyboyd committed Feb 13, 2015
1 parent 98cb0f0 commit 0259aab
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/js/core/services/rowSearcher.js
Expand Up @@ -134,7 +134,7 @@ module.service('rowSearcher', ['gridUtil', 'uiGridConstants', function (gridUtil
newFilter.condition = rowSearcher.guessCondition(filter);
}

newFilter.flags = angular.extend( { caseSensitive: false }, filter.flags );
newFilter.flags = angular.extend( { caseSensitive: false, date: false }, filter.flags );

if (newFilter.condition === uiGridConstants.filter.STARTS_WITH) {
newFilter.startswithRE = new RegExp('^' + newFilter.term, regexpFlags);
Expand Down Expand Up @@ -221,6 +221,12 @@ module.service('rowSearcher', ['gridUtil', 'uiGridConstants', function (gridUtil
}
}

if (filter.flags.date === true) {
value = new Date(value);
// If the term has a dash in it, it comes through as '\-' -- we need to take out the '\'.
term = new Date(term.replace(/\\/g, ''));
}

if (filter.condition === uiGridConstants.filter.GREATER_THAN) {
return (value > term);
}
Expand Down
40 changes: 40 additions & 0 deletions test/unit/core/services/rowSearcher.spec.js
@@ -0,0 +1,40 @@
describe('rowSearcher', function() {
var rowSearcher,
uiGridConstants;

beforeEach(module('ui.grid'));

beforeEach(inject(function(_rowSearcher_, _uiGridConstants_) {
rowSearcher = _rowSearcher_;
uiGridConstants = _uiGridConstants_;
}));

describe('runColumnFilter', function() {
it('should be able to compare dates', function() {
var grid = {
getCellValue: function() {
return '2015-05-23';
}
};

var filter = {
term: '2015-05-24',
flags: { date: true },
condition: uiGridConstants.filter.GREATER_THAN
};
expect(rowSearcher.runColumnFilter(grid, 1, 2, filter)).toBe(false);

filter.term = '2015-05-22';
filter.condition = uiGridConstants.filter.GREATER_THAN;
expect(rowSearcher.runColumnFilter(grid, 1, 2, filter)).toBe(true);

filter.term = '2015-05-24';
filter.condition = uiGridConstants.filter.GREATER_THAN;
expect(rowSearcher.runColumnFilter(grid, 1, 2, filter)).toBe(false);

filter.term = '2015-05-23';
filter.condition = uiGridConstants.filter.GREATER_THAN_OR_EQUAL_TO;
expect(rowSearcher.runColumnFilter(grid, 1, 2, filter)).toBe(true);
});
});
});

0 comments on commit 0259aab

Please sign in to comment.