Skip to content

Commit 9221089

Browse files
authored
Fixed tree grid records' order after hierarchizing. (#16329)
* fix(TreeGrid): Preserve the original order in the collection when hierarchizing + a unit test.
1 parent 1c1dc75 commit 9221089

File tree

3 files changed

+105
-17
lines changed

3 files changed

+105
-17
lines changed

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,73 @@ describe('IgxTreeGrid - Integration #tGrid', () => {
198198
treeGrid = fix.componentInstance.treeGrid;
199199
});
200200

201+
it('should preserve the order of records on inner levels', () => {
202+
fix = TestBed.createComponent(IgxTreeGridPrimaryForeignKeyComponent);
203+
fix.componentInstance.sortByName = true;
204+
fix.detectChanges();
205+
treeGrid = fix.componentInstance.treeGrid;
206+
207+
const expectedFlatData = [
208+
{
209+
"ID": 1,
210+
"ParentID": -1,
211+
"Name": "Casey Houston",
212+
"JobTitle": "Vice President",
213+
"Age": 32
214+
},
215+
{
216+
"ID": 2,
217+
"ParentID": 1,
218+
"Name": "Gilberto Todd",
219+
"JobTitle": "Director",
220+
"Age": 41
221+
},
222+
{
223+
"ID": 7,
224+
"ParentID": 2,
225+
"Name": "Debra Morton",
226+
"JobTitle": "Associate Software Developer",
227+
"Age": 35
228+
},
229+
{
230+
"ID": 3,
231+
"ParentID": 2,
232+
"Name": "Tanya Bennett",
233+
"JobTitle": "Director",
234+
"Age": 29
235+
},
236+
{
237+
"ID": 4,
238+
"ParentID": 1,
239+
"Name": "Jack Simon",
240+
"JobTitle": "Software Developer",
241+
"Age": 33
242+
},
243+
{
244+
"ID": 10,
245+
"ParentID": -1,
246+
"Name": "Eduardo Ramirez",
247+
"JobTitle": "Manager",
248+
"Age": 53
249+
},
250+
{
251+
"ID": 9,
252+
"ParentID": 10,
253+
"Name": "Leslie Hansen",
254+
"JobTitle": "Associate Software Developer",
255+
"Age": 44
256+
},
257+
{
258+
"ID": 6,
259+
"ParentID": -1,
260+
"Name": "Erma Walsh",
261+
"JobTitle": "CEO",
262+
"Age": 52
263+
},
264+
]
265+
expect(treeGrid.flatData).toEqual(expectedFlatData);
266+
});
267+
201268
it('should transform a non-tree column into a tree column when pinning it', () => {
202269
TreeGridFunctions.verifyTreeColumn(fix, 'ID', 5);
203270

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);

projects/igniteui-angular/src/lib/test-utils/tree-grid-components.spec.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,17 @@ export class IgxTreeGridWithNoScrollsComponent {
159159
`,
160160
imports: [IgxTreeGridComponent, IgxColumnComponent, IgxPaginatorComponent]
161161
})
162-
export class IgxTreeGridPrimaryForeignKeyComponent {
162+
export class IgxTreeGridPrimaryForeignKeyComponent implements OnInit {
163163
@ViewChild(IgxTreeGridComponent, { static: true }) public treeGrid: IgxTreeGridComponent;
164-
public data = SampleTestData.employeePrimaryForeignKeyTreeData();
164+
public data = [];
165165
public paging = false;
166+
public sortByName = false;
167+
168+
public ngOnInit(): void {
169+
this.data = !this.sortByName
170+
? SampleTestData.employeePrimaryForeignKeyTreeData()
171+
: SampleTestData.employeePrimaryForeignKeyTreeData().sort((a, b) => a.Name.localeCompare(b.Name));
172+
}
166173
}
167174

168175
@Component({

0 commit comments

Comments
 (0)