Skip to content

Commit aa0b83f

Browse files
authored
Check for primary key when verifying expansion state (#16798)
1 parent c3cb139 commit aa0b83f

3 files changed

Lines changed: 95 additions & 8 deletions

File tree

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

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

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

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

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

634634
for (const entry of records) {
635-
const expansionStateVal = grid.expansionStates.has(entry) ? grid.expansionStates.get(entry) : grid.getDefaultExpandState(entry);
635+
const rowKey = grid.primaryKey ? entry[grid.primaryKey] : entry;
636+
const expansionStateVal = grid.expansionStates.has(rowKey) ? grid.expansionStates.get(rowKey) : grid.getDefaultExpandState(entry);
636637

637638
const dataWithoutChildren = Object.keys(entry)
638639
.filter(k => columnFields.includes(k))
@@ -653,8 +654,8 @@ export abstract class IgxBaseExporter {
653654

654655
for (const island of childLayoutList) {
655656
const path: IPathSegment = {
656-
rowID: island.primaryKey ? entry[island.primaryKey] : entry,
657-
rowKey: island.primaryKey ? entry[island.primaryKey] : entry,
657+
rowID: grid.primaryKey ? entry[grid.primaryKey] : entry,
658+
rowKey: grid.primaryKey ? entry[grid.primaryKey] : entry,
658659
rowIslandKey: island.key
659660
};
660661

@@ -793,24 +794,27 @@ export abstract class IgxBaseExporter {
793794
this.flatRecords.push(exportRecord);
794795

795796
if (island.children.length > 0) {
797+
const islandRowKey = grid?.primaryKey ? rec[grid.primaryKey] : rec;
796798
const islandExpansionStateVal = grid === undefined ?
797799
false :
798-
grid.expansionStates.has(rec) ?
799-
grid.expansionStates.get(rec) :
800+
grid.expansionStates.has(islandRowKey) ?
801+
grid.expansionStates.get(islandRowKey) :
800802
false;
801803

802804
for (const childIsland of island.children) {
803805
const path: IPathSegment = {
804-
rowID: childIsland.primaryKey ? rec[childIsland.primaryKey] : rec,
805-
rowKey: childIsland.primaryKey ? rec[childIsland.primaryKey] : rec,
806+
rowID: grid?.primaryKey ? rec[grid.primaryKey] : rec,
807+
rowKey: grid?.primaryKey ? rec[grid.primaryKey] : rec,
806808
rowIslandKey: childIsland.key
807809
};
808810

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

813-
this.getAllChildColumnsAndData(childIsland, keyRecordData, islandExpansionStateVal, childIslandGrid);
815+
// Children should only be visible if both parent and current row are expanded
816+
const combinedExpansionState = expansionStateVal && islandExpansionStateVal;
817+
this.getAllChildColumnsAndData(childIsland, keyRecordData, combinedExpansionState, childIslandGrid);
814818
}
815819
}
816820
}

projects/igniteui-angular/grids/core/src/services/pdf/pdf-exporter-grid.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,46 @@ describe('PDF Grid Exporter', () => {
372372
exporter.export(hGrid, options);
373373
});
374374

375+
it('should export hierarchical grid with expanded rows when using primaryKey', (done) => {
376+
const fix = TestBed.createComponent(IgxHierarchicalGridExportComponent);
377+
fix.detectChanges();
378+
379+
const hGrid = fix.componentInstance.hGrid;
380+
381+
// Set primary key on the grid
382+
hGrid.primaryKey = 'Artist';
383+
fix.detectChanges();
384+
385+
// Limit data for test performance
386+
hGrid.data = hGrid.data.slice(0, 1);
387+
fix.detectChanges();
388+
389+
const firstRowData = hGrid.data[0];
390+
391+
hGrid.toggleRow(firstRowData['Artist']);
392+
fix.detectChanges();
393+
394+
expect(hGrid.expansionStates.get(firstRowData['Artist'])).toBe(true);
395+
396+
const childGrids = hGrid.gridAPI.getChildGrids(false) as any[];
397+
expect(childGrids.length).toBeGreaterThan(0);
398+
399+
const firstChildGrid = childGrids[0];
400+
expect(firstChildGrid.data.length).toBeGreaterThan(0);
401+
402+
// Spy on drawDataRow to count exported rows
403+
const drawDataRowSpy = spyOn<any>(exporter as any, 'drawDataRow').and.callThrough();
404+
405+
exporter.exportEnded.pipe(first()).subscribe(() => {
406+
const minExpectedRows = 1 + firstChildGrid.data.length;
407+
expect(drawDataRowSpy.calls.count()).toBeGreaterThanOrEqual(minExpectedRows,
408+
'Child rows should be exported when parent is expanded via toggleRow with primaryKey');
409+
done();
410+
});
411+
412+
exporter.export(hGrid, options);
413+
});
414+
375415
it('should export tree grid with hierarchical data', (done) => {
376416
TestBed.configureTestingModule({
377417
imports: [

0 commit comments

Comments
 (0)