Skip to content

Commit

Permalink
fix: use selectedRowIndex if specified when data changes (#1221)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmfKupl authored Nov 12, 2020
1 parent 1aafd6a commit 3036b76
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
28 changes: 27 additions & 1 deletion projects/ng2-smart-table/src/lib/lib/grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,22 @@ export class Grid {
return false;
}

// TODO: move to selectable? Separate directive
/**
* @breaking-change 1.8.0
* Need to add `| null` in return type
*
* TODO: move to selectable? Separate directive
*/
determineRowToSelect(changes: any): Row {

if (['load', 'page', 'filter', 'sort', 'refresh'].indexOf(changes['action']) !== -1) {
return this.dataSet.select(this.getRowIndexToSelect());
}

if (this.shouldSkipSelection()) {
return null;
}

if (changes['action'] === 'remove') {
if (changes['elements'].length === 0) {
// we have to store which one to select as the data will be reloaded
Expand Down Expand Up @@ -328,4 +338,20 @@ export class Grid {
const maxPageAmount: number = Math.ceil(source.count() / perPage);
return maxPageAmount ? Math.min(pageToSelect, maxPageAmount) : pageToSelect;
}

private shouldSkipSelection(): boolean {
/**
* For backward compatibility when using `selectedRowIndex` with non-number values - ignored.
*
* Therefore, in order to select a row after some changes,
* the `selectedRowIndex` value must be invalid or >= 0 (< 0 means that no row is selected).
*
* `Number(value)` returns `NaN` on all invalid cases, and comparisons with `NaN` always return `false`.
*
* !!! We should skip a row only in cases when `selectedRowIndex` < 0
* because when < 0 all lines must be deselected
*/
const selectedRowIndex = Number(this.getSetting('selectedRowIndex'));
return selectedRowIndex < 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ export class Ng2SmartTableComponent implements OnChanges, OnDestroy {
defaultSettings: Object = {
mode: 'inline', // inline|external|click-to-edit
selectMode: 'single', // single|multi
selectedRowIndex: 0, // points to an element in all data
/**
* Points to an element in all data
*
* when < 0 all lines must be deselected
*/
selectedRowIndex: 0,
switchPageToSelectedRowPage: false,
hideHeader: false,
hideSubHeader: false,
Expand Down

0 comments on commit 3036b76

Please sign in to comment.