diff --git a/lib/core/datatable/components/datatable/datatable.component.spec.ts b/lib/core/datatable/components/datatable/datatable.component.spec.ts index f09cdcb485..6baa522cba 100644 --- a/lib/core/datatable/components/datatable/datatable.component.spec.ts +++ b/lib/core/datatable/components/datatable/datatable.component.spec.ts @@ -52,6 +52,24 @@ describe('DataTable', () => { fixture.destroy(); }); + it('should preserve the top-to-bottom selection order', () => { + dataTable.data = new ObjectDataTableAdapter( + [{ id: 1 }, { id: 2 }, { id: 3 }], + [ new ObjectDataColumn({ key: 'id' })] + ); + + const rows = dataTable.data.getRows(); + + dataTable.selectRow(rows[2], true); + dataTable.selectRow(rows[0], true); + 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); + }); + it('should update schema if columns change', fakeAsync(() => { dataTable.columnList = new DataColumnListComponent(); diff --git a/lib/core/datatable/components/datatable/datatable.component.ts b/lib/core/datatable/components/datatable/datatable.component.ts index 7868b18f6e..7bd4b36146 100644 --- a/lib/core/datatable/components/datatable/datatable.component.ts +++ b/lib/core/datatable/components/datatable/datatable.component.ts @@ -422,7 +422,7 @@ export class DataTableComponent implements AfterContentInit, OnChanges, DoCheck, if (rows && rows.length > 0) { rows.forEach(r => r.isSelected = false); } - this.selection.splice(0); + this.selection = []; } this.isSelectAllChecked = false; } @@ -613,17 +613,8 @@ export class DataTableComponent implements AfterContentInit, OnChanges, DoCheck, selectRow(row: DataRow, value: boolean) { if (row) { row.isSelected = value; - const idx = this.selection.indexOf(row); - - if (value) { - if (idx < 0) { - this.selection.push(row); - } - } else { - if (idx > -1) { - this.selection.splice(idx, 1); - } - } + const rows = this.data.getRows() || []; + this.selection = rows.filter(r => r.isSelected); } }