Skip to content

Commit 5ea2a18

Browse files
MayaKirovakdinev
andauthored
fix(hgrid): add and use lifecycle placeholder for grid connection eve… (#17242)
* fix(hgrid): add and use lifecycle placeholder for grid connection events. * chore(*): Fix virtualization issue with change detection. * chore(*): Apply review comments. * chore(*): Remove duplicate detectChange. * chore(*): Remove the other duplicate detect. --------- Co-authored-by: Konstantin Dinev <kdinev@infragistics.com>
1 parent 638b702 commit 5ea2a18

5 files changed

Lines changed: 65 additions & 15 deletions

File tree

projects/igniteui-angular/directives/src/directives/for-of/for_of.directive.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,6 @@ export class IgxForOfDirective<T, U extends T[] = T[]> extends IgxForOfToken<T,U
10651065
this.scrollFocus(embView.rootNodes.find(node => node.nodeType === Node.ELEMENT_NODE)
10661066
|| embView.rootNodes[0].nextElementSibling);
10671067
const view = container.detach(0);
1068-
// embView and view both refer to the same collections
10691068
this.updateTemplateContext(embView.context, i);
10701069

10711070
// Because in Elements the whole parent div (containing data-index) gets removed (possibly due to being disconnected). In Angular it just gets moved.
@@ -1091,7 +1090,6 @@ export class IgxForOfDirective<T, U extends T[] = T[]> extends IgxForOfToken<T,U
10911090
|| embView.rootNodes[0].nextElementSibling);
10921091
// embView and view both refer to the same collections
10931092
const view = container.detach(container.length - 1);
1094-
10951093
this.updateTemplateContext(embView.context, i);
10961094
view.detectChanges();
10971095

projects/igniteui-angular/grids/hierarchical-grid/src/hierarchical-grid.component.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,8 @@
276276
<ng-content select="igx-column,igc-column,igx-column-group,igc-column-group,igx-action-strip,igc-action-strip"></ng-content>
277277
<ng-content select="igx-row-island,igc-row-island"></ng-content>
278278
}
279+
280+
<!-- Lifecycle placeholder used to determine when the grid gets connected/disconnected from the DOM, for example as a result of virtualization or caching. -->
281+
<igc-lifecycle-placeholder (igcConnected)="onLifecyclePlaceholderConnected()"
282+
(igcDisconnected)="onLifecyclePlaceholderDisconnected()">
283+
</igc-lifecycle-placeholder>

projects/igniteui-angular/grids/hierarchical-grid/src/hierarchical-grid.component.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { IgxIconComponent } from 'igniteui-angular/icon';
3636
import { EntityType, FieldType, IFilteringExpressionsTree, IgxActionStripToken, IgxOverlayOutletDirective, flatten, IGridResourceStrings } from 'igniteui-angular/core';
3737
import { IgxPaginatorToken } from 'igniteui-angular/paginator';
3838
import { IgxGridCellMergePipe, IgxGridComponent, IgxGridFilteringPipe, IgxGridSortingPipe, IgxGridUnmergeActivePipe } from 'igniteui-angular/grids/grid';
39+
import { registerLifecyclePlaceholderElement } from './lifecycle-placeholder-element';
3940

4041
let NEXT_ID = 0;
4142

@@ -662,6 +663,9 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
662663
* @hidden
663664
*/
664665
public override ngOnInit() {
666+
if (this.platform.isBrowser) {
667+
registerLifecyclePlaceholderElement();
668+
}
665669
// this.expansionStatesChange.pipe(takeUntil(this.destroy$)).subscribe((value: Map<any, boolean>) => {
666670
// const res = Array.from(value.entries()).filter(({1: v}) => v === true).map(([k]) => k);
667671
// });
@@ -674,6 +678,26 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
674678
super.ngOnInit();
675679
}
676680

681+
// Event that triggers when element gets connected back to the DOM.
682+
// Used to determine when to reopen a previously closed row editing overlay.
683+
protected onLifecyclePlaceholderConnected(): void {
684+
if (this.rowEditable && this.crudService.rowInEditMode && this.rowEditingOverlay &&
685+
this.rowEditingOverlay.collapsed) {
686+
// Row is in edit mode, but overlay is closed - reopen.
687+
this.openRowOverlay(this.crudService.rowInEditMode.id);
688+
}
689+
690+
}
691+
692+
// Event that triggers when element gets disconnected from the DOM, for example as a result of virtualization or caching.
693+
// Used to determine when to close the row editing overlay.
694+
protected onLifecyclePlaceholderDisconnected(): void {
695+
if (this.rowEditable && this.crudService.rowInEditMode && this.rowEditingOverlay) {
696+
// disconnected from DOM (possibly cached) & row was in edit mode - close overlay.
697+
this.closeRowEditingOverlay();
698+
}
699+
}
700+
677701
/**
678702
* @hidden
679703
*/
@@ -1254,15 +1278,15 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
12541278
{
12551279
name: null,
12561280
fields: filterableFields.map(f => ({
1257-
field: f.field,
1258-
dataType: f.dataType,
1259-
header: f.header,
1260-
editorOptions: f.editorOptions,
1261-
filters: f.filters,
1262-
pipeArgs: f.pipeArgs,
1263-
defaultTimeFormat: f.defaultTimeFormat,
1264-
defaultDateTimeFormat: f.defaultDateTimeFormat
1265-
})) as FieldType[]
1281+
field: f.field,
1282+
dataType: f.dataType,
1283+
header: f.header,
1284+
editorOptions: f.editorOptions,
1285+
filters: f.filters,
1286+
pipeArgs: f.pipeArgs,
1287+
defaultTimeFormat: f.defaultTimeFormat,
1288+
defaultDateTimeFormat: f.defaultDateTimeFormat
1289+
})) as FieldType[]
12661290
}
12671291
];
12681292

@@ -1271,7 +1295,7 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
12711295
this.data[0][rowIsland.key][0] : null;
12721296
return acc.concat(this.generateChildEntity(rowIsland, childFirstRowData));
12731297
}
1274-
, []);
1298+
, []);
12751299
}
12761300

12771301
return entities;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const LIFECYCLE_PLACEHOLDER_TAG = 'igc-lifecycle-placeholder';
2+
const LIFECYCLE_CONNECTED_EVENT = 'igcConnected';
3+
const LIFECYCLE_DISCONNECTED_EVENT = 'igcDisconnected';
4+
5+
/** @hidden @internal */
6+
export function registerLifecyclePlaceholderElement(): void {
7+
if (typeof customElements === 'undefined' || typeof HTMLElement === 'undefined' ||
8+
typeof CustomEvent === 'undefined' || customElements.get(LIFECYCLE_PLACEHOLDER_TAG)) {
9+
return;
10+
}
11+
12+
class LifecyclePlaceholderElement extends HTMLElement {
13+
public connectedCallback(): void {
14+
this.dispatchEvent(new CustomEvent(LIFECYCLE_CONNECTED_EVENT));
15+
}
16+
17+
public disconnectedCallback(): void {
18+
this.dispatchEvent(new CustomEvent(LIFECYCLE_DISCONNECTED_EVENT));
19+
}
20+
}
21+
22+
customElements.define(LIFECYCLE_PLACEHOLDER_TAG, LifecyclePlaceholderElement);
23+
}

src/app/hierarchical-grid/hierarchical-grid.sample.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ <h4 class="sample-title">Sample two</h4>
150150
<igx-grid-toolbar-exporter></igx-grid-toolbar-exporter>
151151
</igx-grid-toolbar-actions>
152152
</igx-grid-toolbar>
153-
<igx-row-island [key]="'childData'" [autoGenerate]="true" [rowSelection]='selectionMode' [batchEditing]="true" [rowEditable]="true"
153+
<igx-row-island [rowEditable]="true" [primaryKey]="'ID'" [key]="'childData'" [autoGenerate]="true" [rowSelection]='selectionMode' [batchEditing]="true" [rowEditable]="true"
154154
[allowFiltering]="true">
155-
<igx-row-island [key]="'childData'" [autoGenerate]="true" [rowSelection]='selectionMode' [batchEditing]="true" [rowEditable]="true"
155+
<igx-row-island [rowEditable]="true" [primaryKey]="'ID'" [key]="'childData'" [autoGenerate]="true" [rowSelection]='selectionMode' [batchEditing]="true" [rowEditable]="true"
156156
[allowFiltering]="true"></igx-row-island>
157157
</igx-row-island>
158-
<igx-row-island [key]="'childData2'" [autoGenerate]="true" [allowFiltering]="true"></igx-row-island>
158+
<igx-row-island [rowEditable]="true" [primaryKey]="'ID'" [key]="'childData2'" [autoGenerate]="true" [allowFiltering]="true"></igx-row-island>
159159
</igx-hierarchical-grid>
160160

161161

0 commit comments

Comments
 (0)