Skip to content

Commit f8215a0

Browse files
authored
Check for primary key when verifying expansion state - 20.1.x (#16801)
1 parent 8edc5c2 commit f8215a0

2 files changed

Lines changed: 55 additions & 8 deletions

File tree

projects/igniteui-angular/src/lib/services/excel/excel-exporter-grid.spec.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,49 @@ describe('Excel Exporter', () => {
897897
await exportAndVerify(hGrid, options, actualData.exportHierarchicalDataWithExpandedRows);
898898
});
899899

900+
it('should export hierarchical grid with expanded rows when using primaryKey', async () => {
901+
hGrid.primaryKey = 'Artist';
902+
fix.detectChanges();
903+
904+
const firstRow = hGrid.gridAPI.get_row_by_index(0) as IgxHierarchicalRowComponent;
905+
const secondRow = hGrid.gridAPI.get_row_by_index(1) as IgxHierarchicalRowComponent;
906+
907+
UIInteractions.simulateClickAndSelectEvent(firstRow.expander);
908+
fix.detectChanges();
909+
expect(firstRow.expanded).toBe(true);
910+
911+
let childGrids = hGrid.gridAPI.getChildGrids(false);
912+
913+
const firstChildGrid = childGrids[0];
914+
const firstChildRow = firstChildGrid.gridAPI.get_row_by_index(2) as IgxHierarchicalRowComponent;
915+
916+
UIInteractions.simulateClickAndSelectEvent(firstChildRow.expander);
917+
fix.detectChanges();
918+
expect(firstChildRow.expanded).toBe(true);
919+
920+
const secondChildGrid = childGrids[1];
921+
const secondChildRow = secondChildGrid.gridAPI.get_row_by_index(0) as IgxHierarchicalRowComponent;
922+
923+
UIInteractions.simulateClickAndSelectEvent(secondChildRow.expander);
924+
fix.detectChanges();
925+
expect(secondChildRow.expanded).toBe(true);
926+
927+
UIInteractions.simulateClickAndSelectEvent(secondRow.expander);
928+
fix.detectChanges();
929+
expect(secondRow.expanded).toBe(true);
930+
931+
childGrids = hGrid.gridAPI.getChildGrids(false);
932+
933+
const thirdChildGrid = childGrids[3];
934+
const thirdChildRow = thirdChildGrid.gridAPI.get_row_by_index(0) as IgxHierarchicalRowComponent;
935+
936+
UIInteractions.simulateClickAndSelectEvent(thirdChildRow.expander);
937+
fix.detectChanges();
938+
expect(thirdChildRow.expanded).toBe(true);
939+
940+
await exportAndVerify(hGrid, options, actualData.exportHierarchicalDataWithExpandedRows);
941+
});
942+
900943
it('should export hierarchical grid data with frozen headers', async () => {
901944
options.freezeHeaders = true;
902945
fix.detectChanges();

projects/igniteui-angular/src/lib/services/exporter-common/base-export-service.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ export abstract class IgxBaseExporter {
644644
const columnFields = this._ownersMap.get(grid).columns.map(col => col.field);
645645

646646
for (const entry of records) {
647-
const expansionStateVal = grid.expansionStates.has(entry) ? grid.expansionStates.get(entry) : false;
647+
const rowKey = grid.primaryKey ? entry[grid.primaryKey] : entry;
648+
const expansionStateVal = grid.expansionStates.has(rowKey) ? grid.expansionStates.get(rowKey) : grid.getDefaultExpandState(entry);
648649

649650
const dataWithoutChildren = Object.keys(entry)
650651
.filter(k => columnFields.includes(k))
@@ -665,8 +666,8 @@ export abstract class IgxBaseExporter {
665666

666667
for (const island of childLayoutList) {
667668
const path: IPathSegment = {
668-
rowID: island.primaryKey ? entry[island.primaryKey] : entry,
669-
rowKey: island.primaryKey ? entry[island.primaryKey] : entry,
669+
rowID: grid.primaryKey ? entry[grid.primaryKey] : entry,
670+
rowKey: grid.primaryKey ? entry[grid.primaryKey] : entry,
670671
rowIslandKey: island.key
671672
};
672673

@@ -805,24 +806,27 @@ export abstract class IgxBaseExporter {
805806
this.flatRecords.push(exportRecord);
806807

807808
if (island.children.length > 0) {
809+
const islandRowKey = grid?.primaryKey ? rec[grid.primaryKey] : rec;
808810
const islandExpansionStateVal = grid === undefined ?
809811
false :
810-
grid.expansionStates.has(rec) ?
811-
grid.expansionStates.get(rec) :
812+
grid.expansionStates.has(islandRowKey) ?
813+
grid.expansionStates.get(islandRowKey) :
812814
false;
813815

814816
for (const childIsland of island.children) {
815817
const path: IPathSegment = {
816-
rowID: childIsland.primaryKey ? rec[childIsland.primaryKey] : rec,
817-
rowKey: childIsland.primaryKey ? rec[childIsland.primaryKey] : rec,
818+
rowID: grid?.primaryKey ? rec[grid.primaryKey] : rec,
819+
rowKey: grid?.primaryKey ? rec[grid.primaryKey] : rec,
818820
rowIslandKey: childIsland.key
819821
};
820822

821823
// only defined when row is expanded in UI
822824
const childIslandGrid = grid?.gridAPI.getChildGrid([path]);
823825
const keyRecordData = this.prepareIslandData(island, childIslandGrid, rec[childIsland.key]) || [];
824826

825-
this.getAllChildColumnsAndData(childIsland, keyRecordData, islandExpansionStateVal, childIslandGrid);
827+
// Children should only be visible if both parent and current row are expanded
828+
const combinedExpansionState = expansionStateVal && islandExpansionStateVal;
829+
this.getAllChildColumnsAndData(childIsland, keyRecordData, combinedExpansionState, childIslandGrid);
826830
}
827831
}
828832
}

0 commit comments

Comments
 (0)