From 1ee312b070012ede656fa8ae7738488356c99fa2 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 12 Dec 2020 01:32:56 +0100 Subject: [PATCH] Add dateRange filter --- src/app/components/table/table.component.ts | 2 + .../date-range-cell.component.html | 4 +- .../date-range-cell.component.ts | 6 + .../filter-row/filter-row.component.html | 24 ++++ .../customTableFilterFunction.ts | 103 ++++++++++++++---- 5 files changed, 116 insertions(+), 23 deletions(-) diff --git a/src/app/components/table/table.component.ts b/src/app/components/table/table.component.ts index f3ea972..f861c01 100644 --- a/src/app/components/table/table.component.ts +++ b/src/app/components/table/table.component.ts @@ -439,6 +439,8 @@ export class TableComponent implements AfterViewInit { value: null, minValue: {}, maxValue: {}, + fromValue: {}, + toValue: {}, type: column.type, options: {}, }; diff --git a/src/app/components/tableComponents/date-range-cell/date-range-cell.component.html b/src/app/components/tableComponents/date-range-cell/date-range-cell.component.html index d43384c..e4781dc 100644 --- a/src/app/components/tableComponents/date-range-cell/date-range-cell.component.html +++ b/src/app/components/tableComponents/date-range-cell/date-range-cell.component.html @@ -7,7 +7,7 @@ (); isValid = true; diff --git a/src/app/components/tableComponents/filter-row/filter-row.component.html b/src/app/components/tableComponents/filter-row/filter-row.component.html index 3ae1ead..6c6c45a 100644 --- a/src/app/components/tableComponents/filter-row/filter-row.component.html +++ b/src/app/components/tableComponents/filter-row/filter-row.component.html @@ -90,3 +90,27 @@ (toChange)="newFilterValue()" > + + + + + + + + diff --git a/src/app/helperFunctions/customTableFilterFunction.ts b/src/app/helperFunctions/customTableFilterFunction.ts index 66b8f7d..7d0b440 100644 --- a/src/app/helperFunctions/customTableFilterFunction.ts +++ b/src/app/helperFunctions/customTableFilterFunction.ts @@ -13,7 +13,7 @@ export function customTableFilterFunction(data: any, filter: any) { let searchString = filterElement.value.trim(); let dataElement = data[filterElementName]?.trim(); if (dataElement == null) { - return false; + return false; } if (!filterElement.options.caseSensitive) { searchString = searchString.toLowerCase(); @@ -57,34 +57,95 @@ export function customTableFilterFunction(data: any, filter: any) { let dataElementMin = data[filterElementName + '.min']; let dataElementMax = data[filterElementName + '.max']; if (dataElementMin == null && dataElementMax == null) { - return false; + return false; } - if (filterElement.minValue.min != null && dataElementMin < filterElement.minValue.min) { - return false; + if ( + filterElement.minValue.min != null && + dataElementMin < filterElement.minValue.min + ) { + return false; } - if (filterElement.minValue.max != null && dataElementMin > filterElement.minValue.max) { - return false; + if ( + filterElement.minValue.max != null && + dataElementMin > filterElement.minValue.max + ) { + return false; } - if (filterElement.maxValue.min != null && dataElementMax < filterElement.maxValue.min) { - return false; + if ( + filterElement.maxValue.min != null && + dataElementMax < filterElement.maxValue.min + ) { + return false; } - if (filterElement.maxValue.max != null && dataElementMax > filterElement.maxValue.max) { - return false; + if ( + filterElement.maxValue.max != null && + dataElementMax > filterElement.maxValue.max + ) { + return false; } } } // Date Filter - if (filterElement.from != null || filterElement.to != null) { - let dataElement = data[filterElementName]; - if (dataElement == null) { - return false; - } - if (filterElement.from != null && new Date(dataElement) < new Date(filterElement.from)) { - return false; - } - if (filterElement.to != null && new Date(dataElement) > new Date(filterElement.to)) { - return false; - } + if ( + filterElement.type === 'Date' && + (filterElement.from != null || filterElement.to != null) + ) { + let dataElement = data[filterElementName]; + if (dataElement == null) { + return false; + } + if ( + filterElement.from != null && + new Date(dataElement) < new Date(filterElement.from) + ) { + return false; + } + if ( + filterElement.to != null && + new Date(dataElement) > new Date(filterElement.to) + ) { + return false; + } + } + // DateRange Filter + if ( + filterElement.type === 'DateRange' && + (filterElement.fromValue.from != null || + filterElement.fromValue.to != null || + filterElement.toValue.from != null || + filterElement.toValue.to != null) + ) { + let dataElementFrom = data[filterElementName + '.from']; + let dataElementTo = data[filterElementName + '.to']; + if (!dataElementFrom && !dataElementTo) { + return false; + } + if ( + filterElement.fromValue.from != null && + (!dataElementFrom || + new Date(dataElementFrom) < new Date(filterElement.fromValue.from)) + ) { + return false; + } + if ( + filterElement.fromValue.to != null && + new Date(dataElementFrom) > new Date(filterElement.fromValue.to) + ) { + return false; + } + if ( + filterElement.toValue.from != null && + new Date(dataElementTo) < new Date(filterElement.toValue.from) + ) { + return false; + } + if ( + filterElement.toValue.to != null && + (!dataElementTo || + new Date(dataElementTo) > new Date(filterElement.toValue.to)) + ) { + return false; + } } } /*const b =