Skip to content

Commit 8faf374

Browse files
Merge pull request #17300 from IgniteUI/ganastasov/fix-17295-21.2.x
fix(hierarchical-grid): align selected data with expanded row indexes - 21.2.x
2 parents 31ebe43 + b504eea commit 8faf374

4 files changed

Lines changed: 60 additions & 3 deletions

File tree

projects/igniteui-angular-elements/src/analyzer/elements.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,7 @@ export var registerConfig = [
585585
methods: [
586586
"getRowByIndex",
587587
"getRowByKey",
588+
"getSelectedData",
588589
"getCellByColumn",
589590
"getCellByKey",
590591
"pinRow",
@@ -631,7 +632,6 @@ export var registerConfig = [
631632
"clearCellSelection",
632633
"selectRange",
633634
"getSelectedRanges",
634-
"getSelectedData",
635635
"selectedColumns",
636636
"selectColumns",
637637
"deselectColumns",

projects/igniteui-angular/grids/grid/src/grid-base.directive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7433,7 +7433,7 @@ export abstract class IgxGridBaseDirective implements GridType,
74337433
const keysAndData = [];
74347434
const activeEl = this.selectionService.activeElement;
74357435

7436-
if (this.type === 'hierarchical') {
7436+
if (this.type === 'hierarchical' && source === this.filteredSortedData) {
74377437
const expansionRowIndexes = [];
74387438
for (const [key, value] of this.expansionStates.entries()) {
74397439
if (value) {

projects/igniteui-angular/grids/hierarchical-grid/src/hierarchical-grid.component.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,29 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
802802
.reduce((a, b) => a.concat(b), []);
803803
}
804804

805+
/**
806+
*
807+
* Returns an array of the current cell selection in the form of `[{ column.field: cell.value }, ...]`.
808+
*
809+
* @remarks
810+
* If `formatters` is enabled, the cell value will be formatted by its respective column formatter (if any).
811+
* If `headers` is enabled, it will use the column header (if any) instead of the column field.
812+
*/
813+
public override getSelectedData(formatters = false, headers = false): any[] {
814+
const source: any[] = [];
815+
816+
const process = (record: any) => {
817+
if (this.isChildGridRecord(record)) {
818+
source.push(null);
819+
return;
820+
}
821+
source.push(this.isGhostRecord(record) || this.isRecordMerged(record) ? record.recordRef : record);
822+
};
823+
824+
this.dataView.forEach(process);
825+
return this.extractDataFromSelection(source, formatters, headers);
826+
}
827+
805828
/**
806829
* Returns a `CellType` object that matches the conditions.
807830
*
@@ -1165,7 +1188,6 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
11651188
super.initColumns(collection, cb);
11661189
}
11671190

1168-
11691191
protected override setupColumns() {
11701192
if (this.parentIsland && this.parentIsland.childColumns.length > 0 && !this.autoGenerate) {
11711193
this.createColumnsList(this.parentIsland.childColumns.toArray());

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { IgxColumnMovingDragDirective, IgxGridNavigationService } from 'igniteui
77
import { IgxHierarchicalRowComponent } from './hierarchical-row.component';
88
import { take } from 'rxjs/operators';
99
import {
10+
IgxHierarchicalGridEmptyDataExportComponent,
1011
IgxHierarchicalGridTestBaseComponent,
1112
IgxHierarchicalGridTestCustomToolbarComponent,
1213
IgxHierarchicalGridTestInputPaginatorComponent,
@@ -35,6 +36,7 @@ describe('IgxHierarchicalGrid Integration #hGrid', () => {
3536
TestBed.configureTestingModule({
3637
imports: [
3738
NoopAnimationsModule,
39+
IgxHierarchicalGridEmptyDataExportComponent,
3840
IgxHierarchicalGridTestBaseComponent,
3941
IgxHierarchicalGridTestCustomToolbarComponent,
4042
IgxHierarchicalGridWithTransactionProviderComponent,
@@ -162,6 +164,39 @@ describe('IgxHierarchicalGrid Integration #hGrid', () => {
162164
expect(fChildCell.selected).toBeFalsy();
163165
expect(fCell.selected).toBeTruthy();
164166
}));
167+
168+
it('should not copy the previous row value from an expanded parent row', fakeAsync(() => {
169+
const singersFixture = TestBed.createComponent(IgxHierarchicalGridEmptyDataExportComponent);
170+
const singersData = SampleTestData.hierarchicalGridSingersFullData();
171+
(singersFixture.componentInstance as { data: unknown[] }).data = singersData;
172+
singersFixture.detectChanges();
173+
174+
const grid = singersFixture.componentInstance.hGrid;
175+
176+
const previousArtist = singersData[1].Artist;
177+
const targetArtist = singersData[2].Artist;
178+
const targetRow = grid.dataRowList.toArray()
179+
.find(row => row.data.Artist === targetArtist) as IgxHierarchicalRowComponent | undefined;
180+
181+
expect(targetRow).toBeDefined();
182+
targetRow!.toggle();
183+
tick(DEBOUNCE_TIME);
184+
singersFixture.detectChanges();
185+
186+
grid.selectRange({
187+
rowStart: targetRow!.index,
188+
rowEnd: targetRow!.index,
189+
columnStart: 'Artist',
190+
columnEnd: 'Artist'
191+
});
192+
singersFixture.detectChanges();
193+
194+
expect(targetRow!.expanded).toBeTruthy();
195+
196+
const selectedData = grid.getSelectedData();
197+
expect(selectedData).toEqual([{ Artist: targetArtist }]);
198+
expect(selectedData[0].Artist).not.toBe(previousArtist);
199+
}));
165200
});
166201

167202
describe('Updating', () => {

0 commit comments

Comments
 (0)