Skip to content

Commit 9fb9aa1

Browse files
committed
feat(grid): select by clicking the row
1 parent 6413b34 commit 9fb9aa1

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

projects/angular/components/ui-grid/src/ui-grid.component.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,16 @@ describe('Component: UiGrid', () => {
503503
expect(component.grid.selectionManager.selected[0].id).toEqual(component.data[0].id);
504504
});
505505

506+
it('should select by clicking on the row', () => {
507+
grid.shouldSelectOnRowClick = true;
508+
fixture.detectChanges();
509+
const firstRow = fixture.debugElement.query(By.css('.ui-grid-row'));
510+
firstRow.nativeElement.dispatchEvent(EventGenerator.click);
511+
fixture.detectChanges();
512+
513+
expect(component.grid.selectionManager.selected[0].id).toEqual(component.data[0].id);
514+
});
515+
506516
it('should have only one selection', () => {
507517
const radioBtns = fixture.debugElement.queryAll(By.css('.mat-radio-label'));
508518

projects/angular/components/ui-grid/src/ui-grid.component.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ import { UiGridIntl } from './ui-grid.intl';
9696
export const UI_GRID_OPTIONS = new InjectionToken<GridOptions<unknown>>('UiGrid DataManager options.');
9797
const DEFAULT_VIRTUAL_SCROLL_ITEM_SIZE = 48;
9898
const FOCUSABLE_ELEMENTS_QUERY = 'a, button:not([hidden]), input:not([hidden]), textarea, select, details, [tabindex]:not([tabindex="-1"])';
99+
const EXCLUDED_ROW_SELECTION_ELEMENTS = ['a', 'button', 'input', 'textarea', 'select'];
99100

100101
@Component({
101102
selector: 'ui-grid',
@@ -258,6 +259,13 @@ export class UiGridComponent<T extends IGridDataEntry> extends ResizableGrid<T>
258259
@Input()
259260
singleSelectable = false;
260261

262+
/**
263+
* Configure if the grid selects entity on row click.
264+
*
265+
*/
266+
@Input()
267+
shouldSelectOnRowClick = false;
268+
261269
/**
262270
* Option to select an alternate layout for footer pagination.
263271
*
@@ -1020,6 +1028,14 @@ export class UiGridComponent<T extends IGridDataEntry> extends ResizableGrid<T>
10201028
}
10211029

10221030
onRowClick(event: Event, row: T) {
1031+
if (this.shouldSelectOnRowClick && (event.target instanceof Element) &&
1032+
!EXCLUDED_ROW_SELECTION_ELEMENTS.find(el => (event.target as Element).closest(el))) {
1033+
if (this.singleSelectable) {
1034+
this.rowSelected(row);
1035+
} else {
1036+
this.selectionManager.toggle(row);
1037+
}
1038+
}
10231039
this.rowClick.emit({
10241040
event,
10251041
row,

0 commit comments

Comments
 (0)