Skip to content
This repository was archived by the owner on Jun 1, 2025. It is now read-only.

Commit 17d94a2

Browse files
Ghislain BeaulacGhislain Beaulac
authored andcommitted
fix(backend): Clear Filter was not working correctly
- also remove any default sort on GraphQL Service - cleanup code and add new emitterType enum
1 parent 45aae30 commit 17d94a2

File tree

6 files changed

+52
-42
lines changed

6 files changed

+52
-42
lines changed

src/app/modules/angular-slickgrid/extensions/headerMenuExtension.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export class HeaderMenuExtension implements Extension {
172172
}
173173

174174
/** Execute the Header Menu Commands that was triggered by the onCommand subscribe */
175-
executeHeaderMenuInternalCommands(e: Event, args: HeaderMenuOnCommandArgs) {
175+
executeHeaderMenuInternalCommands(event: Event, args: HeaderMenuOnCommandArgs) {
176176
if (args && args.command) {
177177
switch (args.command) {
178178
case 'hide':
@@ -182,15 +182,15 @@ export class HeaderMenuExtension implements Extension {
182182
}
183183
break;
184184
case 'clear-filter':
185-
this.clearColumnFilter(e, args);
185+
this.clearColumnFilter(event, args);
186186
break;
187187
case 'clear-sort':
188-
this.clearColumnSort(e, args);
188+
this.clearColumnSort(event, args);
189189
break;
190190
case 'sort-asc':
191191
case 'sort-desc':
192192
const isSortingAsc = (args.command === 'sort-asc');
193-
this.sortColumn(e, args, isSortingAsc);
193+
this.sortColumn(event, args, isSortingAsc);
194194
break;
195195
default:
196196
break;
@@ -268,15 +268,15 @@ export class HeaderMenuExtension implements Extension {
268268
}
269269

270270
/** Sort the current column */
271-
private sortColumn(e: Event, args: HeaderMenuOnCommandArgs, isSortingAsc = true) {
271+
private sortColumn(event: Event, args: HeaderMenuOnCommandArgs, isSortingAsc = true) {
272272
if (args && args.column) {
273273
// get previously sorted columns
274274
const sortedColsWithoutCurrent: ColumnSort[] = this.sortService.getPreviousColumnSorts(args.column.id + '');
275275

276276
// add to the column array, the column sorted by the header menu
277277
sortedColsWithoutCurrent.push({ sortCol: args.column, sortAsc: isSortingAsc });
278278
if (this.sharedService.gridOptions.backendServiceApi) {
279-
this.sortService.onBackendSortChanged(e, { multiColumnSort: true, sortCols: sortedColsWithoutCurrent, grid: this.sharedService.grid });
279+
this.sortService.onBackendSortChanged(event, { multiColumnSort: true, sortCols: sortedColsWithoutCurrent, grid: this.sharedService.grid });
280280
} else if (this.sharedService.dataView) {
281281
this.sortService.onLocalSortChanged(this.sharedService.grid, this.sharedService.dataView, sortedColsWithoutCurrent);
282282
} else {
@@ -298,22 +298,22 @@ export class HeaderMenuExtension implements Extension {
298298
}
299299

300300
/** Clear the Filter on the current column (if it's actually filtered) */
301-
private clearColumnFilter(e: Event, args: HeaderMenuOnCommandArgs) {
301+
private clearColumnFilter(event: Event, args: HeaderMenuOnCommandArgs) {
302302
if (args && args.column) {
303-
this.filterService.clearFilterByColumnId(args.column.id);
303+
this.filterService.clearFilterByColumnId(event, args.column.id);
304304
}
305305
}
306306

307307
/** Clear the Sort on the current column (if it's actually sorted) */
308-
private clearColumnSort(e: Event, args: HeaderMenuOnCommandArgs) {
308+
private clearColumnSort(event: Event, args: HeaderMenuOnCommandArgs) {
309309
if (args && args.column && this.sharedService) {
310310
// get previously sorted columns
311311
const allSortedCols: ColumnSort[] = this.sortService.getPreviousColumnSorts();
312312
const sortedColsWithoutCurrent: ColumnSort[] = this.sortService.getPreviousColumnSorts(args.column.id + '');
313313

314314
if (allSortedCols.length !== sortedColsWithoutCurrent.length) {
315315
if (this.sharedService.gridOptions && this.sharedService.gridOptions.backendServiceApi) {
316-
this.sortService.onBackendSortChanged(e, { multiColumnSort: true, sortCols: sortedColsWithoutCurrent, grid: this.sharedService.grid });
316+
this.sortService.onBackendSortChanged(event, { multiColumnSort: true, sortCols: sortedColsWithoutCurrent, grid: this.sharedService.grid });
317317
} else if (this.sharedService.dataView) {
318318
this.sortService.onLocalSortChanged(this.sharedService.grid, this.sharedService.dataView, sortedColsWithoutCurrent, true);
319319
} else {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum EmitterType {
2+
local = 'local',
3+
remote = 'remote'
4+
}
5+

src/app/modules/angular-slickgrid/models/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export * from './editorArgs.interface';
3131
export * from './editorValidator.interface';
3232
export * from './editorValidatorOutput.interface';
3333
export * from './elementPosition.interface';
34+
export * from './emitterType.enum';
3435
export * from './exportOption.interface';
3536
export * from './extension.interface';
3637
export * from './extensionModel.interface';

src/app/modules/angular-slickgrid/services/filter.service.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Column,
55
ColumnFilter,
66
ColumnFilters,
7+
EmitterType,
78
Filter,
89
FilterArguments,
910
FilterCallbackArg,
@@ -131,7 +132,7 @@ export class FilterService {
131132

132133
// emit an onFilterChanged event when it's not called by a clear filter
133134
if (args && !args.clearFilterTriggered) {
134-
this.emitFilterChanged('remote');
135+
this.emitFilterChanged(EmitterType.remote);
135136
}
136137

137138
// the processes can be Observables (like HttpClient) or Promises
@@ -166,7 +167,7 @@ export class FilterService {
166167
}
167168
// emit an onFilterChanged event when it's not called by a clear filter
168169
if (args && !args.clearFilterTriggered) {
169-
this.emitFilterChanged('local');
170+
this.emitFilterChanged(EmitterType.local);
170171
}
171172
});
172173

@@ -176,7 +177,7 @@ export class FilterService {
176177
});
177178
}
178179

179-
clearFilterByColumnId(columnId: number | string) {
180+
clearFilterByColumnId(event: Event, columnId: number | string) {
180181
const colFilter: Filter = this._filters.find((filter: Filter) => filter.columnDef.id === columnId);
181182
if (colFilter && colFilter.clear) {
182183
colFilter.clear();
@@ -190,9 +191,17 @@ export class FilterService {
190191
}
191192
}
192193

194+
let emitter: EmitterType = EmitterType.local;
195+
const isBackendApi = this._gridOptions && this._gridOptions.backendServiceApi || false;
196+
197+
// when using a backend service, we need to manually trigger a filter change
198+
if (isBackendApi) {
199+
emitter = EmitterType.remote;
200+
this.onBackendFilterChange(event as KeyboardEvent, { grid: this._grid, columnFilters: this._columnFilters });
201+
}
202+
193203
// emit an event when filter is cleared
194-
const sender = this._gridOptions && this._gridOptions.backendServiceApi ? 'remote' : 'local';
195-
this.emitFilterChanged(sender);
204+
this.emitFilterChanged(emitter);
196205
}
197206

198207
/** Clear the search filters (below the column titles) */
@@ -486,15 +495,15 @@ export class FilterService {
486495
* Other services, like Pagination, can then subscribe to it.
487496
* @param caller
488497
*/
489-
emitFilterChanged(caller: 'local' | 'remote') {
490-
if (caller === 'remote' && this._gridOptions && this._gridOptions.backendServiceApi) {
498+
emitFilterChanged(caller: EmitterType) {
499+
if (caller === EmitterType.remote && this._gridOptions && this._gridOptions.backendServiceApi) {
491500
let currentFilters: CurrentFilter[] = [];
492501
const backendService = this._gridOptions.backendServiceApi.service;
493502
if (backendService && backendService.getCurrentFilters) {
494503
currentFilters = backendService.getCurrentFilters() as CurrentFilter[];
495504
}
496505
this.onFilterChanged.next(currentFilters);
497-
} else if (caller === 'local') {
506+
} else if (caller === EmitterType.local) {
498507
this.onFilterChanged.next(this.getCurrentLocalFilters());
499508
}
500509
}

src/app/modules/angular-slickgrid/services/graphql.service.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ export class GraphqlService implements BackendService {
3636
private _grid: any;
3737
options: GraphqlServiceOption;
3838
pagination: Pagination | undefined;
39-
defaultOrderBy: GraphqlSortingOption = { field: 'id', direction: SortDirection.ASC };
4039
defaultPaginationOptions: GraphqlPaginationOption | GraphqlCursorPaginationOption = {
4140
first: DEFAULT_ITEMS_PER_PAGE,
4241
offset: 0
@@ -437,7 +436,7 @@ export class GraphqlService implements BackendService {
437436
*/
438437
updateSorters(sortColumns?: ColumnSort[], presetSorters?: CurrentSorter[]) {
439438
let currentSorters: CurrentSorter[] = [];
440-
let graphqlSorters: GraphqlSortingOption[] = [];
439+
const graphqlSorters: GraphqlSortingOption[] = [];
441440

442441
if (!sortColumns && presetSorters) {
443442
// make the presets the current sorters, also make sure that all direction are in uppercase for GraphQL
@@ -470,23 +469,18 @@ export class GraphqlService implements BackendService {
470469
} else if (sortColumns && !presetSorters) {
471470
// build the orderBy array, it could be multisort, example
472471
// orderBy:[{field: lastName, direction: ASC}, {field: firstName, direction: DESC}]
473-
if (sortColumns && sortColumns.length === 0) {
474-
graphqlSorters = new Array(this.defaultOrderBy); // when empty, use the default sort
475-
currentSorters = new Array({ columnId: this.defaultOrderBy.field, direction: this.defaultOrderBy.direction });
476-
} else {
477-
if (sortColumns) {
478-
for (const column of sortColumns) {
479-
if (column && column.sortCol) {
480-
currentSorters.push({
481-
columnId: column.sortCol.id + '',
482-
direction: column.sortAsc ? SortDirection.ASC : SortDirection.DESC
483-
});
484-
485-
graphqlSorters.push({
486-
field: (column.sortCol.queryField || column.sortCol.queryFieldSorter || column.sortCol.field || column.sortCol.id) + '',
487-
direction: column.sortAsc ? SortDirection.ASC : SortDirection.DESC
488-
});
489-
}
472+
if (Array.isArray(sortColumns) && sortColumns.length > 0) {
473+
for (const column of sortColumns) {
474+
if (column && column.sortCol) {
475+
currentSorters.push({
476+
columnId: column.sortCol.id + '',
477+
direction: column.sortAsc ? SortDirection.ASC : SortDirection.DESC
478+
});
479+
480+
graphqlSorters.push({
481+
field: (column.sortCol.queryField || column.sortCol.queryFieldSorter || column.sortCol.field || column.sortCol.id) + '',
482+
direction: column.sortAsc ? SortDirection.ASC : SortDirection.DESC
483+
});
490484
}
491485
}
492486
}

src/app/modules/angular-slickgrid/services/sort.service.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
Column,
33
ColumnSort,
44
CurrentSorter,
5+
EmitterType,
56
FieldType,
67
GraphqlResult,
78
GridOption,
@@ -73,7 +74,7 @@ export class SortService {
7374
}
7475

7576
const query = backendApi.service.processOnSortChanged(event, args);
76-
this.emitSortChanged('remote');
77+
this.emitSortChanged(EmitterType.remote);
7778

7879
// the processes can be Observables (like HttpClient) or Promises
7980
const process = backendApi.process(query);
@@ -121,7 +122,7 @@ export class SortService {
121122
}
122123

123124
this.onLocalSortChanged(grid, dataView, sortColumns);
124-
this.emitSortChanged('local');
125+
this.emitSortChanged(EmitterType.local);
125126
});
126127
}
127128

@@ -277,15 +278,15 @@ export class SortService {
277278
* Other services, like Pagination, can then subscribe to it.
278279
* @param sender
279280
*/
280-
emitSortChanged(sender: 'local' | 'remote') {
281-
if (sender === 'remote' && this._gridOptions && this._gridOptions.backendServiceApi) {
281+
emitSortChanged(sender: EmitterType) {
282+
if (sender === EmitterType.remote && this._gridOptions && this._gridOptions.backendServiceApi) {
282283
let currentSorters: CurrentSorter[] = [];
283284
const backendService = this._gridOptions.backendServiceApi.service;
284285
if (backendService && backendService.getCurrentSorters) {
285286
currentSorters = backendService.getCurrentSorters() as CurrentSorter[];
286287
}
287288
this.onSortChanged.next(currentSorters);
288-
} else if (sender === 'local') {
289+
} else if (sender === EmitterType.local) {
289290
this.onSortChanged.next(this.getCurrentLocalSorters());
290291
}
291292
}

0 commit comments

Comments
 (0)