Skip to content

Commit be5c3e0

Browse files
authored
Merge branch 'master' into sstoychev/focus-changes-editing
2 parents ee575d6 + 396130c commit be5c3e0

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,14 @@ export abstract class IgxGridBaseDirective implements GridType,
440440
*/
441441
@WatchChanges()
442442
@Input()
443-
public primaryKey: string;
443+
public get primaryKey(): string {
444+
return this._primaryKey;
445+
}
446+
447+
public set primaryKey(value: string) {
448+
this._primaryKey = value;
449+
this.checkPrimaryKeyColumn();
450+
}
444451

445452
/* blazorSuppress */
446453
/**
@@ -3132,6 +3139,7 @@ export abstract class IgxGridBaseDirective implements GridType,
31323139
return this.verticalScrollContainer.getScrollNativeSize();
31333140
}
31343141

3142+
private _primaryKey: string;
31353143
private _rowEditable = false;
31363144
private _currentRowState: any;
31373145
private _filteredSortedData = null;
@@ -6650,6 +6658,7 @@ export abstract class IgxGridBaseDirective implements GridType,
66506658

66516659
this.initColumns(this._columns, (col: IgxColumnComponent) => this.columnInit.emit(col));
66526660
this.columnListDiffer.diff(this.columnList);
6661+
this.checkPrimaryKeyColumn();
66536662

66546663
this.columnList.changes
66556664
.pipe(takeUntil(this.destroy$))
@@ -6761,6 +6770,16 @@ export abstract class IgxGridBaseDirective implements GridType,
67616770
if (added || removed) {
67626771
this.onColumnsAddedOrRemoved();
67636772
}
6773+
this.checkPrimaryKeyColumn();
6774+
}
6775+
}
6776+
6777+
/**
6778+
* @hidden @internal
6779+
*/
6780+
protected checkPrimaryKeyColumn() {
6781+
if (this.primaryKey && this.columns.length > 0 && !this.columns.find(c => c.field === this.primaryKey)) {
6782+
console.warn(`Primary key column "${this.primaryKey}" is not defined. Set \`primaryKey\` to a valid column.`);
67646783
}
67656784
}
67666785

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,40 @@ describe('IgxGrid Component Tests #grid', () => {
673673
const expectedCellHeight = 76; // rowHeight + 1px border
674674
expect(cell.offsetHeight).toEqual(expectedCellHeight);
675675
});
676+
677+
it('should throw a warning when primaryKey is set to a non-existing column field', () => {
678+
const fixture = TestBed.createComponent(IgxGridTestComponent);
679+
const grid = fixture.componentInstance.grid;
680+
const warnSpy = spyOn(console, 'warn');
681+
grid.primaryKey = 'testField';
682+
fixture.detectChanges();
683+
684+
expect(console.warn).toHaveBeenCalledTimes(1);
685+
expect(console.warn).toHaveBeenCalledWith(
686+
`Primary key column "${grid.primaryKey}" is not defined. Set \`primaryKey\` to a valid column.`
687+
);
688+
warnSpy.calls.reset();
689+
690+
// update columns to include the 'testField'
691+
fixture.componentInstance.columns = [
692+
{ field: 'index', header: 'index', dataType: 'number', width: null, hasSummary: false },
693+
{ field: 'value', header: 'value', dataType: 'number', width: null, hasSummary: false },
694+
{ field: 'testField', header: 'testField', dataType: 'number', width: null, hasSummary: false }
695+
];
696+
fixture.detectChanges();
697+
698+
expect(console.warn).toHaveBeenCalledTimes(0);
699+
700+
// remove the 'testField' runtime
701+
fixture.componentInstance.columns.pop();
702+
fixture.componentInstance.columns = [...fixture.componentInstance.columns];
703+
fixture.detectChanges();
704+
705+
expect(console.warn).toHaveBeenCalledTimes(1);
706+
expect(console.warn).toHaveBeenCalledWith(
707+
`Primary key column "${grid.primaryKey}" is not defined. Set \`primaryKey\` to a valid column.`
708+
);
709+
});
676710
});
677711

678712
describe('IgxGrid - virtualization tests', () => {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,7 @@ export class IgxHierarchicalGridComponent extends IgxHierarchicalGridBaseDirecti
11391139
protected override setupColumns() {
11401140
if (this.parentIsland && this.parentIsland.childColumns.length > 0 && !this.autoGenerate) {
11411141
this.createColumnsList(this.parentIsland.childColumns.toArray());
1142+
super.checkPrimaryKeyColumn();
11421143
} else {
11431144
super.setupColumns();
11441145
}

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,35 @@ describe('Basic IgxHierarchicalGrid #hGrid', () => {
638638
fixture.detectChanges();
639639
expect(hierarchicalGrid.columnInit.emit).toHaveBeenCalled();
640640
});
641+
642+
it('should throw a warning when primaryKey is set to a non-existing column field', () => {
643+
spyOn(console, 'warn');
644+
hierarchicalGrid.primaryKey = 'testField';
645+
fixture.componentInstance.rowIsland.primaryKey = 'testField-rowIsland';
646+
fixture.componentInstance.rowIsland2.primaryKey = 'testField-rowIsland2';
647+
fixture.detectChanges();
648+
649+
expect(console.warn).toHaveBeenCalledWith(
650+
`Primary key column "${hierarchicalGrid.primaryKey}" is not defined. Set \`primaryKey\` to a valid column.`
651+
);
652+
653+
let row1 = hierarchicalGrid.gridAPI.get_row_by_index(0) as IgxHierarchicalRowComponent;
654+
UIInteractions.simulateClickAndSelectEvent(row1.expander);
655+
fixture.detectChanges();
656+
657+
expect(console.warn).toHaveBeenCalledWith(
658+
`Primary key column "${fixture.componentInstance.rowIsland.primaryKey}" is not defined. Set \`primaryKey\` to a valid column.`
659+
);
660+
661+
const secondLevelGrid = hierarchicalGrid.gridAPI.getChildGrids()[0];
662+
row1 = secondLevelGrid.gridAPI.get_row_by_index(0) as IgxHierarchicalRowComponent;
663+
UIInteractions.simulateClickAndSelectEvent(row1.expander);
664+
fixture.detectChanges();
665+
666+
expect(console.warn).toHaveBeenCalledWith(
667+
`Primary key column "${fixture.componentInstance.rowIsland2.primaryKey}" is not defined. Set \`primaryKey\` to a valid column.`
668+
);
669+
});
641670
});
642671

643672
describe('IgxHierarchicalGrid Row Islands #hGrid', () => {

0 commit comments

Comments
 (0)