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

Commit 60074ba

Browse files
authored
Merge pull request #410 from ghiscoding/bugfix/dynamically-remove-columns
fix(columns): remove columns dynamically with Editors, fixes #406
2 parents cb59103 + 903473d commit 60074ba

File tree

4 files changed

+65
-10
lines changed

4 files changed

+65
-10
lines changed

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -606,22 +606,34 @@ export class GridEditorComponent implements OnInit {
606606
this.columnDefinitions = this.columnDefinitions.slice(); // or use spread operator [...cols]
607607

608608
// NOTE if you use an Extensions (Checkbox Selector, Row Detail, ...) that modifies the column definitions in any way
609-
// you MUST use "getColumns()", using this will be ALL columns including the 1st column that is created internally
609+
// you MUST use "getAllColumnDefinitions()" from the GridService, using this will be ALL columns including the 1st column that is created internally
610610
// for example if you use the Checkbox Selector (row selection), you MUST use the code below
611611
/*
612-
const allColumns = this.gridObj.getColumns();
612+
const allColumns = this.angularGrid.gridService.getAllColumnDefinitions();
613613
allColumns.push(newCol);
614614
this.columnDefinitions = [...allColumns]; // (or use slice) reassign to column definitions for Angular to do dirty checking
615615
*/
616616
}
617617

618618
dynamicallyRemoveLastColumn() {
619-
const allColumns = this.gridObj.getColumns();
619+
this.columnDefinitions.pop();
620+
this.columnDefinitions = this.columnDefinitions.slice();
621+
622+
// NOTE if you use an Extensions (Checkbox Selector, Row Detail, ...) that modifies the column definitions in any way
623+
// you MUST use the code below, first you must reassign the Editor facade (from the internalColumnEditor back to the editor)
624+
// in other words, SlickGrid is not using the same as Angular-Slickgrid uses (editor with a "model" and other properties are a facade, SlickGrid only uses what is inside the model)
625+
/*
626+
const allColumns = this.angularGrid.gridService.getAllColumnDefinitions();
627+
const allOriginalColumns = allColumns.map((column) => {
628+
column.editor = column.internalColumnEditor;
629+
return column;
630+
});
620631
621632
// remove your column the full set of columns
622633
// and use slice or spread [...] to trigger an Angular dirty change
623-
allColumns.pop();
624-
this.columnDefinitions = allColumns.slice();
634+
allOriginalColumns.pop();
635+
this.columnDefinitions = allOriginalColumns.slice();
636+
*/
625637
}
626638

627639
setAutoEdit(isAutoEdit) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ export class GridLocalizationComponent implements OnInit {
234234
this.columnDefinitions = this.columnDefinitions.slice(); // or use spread operator [...cols]
235235

236236
// NOTE if you use an Extensions (Checkbox Selector, Row Detail, ...) that modifies the column definitions in any way
237-
// you MUST use "getColumns()" instead to get ALL columns including the 1st column that is created internally
238-
// for example if you use the Checkbox Selector (row selection), you need to use the code below
237+
// you MUST use "getAllColumnDefinitions()" from the GridService, using this will be ALL columns including the 1st column that is created internally
238+
// for example if you use the Checkbox Selector (row selection), you MUST use the code below
239239
/*
240-
const allColumns = this.gridObj.getColumns();
240+
const allColumns = this.angularGrid.gridService.getAllColumnDefinitions();
241241
allColumns.push(newCol);
242242
this.columnDefinitions = [...allColumns]; // (or use slice) reassign to column definitions for Angular to do dirty checking
243243
*/

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TestBed } from '@angular/core/testing';
22
import { TranslateService, TranslateModule } from '@ngx-translate/core';
3-
import { GridService, ExtensionService, FilterService, GridStateService, SortService } from '..';
3+
import { GridService, ExtensionService, FilterService, GridStateService, SortService, SharedService } from '..';
44
import { CellArgs, Column, OnEventArgs, GridOption } from './../../models';
55

66
declare var Slick: any;
@@ -62,6 +62,7 @@ const gridStub = {
6262

6363
describe('Grid Service', () => {
6464
let service: GridService;
65+
let sharedService = new SharedService();
6566
let translate: TranslateService;
6667
jest.spyOn(gridStub, 'getOptions').mockReturnValue({ enableAutoResize: true } as GridOption);
6768

@@ -71,12 +72,14 @@ describe('Grid Service', () => {
7172
{ provide: ExtensionService, useValue: extensionServiceStub },
7273
{ provide: FilterService, useValue: filterServiceStub },
7374
{ provide: GridStateService, useValue: gridStateServiceStub },
75+
{ provide: SharedService, useValue: sharedService },
7476
{ provide: SortService, useValue: sortServiceStub },
7577
GridService,
7678
],
7779
imports: [TranslateModule.forRoot()]
7880
});
7981
translate = TestBed.get(TranslateService);
82+
sharedService = TestBed.get(SharedService);
8083
service = TestBed.get(GridService);
8184
service.init(gridStub, dataviewStub);
8285
});
@@ -89,6 +92,30 @@ describe('Grid Service', () => {
8992
expect(service).toBeTruthy();
9093
});
9194

95+
describe('getAllColumnDefinitions method', () => {
96+
it('should call "allColumns" GETTER ', () => {
97+
const mockColumns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }];
98+
const getSpy = jest.spyOn(SharedService.prototype, 'allColumns', 'get').mockReturnValue(mockColumns);
99+
100+
const output = service.getAllColumnDefinitions();
101+
102+
expect(getSpy).toHaveBeenCalled();
103+
expect(output).toEqual(mockColumns);
104+
});
105+
});
106+
107+
describe('getVisibleColumnDefinitions method', () => {
108+
it('should call "visibleColumns" GETTER ', () => {
109+
const mockColumns = [{ id: 'field1', field: 'field1', width: 100 }, { id: 'field2', field: 'field2', width: 100 }];
110+
const getSpy = jest.spyOn(SharedService.prototype, 'visibleColumns', 'get').mockReturnValue(mockColumns);
111+
112+
const output = service.getVisibleColumnDefinitions();
113+
114+
expect(getSpy).toHaveBeenCalled();
115+
expect(output).toEqual(mockColumns);
116+
});
117+
});
118+
92119
describe('upsertItem methods', () => {
93120
afterEach(() => {
94121
jest.clearAllMocks();

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { Injectable } from '@angular/core';
2+
import { Subject } from 'rxjs';
3+
24
import {
35
CellArgs,
46
Column,
@@ -11,8 +13,8 @@ import {
1113
import { ExtensionService } from './extension.service';
1214
import { FilterService } from './filter.service';
1315
import { GridStateService } from './gridState.service';
16+
import { SharedService } from './shared.service';
1417
import { SortService } from './sort.service';
15-
import { Subject } from 'rxjs';
1618

1719
// using external non-typed js libraries
1820
declare var Slick: any;
@@ -34,6 +36,7 @@ export class GridService {
3436
private extensionService: ExtensionService,
3537
private filterService: FilterService,
3638
private gridStateService: GridStateService,
39+
private sharedService: SharedService,
3740
private sortService: SortService
3841
) { }
3942

@@ -58,6 +61,19 @@ export class GridService {
5861
}
5962
}
6063

64+
/**
65+
* Get all column set in the grid, that is all visible/hidden columns
66+
* and also include any extra columns used by some plugins (like Row Selection, Row Detail, ...)
67+
*/
68+
getAllColumnDefinitions() {
69+
return this.sharedService.allColumns;
70+
}
71+
72+
/** Get only visible column definitions and also include any extra columns by some plugins (like Row Selection, Row Detail, ...) */
73+
getVisibleColumnDefinitions() {
74+
return this.sharedService.visibleColumns;
75+
}
76+
6177
/**
6278
* From a SlickGrid Event triggered get the Column Definition and Item Data Context
6379
*

0 commit comments

Comments
 (0)