Skip to content

Commit

Permalink
feat(row): add a deselect row feature
Browse files Browse the repository at this point in the history
  • Loading branch information
nureha authored and lexzhukov committed Mar 29, 2017
1 parent 58e6a26 commit 2ab02b4
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/ng2-smart-table/lib/data-set/data-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ export class DataSet {
}

selectRow(row: Row): Row {
let previousIsSelected = row.isSelected;
this.deselectAll();

row.isSelected = true;
row.isSelected = !previousIsSelected;
this.selectedRow = row;

return this.selectedRow;
Expand Down
4 changes: 4 additions & 0 deletions src/ng2-smart-table/lib/data-set/row.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export class Row {
return this.data;
}

getIsSelected(): boolean {
return this.isSelected;
}

getNewData(): any {
const values = Object.assign({}, this.data);
this.getCells().forEach((cell) => values[cell.getColumn().id] = cell.newValue);
Expand Down
42 changes: 25 additions & 17 deletions src/ng2-smart-table/ng2-smart-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,20 @@ export class Ng2SmartTableComponent implements OnChanges {
onUserSelectRow(row: Row) {
if (this.grid.getSetting('selectMode') !== 'multi') {
this.grid.selectRow(row);
this._onUserSelectRow(row.getData());
this.onSelectRow(row);
this._onUserSelectRow(row);
this.emitSelectRow(row);
}
}

onSelectRow(row: Row): void {
this.grid.selectRow(row);
this.emitSelectRow(row);
}

multipleSelectRow(row: Row) {
this.grid.multipleSelectRow(row);
this._onUserSelectRow(row.getData());
this._onSelectRow(row.getData());
this._onUserSelectRow(row);
this.emitSelectRow(row);
}

onSelectAllRows($event: any) {
Expand All @@ -115,22 +120,22 @@ export class Ng2SmartTableComponent implements OnChanges {
const selectedRows = this.grid.getSelectedRows();

this._onUserSelectRow(selectedRows[0], selectedRows);
this._onSelectRow(selectedRows[0]);
this.emitSelectRow(selectedRows[0]);
}

onSelectRow(row: Row) {
this.grid.selectRow(row);
this._onSelectRow(row.getData());
this.emitSelectRow(row);
}

onMultipleSelectRow(row: Row) {
this._onSelectRow(row.getData());
this.emitSelectRow(row);
}

initGrid() {
this.source = this.prepareSource();
this.grid = new Grid(this.source, this.prepareSettings());
this.grid.onSelectRow().subscribe((row) => this.onSelectRow(row));
this.grid.onSelectRow().subscribe((row) => this.emitSelectRow(row));
}

prepareSource(): DataSource {
Expand Down Expand Up @@ -159,16 +164,10 @@ export class Ng2SmartTableComponent implements OnChanges {
this.resetAllSelector();
}

private _onSelectRow(data: any) {
this.rowSelect.emit({
data: data || null,
source: this.source,
});
}

private _onUserSelectRow(data: any, selected: Array<any> = []) {
private _onUserSelectRow(row: Row, selected: Array<any> = []) {
this.userRowSelect.emit({
data: data || null,
data: row.getData(),
isSelected: row.getIsSelected(),
source: this.source,
selected: selected.length ? selected : this.grid.getSelectedRows(),
});
Expand All @@ -177,4 +176,13 @@ export class Ng2SmartTableComponent implements OnChanges {
private resetAllSelector() {
this.isAllSelected = false;
}

private emitSelectRow(row: Row): void {
this.rowSelect.emit({
data: row.getData(),
isSelected: row.getIsSelected(),
source: this.source,
});
}

}

0 comments on commit 2ab02b4

Please sign in to comment.