Skip to content

Commit 26f28ab

Browse files
FIX: setState() throws error when column and columnGroup have same header (#13709)
* fix(GridState): when restoring column groups add additional check #13697 * test(gridState): no error is thrown when column groups are restored #13697 * chore(*): set correctly the initialState value --------- Co-authored-by: Hristo <hanastasov@infragistics.com>
1 parent afb6fe0 commit 26f28ab

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

projects/igniteui-angular/src/lib/grids/state.directive.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,26 @@ describe('IgxGridState - input properties #grid', () => {
397397
expect(grid.columnInit.emit).toHaveBeenCalledTimes(columnsStateObject.columns.length);
398398
});
399399

400+
it('setState should correctly restore grid columns with Column Groups and same headers', () => {
401+
const fix = TestBed.createComponent(IgxGridStateComponent);
402+
fix.detectChanges();
403+
const state = fix.componentInstance.state;
404+
/* eslint-disable max-len */
405+
const initialState = '{"columns":[{"pinned":true,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"testCss","headerGroupClasses":"","maxWidth":"300px","groupable":false,"movable":false,"hidden":false,"dataType":"number","hasSummary":false,"field":"ProductID","width":"150px","header":"Product ID","resizable":true,"searchable":false,"parent":null,"columnGroup":false,"disableHiding":false,"disablePinning":false},{"pinned":false,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","maxWidth":"300px","groupable":true,"movable":false,"hidden":false,"dataType":"string","hasSummary":false,"field":"ProductName","width":"150px","header":"Prodyct Name","resizable":true,"searchable":true,"selectable":false,"parent":null,"columnGroup":false,"disableHiding":false,"disablePinning":false},{"pinned":false,"sortable":false,"filterable":true,"editable":true,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","maxWidth":"300px","groupable":false,"movable":false,"hidden":false,"dataType":"boolean","hasSummary":true,"field":"InStock","width":"140px","header":"In Stock","resizable":true,"searchable":true,"parent":null,"columnGroup":false,"disableHiding":false,"disablePinning":true},{"pinned":false,"sortable":true,"filterable":false,"editable":true,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","maxWidth":"300px","groupable":true,"movable":false,"hidden":false,"dataType":"date","hasSummary":false,"field":"OrderDate","width":"110px","header":"Date ordered","resizable":false,"searchable":true,"parent":null,"columnGroup":false,"disableHiding":false,"disablePinning":false}]}';
406+
const columnsState = '{"columns":[{"pinned":false,"sortable":false,"filterable":false,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","groupable":false,"movable":false,"hidden":false,"dataType":"string","hasSummary":false,"field":"ProductID","width":"199px","header":"General Information","resizable":true,"searchable":true,"selectable":true,"parent":null,"columnGroup":false,"disableHiding":false},{"pinned":false,"sortable":false,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","groupable":false,"movable":false,"hidden":false,"dataType":"string","hasSummary":false,"width":"398px","header":"General Information","resizable":false,"searchable":true,"selectable":true,"parent":null,"columnGroup":true,"disableHiding":false},{"pinned":false,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","groupable":false,"movable":false,"hidden":false,"dataType":"string","hasSummary":false,"field":"ProductName","width":"199px","header":"","resizable":true,"searchable":true,"selectable":true,"parent":"General Information","columnGroup":false,"disableHiding":false},{"pinned":false,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","groupable":false,"movable":false,"hidden":false,"dataType":"string","hasSummary":false,"field":"UnitsInStock","width":"199px","header":"","resizable":true,"searchable":true,"selectable":true,"parent":"General Information","columnGroup":false,"disableHiding":false},{"pinned":false,"sortable":true,"filterable":true,"editable":false,"sortingIgnoreCase":true,"filteringIgnoreCase":true,"headerClasses":"","headerGroupClasses":"","groupable":false,"movable":false,"hidden":false,"dataType":"string","hasSummary":false,"field":"InStock","width":"199px","header":"","resizable":true,"searchable":true,"selectable":true,"parent":null,"columnGroup":false,"disableHiding":false}],"filtering":{"filteringOperands":[],"operator":0},"advancedFiltering":{},"sorting":[],"groupBy":{"expressions":[],"expansion":[],"defaultExpanded":true},"cellSelection":[],"rowSelection":[],"columnSelection":[],"rowPinning":[],"expansion":[],"moving":true,"rowIslands":[]}';
407+
/* eslint-enable max-len */
408+
const columnsStateObject = JSON.parse(columnsState);
409+
let gridState = state.getState(true, 'columns');
410+
411+
expect(gridState).toBe(initialState);
412+
expect(() => {
413+
state.setState(columnsStateObject);
414+
}).not.toThrow();
415+
416+
gridState = state.getState(false, 'columns') as IGridState;
417+
HelperFunctions.verifyColumns(columnsStateObject.columns, gridState);
418+
});
419+
400420
it('setState should correctly restore grid paging state from string', () => {
401421
const fix = TestBed.createComponent(IgxGridStateComponent);
402422
fix.detectChanges();

projects/igniteui-angular/src/lib/grids/state.directive.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ export class IgxGridStateDirective {
216216
Object.assign(ref1.instance, colState);
217217
ref1.instance.grid = context.currGrid;
218218
if (ref1.instance.parent) {
219-
const columnGroup: IgxColumnGroupComponent = newColumns.find(e => e.header === ref1.instance.parent);
219+
const columnGroup: IgxColumnGroupComponent = newColumns.find(e => e.header === ref1.instance.parent && e.columnGroup);
220220
columnGroup.children.reset([...columnGroup.children.toArray(), ref1.instance]);
221221
ref1.instance.parent = columnGroup;
222222
}
@@ -227,7 +227,7 @@ export class IgxGridStateDirective {
227227
Object.assign(ref.instance, colState);
228228
ref.instance.grid = context.currGrid;
229229
if (ref.instance.parent) {
230-
const columnGroup: IgxColumnGroupComponent = newColumns.find(e => e.header === ref.instance.parent);
230+
const columnGroup: IgxColumnGroupComponent = newColumns.find(e => e.header === ref.instance.parent && e.columnGroup);
231231
if (columnGroup) {
232232
ref.instance.parent = columnGroup;
233233
columnGroup.children.reset([...columnGroup.children.toArray(), ref.instance]);

0 commit comments

Comments
 (0)