From 4f5fc443fbc7a0ab3cbe46722fc6bd85fd4b1594 Mon Sep 17 00:00:00 2001 From: ghiscoding Date: Thu, 23 Sep 2021 17:37:34 -0400 Subject: [PATCH] fix(tree): when Tree Data is filtered then Sort, footer count is invalid - use case, if we have filtered data in the grid (any filter is applied and some items are filtered) and we then Sort a column, the item count shown in the Footer was showing the entire dataset count instead of the filtered count. This happens because internally the lib will Sort the hierarchical dataset and then overwrite the DataView flat dataset and when it does that it was changing the footer count. --- packages/common/src/services/__tests__/sort.service.spec.ts | 6 +++++- packages/common/src/services/sort.service.ts | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/common/src/services/__tests__/sort.service.spec.ts b/packages/common/src/services/__tests__/sort.service.spec.ts index 196e468d4..b40993f09 100644 --- a/packages/common/src/services/__tests__/sort.service.spec.ts +++ b/packages/common/src/services/__tests__/sort.service.spec.ts @@ -47,11 +47,15 @@ const gridOptionMock = { } as unknown as GridOption; const dataViewStub = { + getFilteredItemCount: jest.fn(), + getItemCount: jest.fn(), getItemMetadata: jest.fn(), + getLength: jest.fn(), refresh: jest.fn(), - sort: jest.fn(), reSort: jest.fn(), + sort: jest.fn(), setItems: jest.fn(), + onRowCountChanged: new Slick.Event(), } as unknown as SlickDataView; const backendServiceStub = { diff --git a/packages/common/src/services/sort.service.ts b/packages/common/src/services/sort.service.ts index f2bbd174b..4811d1c3e 100644 --- a/packages/common/src/services/sort.service.ts +++ b/packages/common/src/services/sort.service.ts @@ -412,6 +412,11 @@ export class SortService { // we could use the DataView sort but that would require re-sorting again (since the 2nd array that is currently in the DataView would have to be resorted against the 1st array that was sorting from tree sort) // it is simply much faster to just replace the entire dataset this._dataView.setItems(datasetSortResult.flat, datasetIdPropertyName); + + // also trigger a row count changed to avoid having an invalid filtered item count in the grid footer + // basically without this the item count in the footer is incorrect and shows the full dataset length instead of the previous filtered count + // that happens because we just overwrote the entire dataset the DataView.refresh() doesn't detect a row count change so we trigger it manually + this._dataView.onRowCountChanged.notify({ previous: this._dataView.getFilteredItemCount(), current: this._dataView.getLength(), itemCount: this._dataView.getItemCount(), dataView: this._dataView, callingOnRowsChanged: true }); } else { dataView.sort(this.sortComparers.bind(this, sortColumns)); }