Skip to content

Commit 8c0d755

Browse files
authored
feat(IgxState): Re-use matching columns for state. (#14756)
1 parent 1777562 commit 8c0d755

File tree

8 files changed

+166
-58
lines changed

8 files changed

+166
-58
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ All notable changes for each version of this project will be documented in this
3939
according to the target type that the editor mask includes. Defaults to `date`.
4040
- `IgxTabs`
4141
- Added `activation` property to control tab selection. In `auto` mode (default), tabs are selected instantly with Arrow or Home/End keys. In `manual` mode, tabs are focused with keys but only selected with Enter or Space.
42+
- `IgxGridState`
43+
- When possible the state directive nows reuses the column that already exists on the grid when restoring the state, instead of creating new column instances every time. This removes the need to set any complex objects manually back on the column on `columnInit`. The only instance where this is still necessary is when the column (or its children in case of column groups) have no `field` property so there's no way to uniquely identify the matching column.
4244

4345
### Themes
4446
- `Palettes`

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

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { IgxGridComponent } from './grid/grid.component';
1616
import { IgxHierarchicalGridComponent } from './hierarchical-grid/hierarchical-grid.component';
1717
import { GridSelectionRange } from './common/types';
1818
import { ISortingExpression } from '../data-operations/sorting-strategy';
19-
import { GridType, IGX_GRID_BASE, IPinningConfig } from './common/grid.interface';
19+
import { ColumnType, GridType, IGX_GRID_BASE, IPinningConfig } from './common/grid.interface';
2020
import { IgxPivotGridComponent } from './pivot-grid/pivot-grid.component';
2121
import { IPivotConfiguration, IPivotDimension } from './pivot-grid/pivot-grid.interface'
2222
import { PivotUtil } from './pivot-grid/pivot-util';
@@ -92,7 +92,12 @@ export interface IColumnState {
9292
resizable: boolean;
9393
searchable: boolean;
9494
columnGroup: boolean;
95-
parent: any;
95+
/**
96+
* @deprecated
97+
*/
98+
parent?: any;
99+
key: string;
100+
parentKey: string;
96101
disableHiding: boolean;
97102
disablePinning: boolean;
98103
collapsible?: boolean;
@@ -205,7 +210,8 @@ export class IgxGridStateBaseDirective {
205210
resizable: c.resizable,
206211
searchable: c.searchable,
207212
selectable: c.selectable,
208-
parent: c.parent ? c.parent.header : null,
213+
key: c.columnGroup ? this.getColumnGroupKey(c) : c.field,
214+
parentKey: c.parent ? this.getColumnGroupKey(c.parent) : undefined,
209215
columnGroup: c.columnGroup,
210216
disableHiding: c.disableHiding,
211217
disablePinning: c.disablePinning,
@@ -221,29 +227,42 @@ export class IgxGridStateBaseDirective {
221227
const hasColumnGroup = colState.columnGroup;
222228
delete colState.columnGroup;
223229
if (hasColumnGroup) {
224-
const ref1 = createComponent(IgxColumnGroupComponent, { environmentInjector: this.envInjector, elementInjector: this.injector });
225-
Object.assign(ref1.instance, colState);
226-
ref1.instance.grid = context.currGrid;
227-
if (ref1.instance.parent) {
228-
const columnGroup: IgxColumnGroupComponent = newColumns.find(e => e.header === ref1.instance.parent && e.columnGroup);
229-
columnGroup.children.reset([...columnGroup.children.toArray(), ref1.instance]);
230-
ref1.instance.parent = columnGroup;
230+
let ref1: IgxColumnGroupComponent = context.currGrid.columns.find(x => x.columnGroup && (colState.key ? this.getColumnGroupKey(x) === colState.key : x.header === colState.header)) as IgxColumnGroupComponent;
231+
if (!ref1) {
232+
const component = createComponent(IgxColumnGroupComponent, { environmentInjector: this.envInjector, elementInjector: this.injector });
233+
ref1 = component.instance;
234+
component.changeDetectorRef.detectChanges();
235+
} else {
236+
ref1.children.reset([]);
231237
}
232-
ref1.changeDetectorRef.detectChanges();
233-
newColumns.push(ref1.instance);
238+
Object.assign(ref1, colState);
239+
ref1.grid = context.currGrid;
240+
if (colState.parent || colState.parentKey) {
241+
const columnGroup: IgxColumnGroupComponent = newColumns.find(e => e.columnGroup && (e.key ? e.key === colState.parentKey : e.header === ref1.parent));
242+
columnGroup.children.reset([...columnGroup.children.toArray(), ref1]);
243+
ref1.parent = columnGroup;
244+
}
245+
ref1.cdr.detectChanges();
246+
newColumns.push(ref1);
234247
} else {
235-
const ref = createComponent(IgxColumnComponent, { environmentInjector: this.envInjector, elementInjector: this.injector});
236-
Object.assign(ref.instance, colState);
237-
ref.instance.grid = context.currGrid;
238-
if (ref.instance.parent) {
239-
const columnGroup: IgxColumnGroupComponent = newColumns.find(e => e.header === ref.instance.parent && e.columnGroup);
248+
let ref: IgxColumnComponent = context.currGrid.columns.find(x => !x.columnGroup && x.field === colState.field) as IgxColumnComponent;
249+
if (!ref) {
250+
const component = createComponent(IgxColumnComponent, { environmentInjector: this.envInjector, elementInjector: this.injector});
251+
ref = component.instance;
252+
component.changeDetectorRef.detectChanges();
253+
}
254+
255+
Object.assign(ref, colState);
256+
ref.grid = context.currGrid;
257+
if (colState.parent || colState.parentKey) {
258+
const columnGroup: IgxColumnGroupComponent = newColumns.find(e => e.columnGroup && (e.key ? e.key === colState.parentKey : e.header === ref.parent));
240259
if (columnGroup) {
241-
ref.instance.parent = columnGroup;
242-
columnGroup.children.reset([...columnGroup.children.toArray(), ref.instance]);
260+
ref.parent = columnGroup;
261+
columnGroup.children.reset([...columnGroup.children.toArray(), ref]);
243262
}
244263
}
245-
ref.changeDetectorRef.detectChanges();
246-
newColumns.push(ref.instance);
264+
ref.cdr.detectChanges();
265+
newColumns.push(ref);
247266
}
248267
});
249268
context.currGrid.updateColumns(newColumns);
@@ -722,6 +741,10 @@ export class IgxGridStateBaseDirective {
722741
return val;
723742
}
724743

744+
private getColumnGroupKey(columnGroup: ColumnType) : string {
745+
return columnGroup.childColumns.map(x => x.columnGroup ? x.level + "_" + this.getColumnGroupKey(x) : x.field).sort().join("_");
746+
}
747+
725748
private getFeature(key: string): Feature {
726749
const feature: Feature = this.FEATURES[key];
727750
return feature;

0 commit comments

Comments
 (0)