Skip to content

Commit dedf987

Browse files
feat(grid): make selectedRows an @input #6653 #6974 (#7717)
1 parent 58d66fa commit dedf987

22 files changed

+295
-223
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ All notable changes for each version of this project will be documented in this
1515
- Added `closeOnEscape` - with it, the dialog can be allowed or prevented from closing when `Esc` is pressed.
1616
- `IgxNavbar`:
1717
- **Breaking Changes** - The `igx-action-icon` has been renamed to `igx-navbar-action`. It should get renamed in your components via `ng update`;
18+
- `IgxGrid`, `IgxTreeGrid`, `IgxHierarchicalGrid`
19+
- **Breaking Change** - The `selectedRows` method is now an `@Input` property. Setting it to an array of Row IDs will update the grid's selection state, any previous selection will be cleared. Setting it to an empty array will clear the selection entirely.
1820
- `igxGrid`
1921
- Added `onScroll` event, which is emitted when the grid is scrolled vertically or horizontally.
2022
- Each grid now expose a default handling for boolean column types. The column will display `check` or `close` icon, instead of true/false by default.
@@ -46,6 +48,7 @@ The following example shows how you can use the Indigo theme:
4648
}
4749
```
4850

51+
4952
### New Features
5053
- `IgxGrid`, `IgxTreeGrid`, `IgxHierarchicalGrid`
5154
- Introduced `showSummaryOnCollapse` grid property which allows you to control whether the summary row stays visible when the groupBy / parent row is collapsed.

projects/igniteui-angular/src/lib/grids/grid-base.directive.ts

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ import {
2525
InjectionToken,
2626
Optional,
2727
DoCheck,
28-
Directive
28+
Directive,
29+
OnChanges,
30+
SimpleChanges
2931
} from '@angular/core';
3032
import ResizeObserver from 'resize-observer-polyfill';
3133
import 'igniteui-trial-watermark';
@@ -1093,6 +1095,26 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
10931095
filteringExpressionsTree: IFilteringExpressionsTree,
10941096
done: (values: any[]) => void) => void;
10951097

1098+
/**
1099+
* Gets/Sets the current selection state.
1100+
* @remarks
1101+
* Represents the selected rows' IDs (primary key or rowData)
1102+
* @example
1103+
* ```html
1104+
* <igx-grid [data]="localData" primaryKey="ID" rowSelection="multiple" [selectedRows]="[0, 1, 2]"><igx-grid>
1105+
* ```
1106+
*/
1107+
@Input()
1108+
public set selectedRows(rowIDs: any[]) {
1109+
rowIDs.length > 0
1110+
? this.selectRows(rowIDs, true)
1111+
: this.deselectAllRows();
1112+
}
1113+
1114+
public get selectedRows(): any[] {
1115+
return this.selectionService.getSelectedRows();
1116+
}
1117+
10961118
/**
10971119
* Emitted when `IgxGridCellComponent` is clicked.
10981120
* @remarks
@@ -5291,18 +5313,6 @@ export class IgxGridBaseDirective extends DisplayDensityBase implements
52915313
[...this.unpinnedDataView, ...this.pinnedDataView];
52925314
}
52935315

5294-
/**
5295-
* Get current selection state.
5296-
* @example
5297-
* Returns an array with selected rows' IDs (primaryKey or rowData)
5298-
* ```typescript
5299-
* const selectedRows = this.grid.selectedRows();
5300-
* ```
5301-
*/
5302-
public selectedRows(): any[] {
5303-
return this.selectionService.getSelectedRows();
5304-
}
5305-
53065316
/**
53075317
* Select specified rows by ID.
53085318
* @example

projects/igniteui-angular/src/lib/grids/grid/grid-row-selection.spec.ts

Lines changed: 97 additions & 83 deletions
Large diffs are not rendered by default.

projects/igniteui-angular/src/lib/grids/grid/grid.groupby.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,20 +1022,20 @@ describe('IgxGrid - GroupBy #grid', () => {
10221022
grid.selectAllRows();
10231023
fix.detectChanges();
10241024

1025-
expect(grid.selectedRows().length).toEqual(8);
1025+
expect(grid.selectedRows.length).toEqual(8);
10261026
let rows = fix.debugElement.queryAll(By.css('.igx-grid__tr--selected'));
10271027
for (const r of rows) {
10281028
expect(r.componentInstance instanceof IgxGridRowComponent).toBe(true);
10291029
}
10301030

10311031
grid.deselectAllRows();
10321032
fix.detectChanges();
1033-
expect(grid.selectedRows().length).toEqual(0);
1033+
expect(grid.selectedRows.length).toEqual(0);
10341034

10351035
GridSelectionFunctions.clickHeaderRowCheckbox(fix);
10361036
fix.detectChanges();
10371037

1038-
expect(grid.selectedRows().length).toEqual(8);
1038+
expect(grid.selectedRows.length).toEqual(8);
10391039

10401040
rows = fix.debugElement.queryAll(By.css('.igx-grid__tr--selected'));
10411041
for (const r of rows) {
@@ -1600,7 +1600,7 @@ describe('IgxGrid - GroupBy #grid', () => {
16001600
GridSelectionFunctions.clickRowCheckbox(rows[0].element);
16011601
await wait(100);
16021602
grid.cdr.detectChanges();
1603-
expect(grid.selectedRows().length).toEqual(1);
1603+
expect(grid.selectedRows.length).toEqual(1);
16041604
GridSelectionFunctions.verifyRowSelected(rows[0]);
16051605

16061606
});

0 commit comments

Comments
 (0)