Skip to content

Commit

Permalink
Fixed #981
Browse files Browse the repository at this point in the history
  • Loading branch information
Çağatay Çivici committed Jun 12, 2017
1 parent 914daae commit b9f495d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 15 additions & 2 deletions src/app/components/datatable/datatable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,8 @@ export class DataTable implements AfterViewChecked,AfterViewInit,AfterContentIni
@Input() metaKeySelection: boolean = true;

@Input() rowTrackBy: Function = (index: number, item: any) => item;

@Input() compareSelectionBy: string = 'deepEquals';

@Output() onEditInit: EventEmitter<any> = new EventEmitter();

Expand Down Expand Up @@ -1301,7 +1303,7 @@ export class DataTable implements AfterViewChecked,AfterViewInit,AfterContentIni
let index: number = -1;
if(this.selection) {
for(let i = 0; i < this.selection.length; i++) {
if(this.objectUtils.equals(rowData, this.selection[i], this.dataKey)) {
if(this.equals(rowData, this.selection)) {
index = i;
break;
}
Expand All @@ -1312,7 +1314,18 @@ export class DataTable implements AfterViewChecked,AfterViewInit,AfterContentIni
}

isSelected(rowData) {
return ((rowData && this.objectUtils.equals(rowData, this.selection, this.dataKey)) || this.findIndexInSelection(rowData) != -1);
if(rowData && this.selection) {
if(this.selection instanceof Array)
return this.findIndexInSelection(rowData) > -1;
else
return this.equals(rowData, this.selection);
}

return false;
}

equals(data1, data2) {
return this.compareSelectionBy === 'equals' ? (data1 === data2) : this.objectUtils.equals(data1, data2, this.dataKey);
}

get allSelected() {
Expand Down
12 changes: 10 additions & 2 deletions src/app/showcase/components/datatable/datatabledemo.html
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,10 @@ <h3>Filtering</h3>
<h3>Selection</h3>
<p>DataTable provides single and multiple selection modes on click of a row. Selected rows are bound to the selection property and onRowSelect-onRowUnselect events
are provided as optional callbacks. Alternatively column based selection can be done using radio buttons or checkboxes using selectionMode of a particular column.
When comparing if a row is selected, DataTable traverses all properties of the object which affects performance as a result. It is suggested to define a dataKey property
that uniquely identifies a record to avoid deep object comparison and increase performance.</p>
When resolving if a row is selected, by default DataTable uses deepEquals which traverses all properties of the object thus affects performance as a result so it is suggested to define a dataKey property
that uniquely identifies a record to avoid deep object comparison and increase performance. Another alternative is to compare rows by reference using compareSelectionBy property as "equals". See compareSelectionBy
in properties section for more information. Depending on your case you may use "equals" and "deepEquals" alternatives.</p>

<p>In single mode, selection binding is an object reference.</p>
<pre>
<code class="language-typescript" pCode ngNonBindable>
Expand Down Expand Up @@ -1275,6 +1277,12 @@ <h3>Properties</h3>
<td>null</td>
<td>Function to optimize the dom operations by delegating to ngForTrackBy, default algoritm checks for object identity.</td>
</tr>
<tr>
<td>compareSelectionBy</td>
<td>string</td>
<td>deepEquals</td>
<td>Algorithm to define if a row is selected, valid values are "equals" that compares by reference and "deepEquals" that compares all fields.</td>
</tr>
</tbody>
</table>
</div>
Expand Down

0 comments on commit b9f495d

Please sign in to comment.