Skip to content

Commit

Permalink
perf(data-table): make checkbox state check more performant (#777)
Browse files Browse the repository at this point in the history
also, when clicking on uncheck all, we only uncheck the items that are in the current data array
  • Loading branch information
emoralesb05 authored Jul 21, 2017
1 parent 846a4b4 commit 3d2a1ab
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions src/platform/core/data-table/data-table.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,14 @@ export class TdDataTableComponent implements ControlValueAccessor, AfterContentI
if (this.isRowSelected(row)) {
toggledRows.push(row);
}
row = this._value.filter((val: any) => {
return this.compareWith(row, val);
})[0];
let index: number = this._value.indexOf(row);
if (index > -1) {
this._value.splice(index, 1);
}
});
this.clearModel();
this._allSelected = false;
this._indeterminate = false;
}
Expand Down Expand Up @@ -597,38 +603,23 @@ export class TdDataTableComponent implements ControlValueAccessor, AfterContentI
}
}
this._calculateCheckboxState();
this.onRowSelect.emit({row: row, selected: this.isRowSelected(row)});
this.onRowSelect.emit({row: row, selected: !wasSelected});
this.onChange(this._value);
}

/**
* Calculate all the state of all checkboxes
*/
private _calculateCheckboxState(): void {
this._calculateAllSelected();
this._calculateIndeterminate();
}

/**
* Checks if all visible rows are selected.
*/
private _calculateAllSelected(): void {
const match: string =
this._data ? this._data.find((d: any) => !this.isRowSelected(d)) : true;
this._allSelected = typeof match === 'undefined';
}

/**
* Checks if all visible rows are selected.
*/
private _calculateIndeterminate(): void {
this._indeterminate = false;
if (this._data) {
this._allSelected = typeof this._data.find((d: any) => !this.isRowSelected(d)) === 'undefined';
this._indeterminate = false;
for (let row of this._data) {
if (!this.isRowSelected(row)) {
continue;
}
this._indeterminate = true;
break;
}
}
}
Expand Down

0 comments on commit 3d2a1ab

Please sign in to comment.