From 87ae46e3490f5a23d58222b896b199e50b516c21 Mon Sep 17 00:00:00 2001 From: igdmdimitrov Date: Fri, 25 Mar 2022 15:43:26 +0200 Subject: [PATCH] fix(grid): fixed dateTime filtering --- .../data-operations/filtering-condition.ts | 172 +++++++++++++++++- .../src/lib/grids/grid/grid-filtering.spec.ts | 5 +- 2 files changed, 170 insertions(+), 7 deletions(-) diff --git a/projects/igniteui-angular/src/lib/data-operations/filtering-condition.ts b/projects/igniteui-angular/src/lib/data-operations/filtering-condition.ts index fc9519655b3..ef593b5cd30 100644 --- a/projects/igniteui-angular/src/lib/data-operations/filtering-condition.ts +++ b/projects/igniteui-angular/src/lib/data-operations/filtering-condition.ts @@ -389,13 +389,9 @@ export class IgxDateFilteringOperand extends IgxBaseDateTimeFilteringOperand { } } -export class IgxDateTimeFilteringOperand extends IgxDateFilteringOperand { +export class IgxDateTimeFilteringOperand extends IgxBaseDateTimeFilteringOperand { protected constructor() { super(); - let index = this.operations.indexOf(this.condition('equals')); - this.operations.splice(index, 1); - index = this.operations.indexOf(this.condition('doesNotEqual')); - this.operations.splice(index, 1); this.operations = [{ name: 'equals', isUnary: false, @@ -432,6 +428,172 @@ export class IgxDateTimeFilteringOperand extends IgxDateFilteringOperand { targetp.minutes !== searchp.minutes || targetp.seconds !== searchp.seconds; } + }, { + name: 'before', + isUnary: false, + iconName: 'is-before', + logic: (target: Date, searchVal: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + return target < searchVal; + } + }, { + name: 'after', + isUnary: false, + iconName: 'is-after', + logic: (target: Date, searchVal: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + return target > searchVal; + } + }, { + name: 'today', + isUnary: true, + iconName: 'today', + logic: (target: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + const d = IgxDateTimeFilteringOperand.getDateParts(target, 'yMd'); + const now = IgxDateTimeFilteringOperand.getDateParts(new Date(), 'yMd'); + return d.year === now.year && + d.month === now.month && + d.day === now.day; + } + }, { + name: 'yesterday', + isUnary: true, + iconName: 'yesterday', + logic: (target: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + const td = IgxDateTimeFilteringOperand.getDateParts(target, 'yMd'); + const y = ((d) => new Date(d.setDate(d.getDate() - 1)))(new Date()); + const yesterday = IgxDateTimeFilteringOperand.getDateParts(y, 'yMd'); + return td.year === yesterday.year && + td.month === yesterday.month && + td.day === yesterday.day; + } + }, { + name: 'thisMonth', + isUnary: true, + iconName: 'this-month', + logic: (target: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + const d = IgxDateTimeFilteringOperand.getDateParts(target, 'yM'); + const now = IgxDateTimeFilteringOperand.getDateParts(new Date(), 'yM'); + return d.year === now.year && + d.month === now.month; + } + }, { + name: 'lastMonth', + isUnary: true, + iconName: 'last-month', + logic: (target: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + const d = IgxDateTimeFilteringOperand.getDateParts(target, 'yM'); + const now = IgxDateTimeFilteringOperand.getDateParts(new Date(), 'yM'); + if (!now.month) { + now.month = 11; + now.year -= 1; + } else { + now.month--; + } + return d.year === now.year && + d.month === now.month; + } + }, { + name: 'nextMonth', + isUnary: true, + iconName: 'next-month', + logic: (target: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + const d = IgxDateTimeFilteringOperand.getDateParts(target, 'yM'); + const now = IgxDateTimeFilteringOperand.getDateParts(new Date(), 'yM'); + if (now.month === 11) { + now.month = 0; + now.year += 1; + } else { + now.month++; + } + return d.year === now.year && + d.month === now.month; + } + }, { + name: 'thisYear', + isUnary: true, + iconName: 'this-year', + logic: (target: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + const d = IgxDateTimeFilteringOperand.getDateParts(target, 'y'); + const now = IgxDateTimeFilteringOperand.getDateParts(new Date(), 'y'); + return d.year === now.year; + } + }, { + name: 'lastYear', + isUnary: true, + iconName: 'last-year', + logic: (target: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + const d = IgxDateTimeFilteringOperand.getDateParts(target, 'y'); + const now = IgxDateTimeFilteringOperand.getDateParts(new Date(), 'y'); + return d.year === now.year - 1; + } + }, { + name: 'nextYear', + isUnary: true, + iconName: 'next-year', + logic: (target: Date) => { + if (!target) { + return false; + } + + this.validateInputData(target); + + const d = IgxDateTimeFilteringOperand.getDateParts(target, 'y'); + const now = IgxDateTimeFilteringOperand.getDateParts(new Date(), 'y'); + return d.year === now.year + 1; + } }].concat(this.operations); } } diff --git a/projects/igniteui-angular/src/lib/grids/grid/grid-filtering.spec.ts b/projects/igniteui-angular/src/lib/grids/grid/grid-filtering.spec.ts index fd908dba50e..76373329f4e 100644 --- a/projects/igniteui-angular/src/lib/grids/grid/grid-filtering.spec.ts +++ b/projects/igniteui-angular/src/lib/grids/grid/grid-filtering.spec.ts @@ -456,13 +456,13 @@ describe('IgxGrid - Filtering actions #grid', () => { const today = SampleTestData.todayFullDate; // Equals 11:15:35 - grid.filter('ReleaseDate', cal.timedelta(today, 'hour', 1), + grid.filter('ReleaseDateTime', cal.timedelta(today, 'hour', 1), IgxDateTimeFilteringOperand.instance().condition('equals')); fix.detectChanges(); expect(grid.rowList.length).toEqual(1); // Does not equal 11:15:35 - grid.filter('ReleaseDate', cal.timedelta(today, 'hour', 1), + grid.filter('ReleaseDateTime', cal.timedelta(today, 'hour', 1), IgxDateTimeFilteringOperand.instance().condition('doesNotEqual')); fix.detectChanges(); expect(grid.rowList.length).toEqual(7); @@ -513,6 +513,7 @@ describe('IgxGrid - Filtering actions #grid', () => { fix.detectChanges(); expect(grid.rowList.length).toEqual(1); })); + it('should correctly filter by \'date\' filtering conditions when dates are ISO 8601 strings', fakeAsync(() => { const cal = SampleTestData.timeGenerator; const today = SampleTestData.today;