Skip to content

Commit 5ea1e3c

Browse files
committed
fix(TreeGrid): Preserve the original order in the collection when hierarchizing.
1 parent 8af9a8a commit 5ea1e3c

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid-integration.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,10 @@ describe('IgxTreeGrid - Integration #tGrid', () => {
199199
});
200200

201201
it('should preserve the order of records on inner levels', () => {
202+
fix = TestBed.createComponent(IgxTreeGridPrimaryForeignKeyComponent);
202203
fix.componentInstance.sortByName = true;
203204
fix.detectChanges();
204-
treeGrid.columns[0].hidden = true;
205-
treeGrid.columns[1].hidden = true;
206-
fix.detectChanges();
205+
treeGrid = fix.componentInstance.treeGrid;
207206

208207
const expectedFlatData = [
209208
{

projects/igniteui-angular/src/lib/grids/tree-grid/tree-grid.pipes.ts

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,44 @@ export class IgxTreeGridHierarchizingPipe implements PipeTransform {
5353
return primaryKey ? rowData[primaryKey] : rowData;
5454
}
5555

56-
private hierarchizeFlatData(collection: any[], primaryKey: string, foreignKey: string,
57-
map: Map<any, ITreeGridRecord>, flatData: any[]):
58-
ITreeGridRecord[] {
59-
const result: ITreeGridRecord[] = [];
60-
const missingParentRecords: ITreeGridRecord[] = [];
56+
/**
57+
* Converts a flat array of data into a hierarchical (tree) structure,
58+
* preserving the original order of the records among siblings.
59+
*
60+
* It uses a two-pass approach:
61+
* 1. Creates all ITreeGridRecord objects and populates the Map for quick lookup.
62+
* 2. Links the records by iterating again, ensuring children are added to
63+
* their parent's children array in the order they appeared in the
64+
* original collection.
65+
*
66+
* @param collection The flat array of data to be hierarchized. This is the array whose order should be preserved.
67+
* @param primaryKey The name of the property in the data objects that serves as the unique identifier (e.g., 'id').
68+
* @param foreignKey The name of the property in the data objects that links to the parent's primary key (e.g., 'parentId').
69+
* @param map A pre-existing Map object (key: primaryKey value, value: ITreeGridRecord) used to store and quickly look up all created records.
70+
* @param flatData The original flat data array. Used for passing to the setIndentationLevels method (not directly used for hierarchy building).
71+
* @returns An array of ITreeGridRecord objects representing the root nodes of the hierarchy, ordered as they appeared in the original collection.
72+
*/
73+
private hierarchizeFlatData(
74+
collection: any[],
75+
primaryKey: string,
76+
foreignKey: string,
77+
map: Map<any, ITreeGridRecord>,
78+
flatData: any[]
79+
): ITreeGridRecord[] {
6180
collection.forEach(row => {
6281
const record: ITreeGridRecord = {
6382
key: this.getRowID(primaryKey, row),
6483
data: row,
6584
children: []
6685
};
67-
const parent = map.get(row[foreignKey]);
68-
if (parent) {
69-
record.parent = parent;
70-
parent.children.push(record);
71-
} else {
72-
missingParentRecords.push(record);
73-
}
74-
7586
map.set(row[primaryKey], record);
7687
});
7788

78-
missingParentRecords.forEach(record => {
79-
const parent = map.get(record.data[foreignKey]);
89+
const result: ITreeGridRecord[] = [];
90+
collection.forEach(row => {
91+
const record: ITreeGridRecord = map.get(row[primaryKey])!;
92+
const parent = map.get(row[foreignKey]);
93+
8094
if (parent) {
8195
record.parent = parent;
8296
parent.children.push(record);

0 commit comments

Comments
 (0)