Skip to content

Commit 49ab5e5

Browse files
committed
feat(core): methods to change column positions/visibilities dynamically
1 parent 12638cc commit 49ab5e5

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed

CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ All notable changes to this project will be documented in this file. See [standa
55
### [2.24.1](https://github.com/ghiscoding/angular-slickgrid/compare/v2.24.0...v2.24.1) (2020-12-10)
66

77

8-
### Features
8+
### Bug Fixes
99

10-
* **filters:** use offset left to calculate auto position (left/right) ([9d79e2d](https://github.com/ghiscoding/angular-slickgrid/commit/9d79e2d149b21fb8766e0ad40cbbd4cf4f6572e6))
10+
* **filters:** use offset left to calculate multi-select auto position (left/right) ([9d79e2d](https://github.com/ghiscoding/angular-slickgrid/commit/9d79e2d149b21fb8766e0ad40cbbd4cf4f6572e6))
1111

1212
## [2.24.0](https://github.com/ghiscoding/angular-slickgrid/compare/v2.23.3...v2.24.0) (2020-12-10)
1313

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

+85
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ const gridStub = {
4747
getColumns: jest.fn(),
4848
getSelectionModel: jest.fn(),
4949
getSelectedRows: jest.fn(),
50+
setColumns: jest.fn(),
5051
setSelectedRows: jest.fn(),
5152
onColumnsReordered: new Slick.Event(),
5253
onColumnsResized: new Slick.Event(),
@@ -133,6 +134,64 @@ describe('GridStateService', () => {
133134
});
134135
});
135136

137+
describe('changeColumnsArrangement method', () => {
138+
const rowCheckboxColumnMock: Column = { id: '_checkbox_selector', field: '_checkbox_selector', minWidth: 50 };
139+
let presetColumnsMock: CurrentColumn[];
140+
let columnsWithoutCheckboxMock: Column[];
141+
let allColumnsMock: Column[];
142+
143+
beforeEach(() => {
144+
allColumnsMock = [
145+
rowCheckboxColumnMock,
146+
{ id: 'field1', field: 'field1', width: 100, cssClass: 'red' },
147+
{ id: 'field2', field: 'field2', width: 150, headerCssClass: 'blue' },
148+
{ id: 'field3', field: 'field3' },
149+
] as Column[];
150+
columnsWithoutCheckboxMock = [
151+
{ id: 'field2', field: 'field2', width: 150, headerCssClass: 'blue' },
152+
{ id: 'field1', field: 'field1', width: 100, cssClass: 'red' },
153+
{ id: 'field3', field: 'field3' },
154+
] as Column[];
155+
presetColumnsMock = [
156+
{ columnId: 'field2', width: 150, headerCssClass: 'blue' },
157+
{ columnId: 'field1', width: 100, cssClass: 'red' },
158+
{ columnId: 'field3' },
159+
] as CurrentColumn[];
160+
jest.spyOn(service, 'getAssociatedGridColumns').mockReturnValue([...columnsWithoutCheckboxMock]);
161+
});
162+
163+
afterEach(() => {
164+
jest.clearAllMocks();
165+
});
166+
167+
it('should call the method and expect slickgrid "setColumns" and "autosizeColumns" to be called with newest columns', () => {
168+
gridOptionMock.enableCheckboxSelector = true;
169+
jest.spyOn(SharedService.prototype, 'allColumns', 'get').mockReturnValue(allColumnsMock);
170+
const setColsSpy = jest.spyOn(gridStub, 'setColumns');
171+
const autoSizeSpy = jest.spyOn(gridStub, 'autosizeColumns');
172+
173+
service.changeColumnsArrangement(presetColumnsMock);
174+
175+
expect(setColsSpy).toHaveBeenCalledWith([rowCheckboxColumnMock, ...columnsWithoutCheckboxMock]);
176+
expect(autoSizeSpy).toHaveBeenCalled();
177+
});
178+
179+
it('should call the method and expect only 1 method of slickgrid "setColumns" to be called when we define 2nd argument (triggerAutoSizeColumns) as False', () => {
180+
const setColsSpy = jest.spyOn(gridStub, 'setColumns');
181+
const autoSizeSpy = jest.spyOn(gridStub, 'autosizeColumns');
182+
const presetColumnsMock = [
183+
{ columnId: 'field2', width: 150, headerCssClass: 'blue' },
184+
{ columnId: 'field1', width: 100, cssClass: 'red' },
185+
{ columnId: 'field3' },
186+
] as CurrentColumn[];
187+
188+
service.changeColumnsArrangement(presetColumnsMock, false);
189+
190+
expect(setColsSpy).toHaveBeenCalledWith([rowCheckboxColumnMock, ...columnsWithoutCheckboxMock]);
191+
expect(autoSizeSpy).not.toHaveBeenCalled();
192+
});
193+
});
194+
136195
describe('bindExtensionAddonEventToGridStateChange tests', () => {
137196
it('should subscribe to some Extension Addon SlickGrid events and expect the event to be triggered when a notify is triggered after service was initialized', () => {
138197
const instanceMock = { onColumnsChanged: slickgridEvent };
@@ -832,6 +891,32 @@ describe('GridStateService', () => {
832891
});
833892
});
834893

894+
describe('resetToOriginalColumns method', () => {
895+
afterEach(() => {
896+
jest.clearAllMocks();
897+
});
898+
899+
it('should call the method and expect 2x slickgrid methods to be called by default "setColumns" & "autosizeColumns"', () => {
900+
const setColsSpy = jest.spyOn(gridStub, 'setColumns');
901+
const autoSizeSpy = jest.spyOn(gridStub, 'autosizeColumns');
902+
903+
service.resetToOriginalColumns();
904+
905+
expect(setColsSpy).toHaveBeenCalled();
906+
expect(autoSizeSpy).toHaveBeenCalled();
907+
});
908+
909+
it('should call the method and expect only 1 slickgrid methods to be called "setColumns"', () => {
910+
const setColsSpy = jest.spyOn(gridStub, 'setColumns');
911+
const autoSizeSpy = jest.spyOn(gridStub, 'autosizeColumns');
912+
913+
service.resetToOriginalColumns(false);
914+
915+
expect(setColsSpy).toHaveBeenCalled();
916+
expect(autoSizeSpy).not.toHaveBeenCalled();
917+
});
918+
});
919+
835920
describe('resetRowSelection method', () => {
836921
it('should call the method and do nothing when row selection is not in use', () => {
837922
const setSelectSpy = jest.spyOn(gridStub, 'setSelectedRows');

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

+45-1
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,45 @@ export class GridStateService {
148148
return currentColumns;
149149
}
150150

151+
/**
152+
* Dynamically change the arrangement/distribution of the columns Positions/Visibilities and optionally Widths.
153+
* For a column to have its visibly as hidden, it has to be part of the original list but excluded from the list provided as argument to be considered a hidden field.
154+
* If you are passing columns Width, then you probably don't want to trigger the autosizeColumns (2nd argument to False).
155+
* @param {Array<Column>} definedColumns - defined columns
156+
* @param {Boolean} triggerAutoSizeColumns - True by default, do we also want to call the "autosizeColumns()" method to make the columns fit in the grid?
157+
*/
158+
changeColumnsArrangement(definedColumns: CurrentColumn[], triggerAutoSizeColumns = true) {
159+
if (Array.isArray(definedColumns) && definedColumns.length > 0) {
160+
const gridColumns: Column[] = this.getAssociatedGridColumns(this._grid, definedColumns);
161+
162+
if (gridColumns && Array.isArray(gridColumns) && gridColumns.length > 0) {
163+
// make sure that the checkbox selector is still visible in the list when it is enabled
164+
if (this._gridOptions.enableCheckboxSelector) {
165+
const checkboxColumn = (Array.isArray(this.sharedService.allColumns) && this.sharedService.allColumns.length > 0) ? this.sharedService.allColumns[0] : null;
166+
if (checkboxColumn && checkboxColumn.id === '_checkbox_selector' && gridColumns[0].id !== '_checkbox_selector') {
167+
gridColumns.unshift(checkboxColumn);
168+
}
169+
}
170+
171+
// finally set the new presets columns (including checkbox selector if need be)
172+
this._grid.setColumns(gridColumns);
173+
174+
// resize the columns to fit the grid canvas
175+
if (triggerAutoSizeColumns) {
176+
this._grid.autosizeColumns();
177+
}
178+
}
179+
}
180+
}
181+
151182
/**
152183
* From an array of Current Columns, get the associated Grid Column Definitions
153184
* @param grid
154185
* @param currentColumns
155186
*/
156187
getAssociatedGridColumns(grid: any, currentColumns: CurrentColumn[]): Column[] {
157188
const columns: Column[] = [];
158-
const gridColumns: Column[] = grid.getColumns();
189+
const gridColumns: Column[] = this.sharedService.allColumns || grid.getColumns();
159190

160191
if (currentColumns && Array.isArray(currentColumns)) {
161192
currentColumns.forEach((currentColumn: CurrentColumn, index: number) => {
@@ -289,6 +320,19 @@ export class GridStateService {
289320
this.onGridStateChanged.next({ change: { newValues: currentColumns, type: GridStateType.columns }, gridState: this.getCurrentGridState() });
290321
}
291322

323+
/**
324+
* Reset the grid to its original (all) columns, that is to display the entire set of columns with their original positions & visibilities
325+
* @param {Boolean} triggerAutoSizeColumns - True by default, do we also want to call the "autosizeColumns()" method to make the columns fit in the grid?
326+
*/
327+
resetToOriginalColumns(triggerAutoSizeColumns = true) {
328+
this._grid.setColumns(this.sharedService.allColumns);
329+
330+
// resize the columns to fit the grid canvas
331+
if (triggerAutoSizeColumns) {
332+
this._grid.autosizeColumns();
333+
}
334+
}
335+
292336
/** if we use Row Selection or the Checkbox Selector, we need to reset any selection */
293337
resetRowSelectionWhenRequired() {
294338
if (!this.needToPreserveRowSelection() && (this._gridOptions.enableRowSelection || this._gridOptions.enableCheckboxSelector)) {

test/cypress/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"author": "Ghislain B.",
1111
"license": "MIT",
1212
"devDependencies": {
13-
"cypress": "^6.0.1",
13+
"cypress": "^6.1.0",
1414
"mocha": "^8.2.0",
1515
"mochawesome": "^6.1.1"
1616
}

0 commit comments

Comments
 (0)