diff --git a/lib/core/datatable/components/datatable/datatable.component.spec.ts b/lib/core/datatable/components/datatable/datatable.component.spec.ts index 55ef7317b4..41b2e51505 100644 --- a/lib/core/datatable/components/datatable/datatable.component.spec.ts +++ b/lib/core/datatable/components/datatable/datatable.component.spec.ts @@ -66,9 +66,9 @@ describe('DataTable', () => { fixture.destroy(); }); - it('should preserve the top-to-bottom selection order', () => { + it('should preserve the historical selection order', () => { dataTable.data = new ObjectDataTableAdapter( - [{ id: 1 }, { id: 2 }, { id: 3 }], + [{ id: 0 }, { id: 1 }, { id: 2 }], [ new ObjectDataColumn({ key: 'id' })] ); @@ -79,9 +79,9 @@ describe('DataTable', () => { dataTable.selectRow(rows[1], true); const selection = dataTable.selection; - expect(selection[0].getValue('id')).toBe(1); - expect(selection[1].getValue('id')).toBe(2); - expect(selection[2].getValue('id')).toBe(3); + expect(selection[0].getValue('id')).toBe(2); + expect(selection[1].getValue('id')).toBe(0); + expect(selection[2].getValue('id')).toBe(1); }); it('should update schema if columns change', fakeAsync(() => { diff --git a/lib/core/datatable/components/datatable/datatable.component.ts b/lib/core/datatable/components/datatable/datatable.component.ts index bdf7499cd4..7fce72ffa5 100644 --- a/lib/core/datatable/components/datatable/datatable.component.ts +++ b/lib/core/datatable/components/datatable/datatable.component.ts @@ -601,8 +601,16 @@ export class DataTableComponent implements AfterContentInit, OnChanges, DoCheck, selectRow(row: DataRow, value: boolean) { if (row) { row.isSelected = value; - const rows = this.data.getRows() || []; - this.selection = rows.filter(r => r.isSelected); + const idx = this.selection.indexOf(row); + if (value) { + if (idx < 0) { + this.selection.push(row); + } + } else { + if (idx > -1) { + this.selection.splice(idx, 1); + } + } } }