Angular Grid Selection

    With Ignite UI for Angular Grid you can easily select data by using variety of events, rich API or with simple mouse interactions like single select.

    Angular Grid Selection Example

    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.

    Angular Grid Selection Options

    IgniteUI for Angular Grid component provides three different selection modes - Row selection, Cell selection and Column selection. By default only Multi-cell selection mode is enabled in the Grid. In order to change/enable selection mode you can use rowSelection, cellSelection or selectable properties.

    Angular Row Selection

    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

    Go to Row selection topic for more information.

    Angular Cell Selection

    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.

    Go to Cell selection topic for more information.

    Angular Column Selection

    The selectable property enables you to specify the following options for each column:

    • false - the corresponding column selection will be disabled for the Grid
    • true - the corresponding column selection will be enabled for the Grid
    • This lead to the following three variations:
    • Single selection - mouse click over the column cell.
    • Multi column selection - holding ctrl + mouse click over the column cells.
    • Range column selection - holding shift + mouse click selects everything in between.

    Go to Column selection topic for more information.

    Grid Context Menu

    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:

    public rightClick(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 ranges
        if (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) {
                    return true;
                }
                return false;
            })
            // If the cell is within a multi-cell selection range, bind all the selected cells data
            if (isCellWithinRange) {
                this.multiCellArgs = { data: this.multiCellSelection.data };
            }
        }
        // Set the position of the context menu
        this.contextmenuX = eventArgs.event.clientX;
        this.contextmenuY = eventArgs.event.clientY;
        this.clickedCell = eventArgs.cell;
        // Enable the context menu
        this.contextmenu = true;
    }
    

    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
    //contextmenu.component.ts
    
    public copySelectedCellData(event) {
        const selectedData = { [this.cell.column.field]: this.cell.value };
        this.copyData(JSON.stringify({ [this.cell.column.field]: this.cell.value }));
        this.onCellValueCopy.emit({ data: selectedData });
    }
    
    public copyRowData(event) {
        const selectedData = this.cell.row.data ;
        this.copyData(JSON.stringify(this.cell.row.data));
        this.onCellValueCopy.emit({ data: selectedData });
    }
    
    public copySelectedCells(event) {
        const selectedData = this.selectedCells.data;
        this.copyData(JSON.stringify(selectedData));
        this.onCellValueCopy.emit({ data: selectedData });
    }
    

    The IgxGrid will fetch the copied data and will paste it in a container element.

    The template we are going to use to combine the grid with the context menu:

    <div class="wrapper">
        <div class="grid__wrapper" (window:click)="disableContextMenu()">
            <igx-grid #grid1 [data]="data" [autoGenerate]="false" height="500px" width="100%"
                (contextMenu)="rightClick($event)" (rangeSelected)="getCells($event)"
                (selected)="cellSelection($event)">
            <!-- Columns area -->
            </igx-grid>
            <div *ngIf="contextmenu==true">
                <contextmenu [x]="contextmenuX" [y]="contextmenuY" [cell]="clickedCell" [selectedCells]="multiCellArgs" (onCellValueCopy)="copy($event)">
                </contextmenu>
            </div>
        </div>
        <div class="selected-data-area">
            <div>
               <pre>{{copiedData}}</pre>
            </div>
        </div>
    </div>
    

    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:

    Known Issues and Limitations

    • 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';
      
    • 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:

      • Row Selection
      • Row Expand/collapse
      • Row Editing
      • Row Pinning

    API References

    Additional Resources

    Our community is active and always welcoming to new ideas.