The sample below demonstrates the three types of Grid's cell selection behavior. Use the buttons below to enable each of the available selection modes. A brief description will be provided on each button interaction through a snackbar message box.
<spanclass="button-group-wrapper"><igx-buttongroup [values]="selectionModes" (selected)="selectCellSelectionMode($event)"></igx-buttongroup></span><divclass="sample-wrapper"><divclass="grid-wrapper"><igx-grid [igxPreventDocumentScroll]="true" #grid [data]="data" [height]="'550px'" [cellSelection]="selectionMode"
(rangeSelected)="onRangeSelected()"
(selected)="onCellSelected($event)"
><igx-column [field]="'ProductID'"></igx-column><igx-column [field]="'ProductName'"></igx-column><igx-column [field]="'UnitsInStock'" [dataType]="'number'"></igx-column><igx-column [field]="'UnitPrice'" [dataType]="'number'"></igx-column><igx-column [field]="'Discontinued'" [dataType]="'boolean'"></igx-column><igx-column [field]="'OrderDate'" [dataType]="'date'"></igx-column></igx-grid></div><divclass="log-wrapper"><divclass="selected-data-area"><divclass="logContainer"><divclass="highlight"><span>Events execution sequence</span><buttonclass="clearBtn"igxButton="flat" (click)="clearLog()"><igx-icon>clear</igx-icon><span>Clear log</span></button></div><hr /><div #loggerclass="logger"></div></div></div></div></div><igx-snackbar #snackbaractionText="Got it. Thanks!" (clicked)="snackbar.close()"><divclass="container"><igx-icon>notification_important</igx-icon>
@if (selectionMode === 'multiple') {
<ul><li><b>This is the default selection behavior.</b></li><li>Click on a cell and while pressing the mouse key perform drag action.
</li><li>Select a cell and press Shift + Arrow down key, this will start range selection as well.</li></ul>
}
@if (selectionMode === 'single') {
<ul><li><b>Now you can select only one cell within the grid.</b></li><li>On single cell click the previous selection state will be cleared.</li><li>The mouse drag will not work and instead of selecting a cell, this will make default text selection.</li></ul>
}
@if (selectionMode === 'none') {
<ul><li><b>Now you are unable to select a cell while interacting with grid UI.</b></li><li>If you need to select a cell, you can use the grid API methods.</li></ul>
}
</div></igx-snackbar>html
Property rowSelection enables you to specify the following options:
none - Row selection would be disabled for the Grid
single - Selection of only one row within the Grid would be available
multiple - Multi-row selection would be available by using the Row selectors, with a key combination like ctrl + click, or by pressing the space key once a cell is focused
Property cellSelection enables you to specify the following options:
none - Cell selection would be disabled for the Grid
single - Selection of only one cell within the Grid would be available.
multiple - Currently, this is the default state of the selection in the Grid. Multi-cell selection is available by mouse dragging over the cells, after a left button mouse clicked continuously.
Using the contextMenu event you can add a custom context menu to facilitate your work with IgxGrid. With a right click on the grid's body, the event emits the cell on which it is triggered. The context menu will operate with the emitted cell.
If there is a multi-cell selection, we will put logic, which will check whether the selected cell is in the area of the multi-cell selection. If it is, we will also emit the values of the selected cells.
Basically the main function will look like this:
publicrightClick(eventArgs: any) {
// Prevent the default behavior of the right click
eventArgs.event.preventDefault();
this.multiCellArgs = {};
// If we have multi-cell selection, check if selected cell is within the rangesif (this.multiCellSelection) {
const node = eventArgs.cell.selectionNode;
const isCellWithinRange = this.grid1.getSelectedRanges().some(range => {
if (node.column >= range.columnStart &&
node.column <= range.columnEnd &&
node.row >= range.rowStart &&
node.row <= range.rowEnd) {
returntrue;
}
returnfalse;
})
// If the cell is within a multi-cell selection range, bind all the selected cells dataif (isCellWithinRange) {
this.multiCellArgs = { data: this.multiCellSelection.data };
}
}
// Set the position of the context menuthis.contextmenuX = eventArgs.event.clientX;
this.contextmenuY = eventArgs.event.clientY;
this.clickedCell = eventArgs.cell;
// Enable the context menuthis.contextmenu = true;
}
typescript
The context menu will have the following functions:
Copy the selected cell's value
Copy the selected cell's dataRow
If the selected cell is within a multi-cell selection range, copy all the selected data
Select multiple cells and press the right mouse button. The context menu will appear and after selecting Copy cells data the selected data will appear in the right empty box.
The result is:
Using the Grid with Selection enabled on IE11 requires the explicit import of the array polyfill in polyfill.ts of the angular application. IE11 is no longer supported as of version 13.0.0.
import'core-js/es7/array';
typescript
When the grid has no primaryKey set and remote data scenarios are enabled (when paging, sorting, filtering, scrolling trigger requests to a remote server to retrieve the data to be displayed in the grid), a row will lose the following state after a data request completes: