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

Commit de21c7f

Browse files
authored
Merge pull request #234 from ghiscoding/bugfix/grid-menu-add-dynamic-column-definition
fix(menu): Column Picker & Grid Menu not updated dynamically, fixes #233
2 parents e963f3c + 800fd94 commit de21c7f

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

src/app/examples/grid-editor.component.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ export class GridEditorComponent implements OnInit {
112112
{
113113
id: 'edit',
114114
field: 'id',
115+
excludeFromColumnPicker: true,
116+
excludeFromGridMenu: true,
115117
excludeFromHeaderMenu: true,
116118
formatter: Formatters.editIcon,
117119
minWidth: 30,
@@ -126,6 +128,8 @@ export class GridEditorComponent implements OnInit {
126128
}, {
127129
id: 'delete',
128130
field: 'id',
131+
excludeFromColumnPicker: true,
132+
excludeFromGridMenu: true,
129133
excludeFromHeaderMenu: true,
130134
formatter: Formatters.deleteIcon,
131135
minWidth: 30,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ export class GridMenuExtension implements Extension {
5252
if (this._addon && this._addon.destroy) {
5353
this._addon.destroy();
5454
}
55+
this._userOriginalGridMenu = undefined;
56+
if (this.sharedService.gridOptions && this.sharedService.gridOptions.gridMenu && this.sharedService.gridOptions.gridMenu.customItems) {
57+
this.sharedService.gridOptions.gridMenu.customItems = [];
58+
}
5559
}
5660

5761
showGridMenu(e) {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,48 @@ describe('ExtensionService', () => {
518518
it('should call "setColumns" on the Shared Service with the collection provided as argument', () => {
519519
const columnsMock = [{ id: 'field1', field: 'field1', headerKey: 'HELLO' }] as Column[];
520520
jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub);
521+
const spyAllCols = jest.spyOn(SharedService.prototype, 'allColumns', 'set');
521522
const setColumnsSpy = jest.spyOn(gridStub, 'setColumns');
522523

523524
service.renderColumnHeaders(columnsMock);
524525

526+
expect(spyAllCols).toHaveBeenCalledWith(columnsMock);
527+
expect(setColumnsSpy).toHaveBeenCalledWith(columnsMock);
528+
});
529+
530+
it('should re-register the Column Picker when enable and method is called with new column definition collection provided as argument', () => {
531+
const gridOptionsMock = { enableColumnPicker: true } as GridOption;
532+
const columnsMock = [{ id: 'field1', field: 'field1', headerKey: 'HELLO' }] as Column[];
533+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
534+
jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub);
535+
const spyCpDispose = jest.spyOn(extensionColumnPickerStub, 'dispose');
536+
const spyCpRegister = jest.spyOn(extensionColumnPickerStub, 'register');
537+
const spyAllCols = jest.spyOn(SharedService.prototype, 'allColumns', 'set');
538+
const setColumnsSpy = jest.spyOn(gridStub, 'setColumns');
539+
540+
service.renderColumnHeaders(columnsMock);
541+
542+
expect(spyCpDispose).toHaveBeenCalled();
543+
expect(spyCpRegister).toHaveBeenCalled();
544+
expect(spyAllCols).toHaveBeenCalledWith(columnsMock);
545+
expect(setColumnsSpy).toHaveBeenCalledWith(columnsMock);
546+
});
547+
548+
it('should re-register the Grid Menu when enable and method is called with new column definition collection provided as argument', () => {
549+
const gridOptionsMock = { enableGridMenu: true } as GridOption;
550+
const columnsMock = [{ id: 'field1', field: 'field1', headerKey: 'HELLO' }] as Column[];
551+
jest.spyOn(SharedService.prototype, 'gridOptions', 'get').mockReturnValue(gridOptionsMock);
552+
jest.spyOn(SharedService.prototype, 'grid', 'get').mockReturnValue(gridStub);
553+
const spyGmDispose = jest.spyOn(extensionGridMenuStub, 'dispose');
554+
const spyGmRegister = jest.spyOn(extensionGridMenuStub, 'register');
555+
const spyAllCols = jest.spyOn(SharedService.prototype, 'allColumns', 'set');
556+
const setColumnsSpy = jest.spyOn(gridStub, 'setColumns');
557+
558+
service.renderColumnHeaders(columnsMock);
559+
560+
expect(spyGmDispose).toHaveBeenCalled();
561+
expect(spyGmRegister).toHaveBeenCalled();
562+
expect(spyAllCols).toHaveBeenCalledWith(columnsMock);
525563
expect(setColumnsSpy).toHaveBeenCalledWith(columnsMock);
526564
});
527565
});

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ export class ExtensionService {
319319

320320
// re-render the column headers
321321
this.renderColumnHeaders(columnDefinitions);
322+
this.gridMenuExtension.translateGridMenu();
322323
}
323324

324325
/**
@@ -331,8 +332,19 @@ export class ExtensionService {
331332
collection = this.sharedService.columnDefinitions;
332333
}
333334
if (Array.isArray(collection) && this.sharedService.grid && this.sharedService.grid.setColumns) {
335+
this.sharedService.allColumns = collection;
334336
this.sharedService.grid.setColumns(collection);
335337
}
338+
339+
if (this.sharedService.gridOptions.enableColumnPicker) {
340+
this.columnPickerExtension.dispose();
341+
this.columnPickerExtension.register();
342+
}
343+
344+
if (this.sharedService.gridOptions.enableGridMenu) {
345+
this.gridMenuExtension.dispose();
346+
this.gridMenuExtension.register();
347+
}
336348
}
337349

338350
/** Translate an array of items from an input key and assign translated value to the output key */

0 commit comments

Comments
 (0)