From 41af992419e66ed28eefb788ba3ba0770a0d3117 Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 12 Dec 2020 02:13:21 +0100 Subject: [PATCH] Add List FIlter --- src/app/components/table/table.component.ts | 1 + .../filter-row/filter-row.component.html | 13 ++ .../customTableFilterFunction.ts | 221 ++++++++++-------- 3 files changed, 135 insertions(+), 100 deletions(-) diff --git a/src/app/components/table/table.component.ts b/src/app/components/table/table.component.ts index f861c01..8bdfd16 100644 --- a/src/app/components/table/table.component.ts +++ b/src/app/components/table/table.component.ts @@ -442,6 +442,7 @@ export class TableComponent implements AfterViewInit { fromValue: {}, toValue: {}, type: column.type, + list: column.list, options: {}, }; } 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 d71cd37..006f22d 100644 --- a/src/app/components/tableComponents/filter-row/filter-row.component.html +++ b/src/app/components/tableComponents/filter-row/filter-row.component.html @@ -1,3 +1,15 @@ + + + + + + \ No newline at end of file diff --git a/src/app/helperFunctions/customTableFilterFunction.ts b/src/app/helperFunctions/customTableFilterFunction.ts index 5053515..8c53504 100644 --- a/src/app/helperFunctions/customTableFilterFunction.ts +++ b/src/app/helperFunctions/customTableFilterFunction.ts @@ -7,153 +7,174 @@ export function customTableFilterFunction(data: any, filter: any) { } for (const filterElementName of Object.keys(filter.columnFilters)) { const filterElement = filter.columnFilters[filterElementName]; - // String Filter - if (filterElement.value) { - if (filterElement.type === 'String' || filterElement.type === 'Id') { - let searchString = filterElement.value.trim(); - let dataElement = data[filterElementName]?.trim(); - if (dataElement == null) { + // List Filter - ignore types if column is list + if (filterElement.list && filterElement.values.length > 0) { + let dataElement: Array = data[filterElementName]; + if (dataElement.length !== filterElement.values.length) { + return false; + } + for (const element of filterElement.values) { + if (!dataElement.includes(element)) { return false; } - if (!filterElement.options.caseSensitive) { - searchString = searchString.toLowerCase(); - dataElement = dataElement.toLowerCase(); + } + } else { + // String Filter + if (filterElement.value) { + if (filterElement.type === 'String' || filterElement.type === 'Id') { + let searchString = filterElement.value.trim(); + let dataElement = data[filterElementName]?.trim(); + if (dataElement == null) { + return false; + } + if (!filterElement.options.caseSensitive) { + searchString = searchString.toLowerCase(); + dataElement = dataElement.toLowerCase(); + } + if ( + (filterElement.options.exact && dataElement !== searchString) || + !dataElement.includes(searchString) + ) { + return false; + } } + } + // Number Filter + if (filterElement.min != null || filterElement.max != null) { if ( - (filterElement.options.exact && dataElement !== searchString) || - !dataElement.includes(searchString) + filterElement.type === 'Float' || + filterElement.type === 'Int' || + filterElement.type === 'Money' ) { - return false; + let dataElement = data[filterElementName]; + if (dataElement == null) { + return false; + } + if (filterElement.min != null && dataElement < filterElement.min) { + return false; + } + if (filterElement.max != null && dataElement > filterElement.max) { + return false; + } } } - } - // Number Filter - if (filterElement.min != null || filterElement.max != null) { + // NumberRange Filter + if (filterElement.type === 'NumRange') { + if ( + filterElement.minValue.min != null || + filterElement.minValue.max != null || + filterElement.maxValue.min != null || + filterElement.maxValue.max != null + ) { + let dataElementMin = data[filterElementName + '.min']; + let dataElementMax = data[filterElementName + '.max']; + if (dataElementMin == null && dataElementMax == null) { + 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.maxValue.min != null && + dataElementMax < filterElement.maxValue.min + ) { + return false; + } + if ( + filterElement.maxValue.max != null && + dataElementMax > filterElement.maxValue.max + ) { + return false; + } + } + } + // Date Filter if ( - filterElement.type === 'Float' || - filterElement.type === 'Int' || - filterElement.type === 'Money' + filterElement.type === 'Date' && + (filterElement.from != null || filterElement.to != null) ) { let dataElement = data[filterElementName]; if (dataElement == null) { return false; } - if (filterElement.min != null && dataElement < filterElement.min) { + if ( + filterElement.from != null && + new Date(dataElement) < new Date(filterElement.from) + ) { return false; } - if (filterElement.max != null && dataElement > filterElement.max) { + if ( + filterElement.to != null && + new Date(dataElement) > new Date(filterElement.to) + ) { return false; } } - } - // NumberRange Filter - if (filterElement.type === 'NumRange') { + // DateRange Filter if ( - filterElement.minValue.min != null || - filterElement.minValue.max != null || - filterElement.maxValue.min != null || - filterElement.maxValue.max != null + filterElement.type === 'DateRange' && + (filterElement.fromValue.from != null || + filterElement.fromValue.to != null || + filterElement.toValue.from != null || + filterElement.toValue.to != null) ) { - let dataElementMin = data[filterElementName + '.min']; - let dataElementMax = data[filterElementName + '.max']; - if (dataElementMin == null && dataElementMax == null) { + let dataElementFrom = data[filterElementName + '.from']; + let dataElementTo = data[filterElementName + '.to']; + if (!dataElementFrom && !dataElementTo) { return false; } if ( - filterElement.minValue.min != null && - dataElementMin < filterElement.minValue.min + filterElement.fromValue.from != null && + (!dataElementFrom || + new Date(dataElementFrom) < new Date(filterElement.fromValue.from)) ) { return false; } if ( - filterElement.minValue.max != null && - dataElementMin > filterElement.minValue.max + filterElement.fromValue.to != null && + new Date(dataElementFrom) > new Date(filterElement.fromValue.to) ) { return false; } if ( - filterElement.maxValue.min != null && - dataElementMax < filterElement.maxValue.min + filterElement.toValue.from != null && + new Date(dataElementTo) < new Date(filterElement.toValue.from) ) { return false; } if ( - filterElement.maxValue.max != null && - dataElementMax > filterElement.maxValue.max + filterElement.toValue.to != null && + (!dataElementTo || + new Date(dataElementTo) > new Date(filterElement.toValue.to)) ) { return false; } } - } - // Date Filter - 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; - } + // Enum Filter if ( - filterElement.to != null && - new Date(dataElement) > new Date(filterElement.to) + filterElement.type.startsWith('Enum') && + filterElement.value != null && + filterElement.value !== data[filterElementName] ) { 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; - } + // Boolean Filter if ( - filterElement.fromValue.from != null && - (!dataElementFrom || - new Date(dataElementFrom) < new Date(filterElement.fromValue.from)) + filterElement.type === 'Boolean' && + ((filterElement.value === 'Ja' && !data[filterElementName]) || + (filterElement.value === 'Nein' && data[filterElementName])) ) { 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; - } - } - // Enum Filter - if (filterElement.type.startsWith('Enum') && filterElement.value != null && filterElement.value !== data[filterElementName]) { - return false; - } - // Boolean Filter - if (filterElement.type === "Boolean" && ((filterElement.value === "Ja" && !data[filterElementName])||(filterElement.value === "Nein" && data[filterElementName]))) { - return false; } } /*const b =