Skip to content

Commit 35936ad

Browse files
committed
feat(sorting): allow to bypass changed events when calling updateSorting
1 parent 62c807e commit 35936ad

File tree

2 files changed

+62
-16
lines changed

2 files changed

+62
-16
lines changed

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

+33-4
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ describe('SortService', () => {
617617
jest.spyOn(gridStub, 'getColumns').mockReturnValue([mockColumn1, mockColumn2]);
618618
});
619619

620-
it('should throw an error when there are no filters defined in the column definitions', (done) => {
620+
it('should throw an error when there are no sorters defined in the column definitions', (done) => {
621621
try {
622622
gridOptionMock.enableSorting = false;
623623
service.bindLocalOnSort(gridStub, dataViewStub);
@@ -628,7 +628,7 @@ describe('SortService', () => {
628628
}
629629
});
630630

631-
it('should trigger an "emitSortChanged" local when using "bindLocalOnSort" and also expect sorters to be set in ColumnFilters', () => {
631+
it('should trigger an "emitSortChanged" local when using "bindLocalOnSort" and also expect sorters to be set in CurrentLocalSorter', () => {
632632
const emitSpy = jest.spyOn(service, 'emitSortChanged');
633633

634634
service.bindLocalOnSort(gridStub, dataViewStub);
@@ -641,21 +641,50 @@ describe('SortService', () => {
641641
]);
642642
});
643643

644-
it('should trigger an "emitSortChanged" remote when using "bindLocalOnSort" and also expect sorters to be set in ColumnFilters', () => {
644+
it('should expect sorters to be set in CurrentLocalSorter when using "bindLocalOnSort" without triggering a sort changed event when 2nd flag argument is set to false', () => {
645+
const emitSpy = jest.spyOn(service, 'emitSortChanged');
646+
647+
service.bindLocalOnSort(gridStub, dataViewStub);
648+
service.updateSorting(mockNewSorters, false);
649+
650+
expect(emitSpy).not.toHaveBeenCalled();
651+
expect(service.getCurrentLocalSorters()).toEqual([
652+
{ columnId: 'firstName', direction: 'ASC' },
653+
{ columnId: 'isActive', direction: 'DESC' }
654+
]);
655+
});
656+
657+
it('should trigger an "emitSortChanged" remote when using "bindBackendOnSort" and also expect sorters to be sent to the backend when using "bindBackendOnSort"', () => {
645658
gridOptionMock.backendServiceApi = {
646659
service: backendServiceStub,
647660
process: () => new Promise((resolve) => resolve(jest.fn())),
648661
};
649662
const emitSpy = jest.spyOn(service, 'emitSortChanged');
650663
const backendUpdateSpy = jest.spyOn(backendServiceStub, 'updateSorters');
651664

652-
service.bindLocalOnSort(gridStub, dataViewStub);
665+
service.bindBackendOnSort(gridStub, dataViewStub);
653666
service.updateSorting(mockNewSorters);
654667

655668
expect(emitSpy).toHaveBeenCalledWith('remote');
656669
expect(service.getCurrentLocalSorters()).toEqual([]);
657670
expect(backendUpdateSpy).toHaveBeenCalledWith(undefined, mockNewSorters);
658671
expect(mockRefreshBackendDataset).toHaveBeenCalledWith(gridOptionMock);
659672
});
673+
674+
it('should expect sorters to be sent to the backend when using "bindBackendOnSort" without triggering a sort changed event neither a backend query when both flag arguments are set to false', () => {
675+
gridOptionMock.backendServiceApi = {
676+
service: backendServiceStub,
677+
process: () => new Promise((resolve) => resolve(jest.fn())),
678+
};
679+
const emitSpy = jest.spyOn(service, 'emitSortChanged');
680+
const backendUpdateSpy = jest.spyOn(backendServiceStub, 'updateSorters');
681+
682+
service.bindBackendOnSort(gridStub, dataViewStub);
683+
service.updateSorting(mockNewSorters, false, false);
684+
685+
expect(emitSpy).not.toHaveBeenCalled();
686+
expect(backendUpdateSpy).toHaveBeenCalledWith(undefined, mockNewSorters);
687+
expect(mockRefreshBackendDataset).not.toHaveBeenCalled();
688+
});
660689
});
661690
});

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

+29-12
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export class SortService {
233233
backendApi.preProcess();
234234
}
235235

236-
// query backend, except when it's called by a ClearFilters then we won't
236+
// query backend
237237
const query = backendApi.service.processOnSortChanged(event, args);
238238
const totalItems = gridOptions && gridOptions.pagination && gridOptions.pagination.totalItems;
239239
executeBackendCallback(backendApi, query, args, startTime, totalItems, this.emitSortChanged.bind(this));
@@ -287,23 +287,40 @@ export class SortService {
287287
return SortDirectionNumber.neutral;
288288
}
289289

290-
updateSorting(sorters: CurrentSorter[]) {
290+
/**
291+
* Update Sorting (sorters) dynamically just by providing an array of sorter(s).
292+
* You can also choose emit (default) a Sort Changed event that will be picked by the Grid State Service.
293+
*
294+
* Also for backend service only, you can choose to trigger a backend query (default) or not if you wish to do it later,
295+
* this could be useful when using updateFilters & updateSorting and you wish to only send the backend query once.
296+
* @param sorters array
297+
* @param triggerEvent defaults to True, do we want to emit a sort changed event?
298+
* @param triggerBackendQuery defaults to True, which will query the backend.
299+
*/
300+
updateSorting(sorters: CurrentSorter[], emitChangedEvent = true, triggerBackendQuery = true) {
291301
if (!this._gridOptions || !this._gridOptions.enableSorting) {
292302
throw new Error('[Angular-Slickgrid] in order to use "updateSorting" method, you need to have Sortable Columns defined in your grid and "enableSorting" set in your Grid Options');
293303
}
294304

295-
const backendApi = this._gridOptions && this._gridOptions.backendServiceApi;
305+
if (Array.isArray(sorters)) {
306+
const backendApi = this._gridOptions && this._gridOptions.backendServiceApi;
307+
308+
if (backendApi) {
309+
const backendApiService = backendApi && backendApi.service;
310+
if (backendApiService) {
311+
backendApiService.updateSorters(undefined, sorters);
312+
if (triggerBackendQuery) {
313+
refreshBackendDataset(this._gridOptions);
314+
}
315+
}
316+
} else {
317+
this.loadGridSorters(sorters);
318+
}
296319

297-
if (backendApi) {
298-
const backendApiService = backendApi && backendApi.service;
299-
if (backendApiService) {
300-
backendApiService.updateSorters(undefined, sorters);
301-
refreshBackendDataset(this._gridOptions);
302-
this.emitSortChanged(EmitterType.remote);
320+
if (emitChangedEvent) {
321+
const emitterType = backendApi ? EmitterType.remote : EmitterType.local;
322+
this.emitSortChanged(emitterType);
303323
}
304-
} else {
305-
this.loadGridSorters(sorters);
306-
this.emitSortChanged(EmitterType.local);
307324
}
308325
}
309326
}

0 commit comments

Comments
 (0)