Skip to content

Commit 983aba7

Browse files
wnvkoLipata
authored andcommitted
fix(grid): disallow editing or primary column in grid with transactions #5377
1 parent 2a2a00d commit 983aba7

File tree

6 files changed

+183
-135
lines changed

6 files changed

+183
-135
lines changed

projects/igniteui-angular/src/lib/core/grid-selection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ export class IgxGridCRUDService {
105105
return new IgxRow(cell.id.rowID, cell.rowIndex, cell.rowData);
106106
}
107107

108-
sameRow(rowID): boolean {
109-
return this.row && this.row.id === rowID;
108+
sameRow(rowIndex): boolean {
109+
return this.row && this.row.index === rowIndex;
110110
}
111111

112112
sameCell(cell: IgxCell): boolean {
@@ -175,7 +175,7 @@ export class IgxGridCRUDService {
175175
return;
176176
}
177177

178-
if (this.row && !this.sameRow(this.cell.id.rowID)) {
178+
if (this.row && !this.sameRow(this.cell.rowIndex)) {
179179
this.grid.endEdit(true);
180180
this.cell = this.createCell(cell);
181181
this.beginRowEdit();

projects/igniteui-angular/src/lib/grids/cell.component.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,6 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
584584
const editableCell = this.crudService.cell;
585585
const editMode = !!(crud.row || crud.cell);
586586

587-
588587
if (this.editable && editMode && !this.row.deleted) {
589588
if (editableCell) {
590589
this.gridAPI.update_cell(editableCell, editableCell.editValue);
@@ -595,9 +594,9 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
595594
return;
596595
}
597596

598-
if (editableCell && crud.sameRow(this.cellID.rowID)) {
597+
if (editableCell && crud.sameRow(this.rowIndex)) {
599598
this.gridAPI.submit_value();
600-
} else if (editMode && !crud.sameRow(this.cellID.rowID)) {
599+
} else if (editMode && !crud.sameRow(this.rowIndex)) {
601600
this.grid.endEdit(true);
602601
}
603602
}
@@ -979,14 +978,7 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy {
979978
}
980979

981980
if (this.editMode) {
982-
const v = this.crudService.cell;
983-
const args = {
984-
cellID: v.id,
985-
rowID: v.id.rowID,
986-
oldValue: v.value,
987-
newValue: v.editValue,
988-
cancel: false
989-
} as IGridEditEventArgs;
981+
const args = this.crudService.cell.createEditEventArgs();
990982
this.grid.onCellEditCancel.emit(args);
991983
if (args.cancel) {
992984
return;

projects/igniteui-angular/src/lib/grids/column.component.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,20 @@ export class IgxColumnComponent implements AfterContentInit {
126126
*/
127127
@Input()
128128
get editable(): boolean {
129-
let result = false;
129+
// Updating the primary key when grid has transactions (incl. row edit)
130+
// should not be allowed, as that can corrupt transaction state.
131+
const rowEditable = this.grid && this.grid.rowEditable;
132+
const hasTransactions = this.grid && this.grid.transactions.enabled;
133+
134+
if (this.isPrimaryColumn && (rowEditable || hasTransactions)) {
135+
return false;
136+
}
137+
130138
if (this._editable !== undefined) {
131-
result = this._editable;
139+
return this._editable;
132140
} else {
133-
result = this.grid && this.grid.rowEditable && this.field !== this.grid.primaryKey;
141+
return rowEditable;
134142
}
135-
return result;
136143
}
137144
/**
138145
* Sets whether the column is editable.
@@ -1085,6 +1092,12 @@ export class IgxColumnComponent implements AfterContentInit {
10851092
* @hidden
10861093
*/
10871094
protected _editable: boolean;
1095+
/**
1096+
* @hidden
1097+
*/
1098+
protected get isPrimaryColumn(): boolean {
1099+
return this.field !== undefined && this.grid !== undefined && this.field === this.grid.primaryKey;
1100+
}
10881101
/**
10891102
*@hidden
10901103
*/

projects/igniteui-angular/src/lib/grids/grid/grid-mrl-keyboard-nav.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2883,7 +2883,7 @@ describe('IgxGrid Multi Row Layout - Keyboard navigation', () => {
28832883
await wait(100);
28842884
fix.detectChanges();
28852885

2886-
targetCell = grid.getCellByColumn(0, 'ID');
2886+
targetCell = grid.getCellByColumn(0, 'PostalCode');
28872887
expect(targetCell.focused).toBe(true);
28882888
});
28892889

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,6 @@ describe('IgxGrid - Summaries', () => {
711711
await wait(30);
712712
fixture.detectChanges();
713713
const grid = fixture.componentInstance.grid;
714-
grid.getColumnByName('ID').editable = true;
715714
grid.getColumnByName('ParentID').editable = true;
716715
fixture.detectChanges();
717716
grid.rowEditable = true;
@@ -735,18 +734,14 @@ describe('IgxGrid - Summaries', () => {
735734

736735
const editTemplate = fixture.debugElement.query(By.css('input[type=\'number\']'));
737736
UIInteractions.sendInput(editTemplate, 87);
738-
fixture.detectChanges();
739-
740-
UIInteractions.triggerKeyDownEvtUponElem('tab', cell.nativeElement, true, false, true);
741737
await wait(50);
742738
fixture.detectChanges();
743739

744740
summaryRow = fixture.debugElement.query(By.css(SUMMARY_ROW));
745741
HelperUtils.verifyColumnSummaries(summaryRow, 1,
746742
['Count', 'Min', 'Max', 'Sum', 'Avg'], ['1', '17', '17', '17', '17']);
747743

748-
const idCell = grid.getCellByColumn(1, 'ID');
749-
UIInteractions.triggerKeyDownEvtUponElem('enter', idCell.nativeElement, true);
744+
UIInteractions.triggerKeyDownEvtUponElem('enter', cell.nativeElement, true);
750745
await wait(50);
751746
fixture.detectChanges();
752747

0 commit comments

Comments
 (0)