restore historical selection order (#3467)

* restore historical selection order

* fix test
This commit is contained in:
Denys Vuika
2018-06-11 11:15:09 +01:00
committed by Eugenio Romano
parent d152c367aa
commit 1d69f5c407
2 changed files with 15 additions and 7 deletions

View File

@@ -66,9 +66,9 @@ describe('DataTable', () => {
fixture.destroy(); fixture.destroy();
}); });
it('should preserve the top-to-bottom selection order', () => { it('should preserve the historical selection order', () => {
dataTable.data = new ObjectDataTableAdapter( dataTable.data = new ObjectDataTableAdapter(
[{ id: 1 }, { id: 2 }, { id: 3 }], [{ id: 0 }, { id: 1 }, { id: 2 }],
[ new ObjectDataColumn({ key: 'id' })] [ new ObjectDataColumn({ key: 'id' })]
); );
@@ -79,9 +79,9 @@ describe('DataTable', () => {
dataTable.selectRow(rows[1], true); dataTable.selectRow(rows[1], true);
const selection = dataTable.selection; const selection = dataTable.selection;
expect(selection[0].getValue('id')).toBe(1); expect(selection[0].getValue('id')).toBe(2);
expect(selection[1].getValue('id')).toBe(2); expect(selection[1].getValue('id')).toBe(0);
expect(selection[2].getValue('id')).toBe(3); expect(selection[2].getValue('id')).toBe(1);
}); });
it('should update schema if columns change', fakeAsync(() => { it('should update schema if columns change', fakeAsync(() => {

View File

@@ -601,8 +601,16 @@ export class DataTableComponent implements AfterContentInit, OnChanges, DoCheck,
selectRow(row: DataRow, value: boolean) { selectRow(row: DataRow, value: boolean) {
if (row) { if (row) {
row.isSelected = value; row.isSelected = value;
const rows = this.data.getRows() || []; const idx = this.selection.indexOf(row);
this.selection = rows.filter(r => r.isSelected); if (value) {
if (idx < 0) {
this.selection.push(row);
}
} else {
if (idx > -1) {
this.selection.splice(idx, 1);
}
}
} }
} }