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

Commit b55051b

Browse files
authored
Merge pull request #745 from ghiscoding/bugfix/row-selection-hidden-row
fix(selection): full row selection should be selected w/show hidden row
2 parents 988be24 + 131ada5 commit b55051b

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

src/app/modules/angular-slickgrid/extensions/__tests__/columnPickerExtension.spec.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ declare const Slick: any;
1010

1111
const gridStub = {
1212
getOptions: jest.fn(),
13+
getSelectedRows: jest.fn(),
1314
registerPlugin: jest.fn(),
1415
setColumns: jest.fn(),
1516
setOptions: jest.fn(),
17+
setSelectedRows: jest.fn(),
1618
};
1719

1820
const mockAddon = jest.fn().mockImplementation(() => ({
@@ -71,7 +73,7 @@ describe('columnPickerExtension', () => {
7173
});
7274

7375
it('should register the addon', () => {
74-
const onRegisteredSpy = jest.spyOn(SharedService.prototype.gridOptions.columnPicker, 'onExtensionRegistered');
76+
const onRegisteredSpy = jest.spyOn(SharedService.prototype.gridOptions.columnPicker!, 'onExtensionRegistered');
7577

7678
const instance = extension.register();
7779
const addonInstance = extension.getAddonInstance();
@@ -84,7 +86,7 @@ describe('columnPickerExtension', () => {
8486

8587
it('should call internal event handler subscribe and expect the "onColumnsChanged" grid option to be called when addon notify is called', () => {
8688
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
87-
const onColumnSpy = jest.spyOn(SharedService.prototype.gridOptions.columnPicker, 'onColumnsChanged');
89+
const onColumnSpy = jest.spyOn(SharedService.prototype.gridOptions.columnPicker!, 'onColumnsChanged');
8890
const visibleColsSpy = jest.spyOn(SharedService.prototype, 'visibleColumns', 'set');
8991
const readjustSpy = jest.spyOn(extensionUtility, 'readjustFrozenColumnIndexWhenNeeded');
9092

@@ -104,7 +106,7 @@ describe('columnPickerExtension', () => {
104106
it(`should call internal event handler subscribe and expect the "onColumnsChanged" grid option to be called when addon notify is called
105107
and it should override "visibleColumns" when array passed as arguments is bigger than previous visible columns`, () => {
106108
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
107-
const onColumnSpy = jest.spyOn(SharedService.prototype.gridOptions.columnPicker, 'onColumnsChanged');
109+
const onColumnSpy = jest.spyOn(SharedService.prototype.gridOptions.columnPicker!, 'onColumnsChanged');
108110
const visibleColsSpy = jest.spyOn(SharedService.prototype, 'visibleColumns', 'set');
109111

110112
const instance = extension.register();
@@ -119,6 +121,25 @@ describe('columnPickerExtension', () => {
119121
expect(visibleColsSpy).toHaveBeenCalledWith(columnsMock);
120122
});
121123

124+
it('should call internal "onColumnsChanged" event and expect "setSelectedRows" method to be called using Row Selection is enabled', () => {
125+
const mockRowSelection = [0, 3, 5];
126+
127+
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
128+
jest.spyOn(gridStub, 'getSelectedRows').mockReturnValue(mockRowSelection);
129+
const setSelectionSpy = jest.spyOn(gridStub, 'setSelectedRows');
130+
131+
gridOptionsMock.enableRowSelection = true;
132+
const instance = extension.register();
133+
instance.onColumnsChanged.notify({ columnId: 'field1', showing: true, allColumns: columnsMock, columns: columnsMock.slice(0, 1), grid: gridStub }, new Slick.EventData(), gridStub);
134+
135+
expect(handlerSpy).toHaveBeenCalledTimes(1);
136+
expect(handlerSpy).toHaveBeenCalledWith(
137+
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
138+
expect.anything()
139+
);
140+
expect(setSelectionSpy).toHaveBeenCalledWith(mockRowSelection);
141+
});
142+
122143
it('should call internal "onColumnsChanged" event and expect "readjustFrozenColumnIndexWhenNeeded" method to be called when the grid is detected to be a frozen grid', () => {
123144
gridOptionsMock.frozenColumn = 0;
124145
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
@@ -157,9 +178,9 @@ describe('columnPickerExtension', () => {
157178
expect(utilitySpy).toHaveBeenCalled();
158179
expect(translateSpy).toHaveBeenCalled();
159180
expect(updateColsSpy).toHaveBeenCalledWith(SharedService.prototype.gridOptions.columnPicker);
160-
expect(SharedService.prototype.gridOptions.columnPicker.columnTitle).toBe('Colonnes');
161-
expect(SharedService.prototype.gridOptions.columnPicker.forceFitTitle).toBe('Ajustement forcé des colonnes');
162-
expect(SharedService.prototype.gridOptions.columnPicker.syncResizeTitle).toBe('Redimension synchrone');
181+
expect(SharedService.prototype.gridOptions.columnPicker!.columnTitle).toBe('Colonnes');
182+
expect(SharedService.prototype.gridOptions.columnPicker!.forceFitTitle).toBe('Ajustement forcé des colonnes');
183+
expect(SharedService.prototype.gridOptions.columnPicker!.syncResizeTitle).toBe('Redimension synchrone');
163184
expect(columnsMock).toEqual([
164185
{ id: 'field1', field: 'field1', width: 100, name: 'Titre', nameKey: 'TITLE' },
165186
{ id: 'field2', field: 'field2', width: 75 }

src/app/modules/angular-slickgrid/extensions/__tests__/gridMenuExtension.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ const gridStub = {
3939
getColumnIndex: jest.fn(),
4040
getColumns: jest.fn(),
4141
getOptions: jest.fn(),
42+
getSelectedRows: jest.fn(),
4243
getUID: () => gridUid,
4344
registerPlugin: jest.fn(),
4445
setColumns: jest.fn(),
4546
setOptions: jest.fn(),
4647
setHeaderRowVisibility: jest.fn(),
48+
setSelectedRows: jest.fn(),
4749
setTopPanelVisibility: jest.fn(),
4850
setPreHeaderPanelVisibility: jest.fn(),
4951
};
@@ -240,6 +242,25 @@ describe('gridMenuExtension', () => {
240242
expect(visibleColsSpy).toHaveBeenCalledWith(columnsMock);
241243
});
242244

245+
it('should call internal "onColumnsChanged" event and expect "setSelectedRows" method to be called using Row Selection is enabled', () => {
246+
const mockRowSelection = [0, 3, 5];
247+
248+
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');
249+
jest.spyOn(gridStub, 'getSelectedRows').mockReturnValue(mockRowSelection);
250+
const setSelectionSpy = jest.spyOn(gridStub, 'setSelectedRows');
251+
252+
gridOptionsMock.enableRowSelection = true;
253+
const instance = extension.register();
254+
instance.onColumnsChanged.notify({ columnId: 'field1', showing: true, allColumns: columnsMock, columns: columnsMock.slice(0, 1), grid: gridStub }, new Slick.EventData(), gridStub);
255+
256+
expect(handlerSpy).toHaveBeenCalledTimes(5);
257+
expect(handlerSpy).toHaveBeenCalledWith(
258+
{ notify: expect.anything(), subscribe: expect.anything(), unsubscribe: expect.anything(), },
259+
expect.anything()
260+
);
261+
expect(setSelectionSpy).toHaveBeenCalledWith(mockRowSelection);
262+
});
263+
243264
it('should call internal "onColumnsChanged" event and expect "readjustFrozenColumnIndexWhenNeeded" method to be called when the grid is detected to be a frozen grid', () => {
244265
gridOptionsMock.frozenColumn = 0;
245266
const handlerSpy = jest.spyOn(extension.eventHandler, 'subscribe');

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,22 @@ export class ColumnPickerExtension implements Extension {
5959
if (this._columnPicker && typeof this._columnPicker.onColumnsChanged === 'function') {
6060
this._columnPicker.onColumnsChanged(e, args);
6161
}
62+
63+
// keep reference to the updated visible columns list
6264
if (args && Array.isArray(args.columns) && args.columns.length !== this.sharedService.visibleColumns.length) {
6365
this.sharedService.visibleColumns = args.columns;
6466
}
67+
68+
// when using row selection, SlickGrid will only apply the "selected" CSS class on the visible columns only
69+
// and if the row selection was done prior to the column being shown then that column that was previously hidden (at the time of the row selection)
70+
// will not have the "selected" CSS class because it wasn't visible at the time.
71+
// To bypass this problem we can simply recall the row selection with the same selection and that will trigger a re-apply of the CSS class
72+
// on all columns including the column we just made visible
73+
if (this.sharedService.gridOptions.enableRowSelection && args.showing) {
74+
const rowSelection = args.grid.getSelectedRows();
75+
args.grid.setSelectedRows(rowSelection);
76+
}
77+
6578
// if we're using frozen columns, we need to readjust pinning when the new hidden column becomes visible again on the left pinning container
6679
// we need to readjust frozenColumn index because SlickGrid freezes by index and has no knowledge of the columns themselves
6780
const frozenColumnIndex = this.sharedService.gridOptions.frozenColumn !== undefined ? this.sharedService.gridOptions.frozenColumn : -1;

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,21 @@ export class GridMenuExtension implements Extension {
121121
if (this._gridMenuOptions && typeof this._gridMenuOptions.onColumnsChanged === 'function') {
122122
this._gridMenuOptions.onColumnsChanged(e, args);
123123
}
124+
125+
// keep reference to the updated visible columns list
124126
if (args && Array.isArray(args.columns) && args.columns.length > this.sharedService.visibleColumns.length) {
125127
this.sharedService.visibleColumns = args.columns;
126128
}
129+
130+
// when using row selection, SlickGrid will only apply the "selected" CSS class on the visible columns only
131+
// and if the row selection was done prior to the column being shown then that column that was previously hidden (at the time of the row selection)
132+
// will not have the "selected" CSS class because it wasn't visible at the time.
133+
// To bypass this problem we can simply recall the row selection with the same selection and that will trigger a re-apply of the CSS class
134+
// on all columns including the column we just made visible
135+
if (this.sharedService.gridOptions.enableRowSelection && args.showing) {
136+
args.grid.setSelectedRows(args.grid.getSelectedRows());
137+
}
138+
127139
// if we're using frozen columns, we need to readjust pinning when the new hidden column becomes visible again on the left pinning container
128140
// we need to readjust frozenColumn index because SlickGrid freezes by index and has no knowledge of the columns themselves
129141
const frozenColumnIndex = this.sharedService.gridOptions.frozenColumn !== undefined ? this.sharedService.gridOptions.frozenColumn : -1;

0 commit comments

Comments
 (0)