From 169f581e9cbd663110a4ddfa34dc0b969f15ac01 Mon Sep 17 00:00:00 2001 From: Aaron George <79060613+octatau@users.noreply.github.com> Date: Fri, 14 Jun 2024 23:39:33 -0400 Subject: [PATCH] Refine datatable column filtering logic --- .../main/lwc/manageRollups/manageRollups.html | 2 +- dlrs/main/lwc/manageRollups/manageRollups.js | 126 ++++++++++-------- 2 files changed, 73 insertions(+), 55 deletions(-) diff --git a/dlrs/main/lwc/manageRollups/manageRollups.html b/dlrs/main/lwc/manageRollups/manageRollups.html index 25064995..20b7c492 100644 --- a/dlrs/main/lwc/manageRollups/manageRollups.html +++ b/dlrs/main/lwc/manageRollups/manageRollups.html @@ -32,7 +32,7 @@ { - let filteredFieldNames = Object.keys(this.filters).filter(fieldName => this.filters[fieldName].value !== 'All'); - - return filteredFieldNames.every(fieldName => { - switch (this.filters[fieldName].type) { - case 'boolean': - return rollup[fieldName] === (this.filters[fieldName].value === 'checked' ? true : false) - - default: - return rollup[fieldName] === this.filters[fieldName].value - } - }) - }); - } - async refreshRollups() { this.isLoading = true; this.rollups = await getAllRollupConfigs(); @@ -182,19 +166,7 @@ export default class ManageRollups extends NavigationMixin(LightningElement) { calcRollupList() { this.rollupList = Object.values(this.rollups).filter((r) => { - if (this.searchFilter.trim().length === 0) { - return true; - } - for (const c of this.dtColumns) { - if ( - r[c.fieldName] && - ("" + r[c.fieldName]).toLowerCase().indexOf(this.searchFilter) >= 0 - ) { - return true; - } - } - // didn't match any of the displayed fields - return false; + return this.meetsSearchFilter(r) && this.meetsColumnFilters(r); }); this.rollupList.sort((a, b) => { const dirModifier = this.sortDirection === "asc" ? 1 : -1; @@ -221,6 +193,34 @@ export default class ManageRollups extends NavigationMixin(LightningElement) { }); } + meetsSearchFilter(rollup) { + if (this.searchFilter.trim().length === 0) { + return true; + } + for (const c of this.dtColumns) { + if ( + rollup[c.fieldName] && + ("" + rollup[c.fieldName]).toLowerCase().indexOf(this.searchFilter) >= 0 + ) { + return true; + } + } + // didn't match any of the displayed fields + return false; + } + + meetsColumnFilters(rollup) { + return Object.keys(this.filters).every(fieldName => { + switch (this.filters[fieldName].type) { + case 'boolean': + return rollup[fieldName] === (this.filters[fieldName].value === 'checked' ? true : false) + + default: + return rollup[fieldName] === this.filters[fieldName].value + } + }) + } + rollupSelectHandler(event) { const action = event.detail.action; const row = event.detail.row; @@ -323,15 +323,33 @@ export default class ManageRollups extends NavigationMixin(LightningElement) { } handleHeaderAction(event) { - let filters = { - ...this.filters, - [event.detail.columnDefinition.fieldName]: { - type: event.detail.columnDefinition.type, - value: event.detail.action.name + const filteredFieldName = event.detail.columnDefinition.fieldName; + const columnRef = [...this.dtColumns]; + const currentColumn = columnRef.find(f => f.fieldName === filteredFieldName); + const previousAction = currentColumn.actions.find(action => action.checked); + const currentAction = currentColumn.actions.find(action => action.name === event.detail.action.name); + + if (event.detail.action.type === 'filter') { + if (event.detail.action.name === 'All') { + delete this.filters[filteredFieldName]; + delete currentColumn.iconName; + } else { + this.filters = { + ...this.filters, + [filteredFieldName]: { + type: event.detail.columnDefinition.type, + value: event.detail.action.name + } + } + currentColumn.iconName = 'utility:filterList'; } + + this.calcRollupList(); } - this.filters = filters; + previousAction.checked = false; + currentAction.checked = true; + this.dtColumns = columnRef; } disconnectedCallback() {