@@ -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