diff --git a/lib/core/src/lib/datatable/components/datatable/datatable.component.spec.ts b/lib/core/src/lib/datatable/components/datatable/datatable.component.spec.ts index 195ad20b69..128f8a9ecf 100644 --- a/lib/core/src/lib/datatable/components/datatable/datatable.component.spec.ts +++ b/lib/core/src/lib/datatable/components/datatable/datatable.component.spec.ts @@ -1345,6 +1345,34 @@ describe('DataTable', () => { expect(idColumn.innerText).toContain('ID'); expect(nameColumn.innerText).toContain('CUSTOM HEADER'); }); + + it('should set isContextMenuSource to true for row whose id matches selectedRowId', () => { + const rows = [{ + id: '1234', + isContextMenuSource: false + }, { + id: '2345', + isContextMenuSource: false + }, { + id: '3456', + isContextMenuSource: false + }] as DataRow[]; + const row = { + id: '2345', + isContextMenuSource: false + } as DataRow; + dataTable.data = new ObjectDataTableAdapter( + rows, + [new ObjectDataColumn({ key: 'id' }), + new ObjectDataColumn({ key: 'isContextMenuSource' })] + ); + + dataTable.markRowAsContextMenuSource(row); + fixture.detectChanges(); + + expect(dataTable.selectedRowId).toEqual('2345'); + expect(row.isContextMenuSource).toBeTrue(); + }); }); describe('Accesibility', () => { diff --git a/lib/core/src/lib/datatable/components/datatable/datatable.component.ts b/lib/core/src/lib/datatable/components/datatable/datatable.component.ts index 7d553d74b9..737c3d9620 100644 --- a/lib/core/src/lib/datatable/components/datatable/datatable.component.ts +++ b/lib/core/src/lib/datatable/components/datatable/datatable.component.ts @@ -226,6 +226,7 @@ export class DataTableComponent implements OnInit, AfterContentInit, OnChanges, isSelectAllIndeterminate: boolean = false; isSelectAllChecked: boolean = false; selection = new Array(); + selectedRowId: string = ''; isDraggingHeaderColumn = false; hoveredHeaderColumnIndex = -1; @@ -287,6 +288,9 @@ export class DataTableComponent implements OnInit, AfterContentInit, OnChanges, ngOnChanges(changes: SimpleChanges) { this.initAndSubscribeClickStream(); + if(this.selectedRowId) { + this.setRowAsContextSource(); + } const dataChanges = changes['data']; const rowChanges = changes['rows']; @@ -782,10 +786,18 @@ export class DataTableComponent implements OnInit, AfterContentInit, OnChanges, } markRowAsContextMenuSource(selectedRow: DataRow): void { + this.selectedRowId = selectedRow.id ? selectedRow.id : ''; this.data.getRows().forEach((row) => row.isContextMenuSource = false); selectedRow.isContextMenuSource = true; } + private setRowAsContextSource(): void { + const selectedRow = this.data.getRows().find((row) => this.selectedRowId === row.id); + if(selectedRow) { + selectedRow.isContextMenuSource = true; + } + } + getSortingKey(): string | null { if (this.data.getSorting()) { return this.data.getSorting().key;