React Grid Selection Overview

    With the Ignite UI for React Select feature in React Grid you can easily interact with and manipulate data using simple mouse interactions. There are three selection modes available:

    • Row selection
    • Cell selection
    • Column selection

    With the rowSelection property, you can specify:

    • None
    • Single
    • Multiple Select

    React Grid Selection Example

    The sample below demonstrates three types of cell selection behaviors in the IgrGrid. Use the buttons below to enable each of the available selection modes.

    React Grid Selection Options

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

    React Grid Row Selection

    Property rowSelection enables you to specify the following options:

    • None - Row selection would be disabled for the IgrGrid.
    • Single - Selection of only one row within the IgrGrid 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.

    React Grid Cell Selection

    Property cellSelection enables you to specify the following options:

    • None - Cell selection would be disabled for the IgrGrid.
    • Single - Selection of only one cell within the IgrGrid would be available.
    • Multiple - Currently, this is the default state of the selection in the IgrGrid. 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.

    React Grid Column Selection

    The selectable property enables you to specify the following options for each IgrColumn. The corresponding column selection will be enabled or disabled if this property is set to true or false, respectively.

    This leads 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.

    React Grid Context Menu

    Using the ContextMenu event you can add a custom context menu to facilitate your work with IgrGrid. 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:

    function rightClick(grid: IgrGridBaseDirective, event: IgrGridCellEventArgs) {
        const eventArgs = event.detail;
        eventArgs.event.preventDefault();
        const node = eventArgs.cell.id;
        const isCellWithinRange = grid.getSelectedRanges().some((range: any) => {
            if (node.columnID >= range.columnStart &&
                node.columnID <= range.columnEnd &&
                node.rowIndex >= range.rowStart &&
                node.rowIndex <= range.rowEnd
                ) {
                    return true;
                }
            return false;
        });
        setIsCellWithinRange(isCellWithinRange);
        setClickedCell(eventArgs.cell);
        openContextMenu(eventArgs.event.clientX, eventArgs.event.clientY)
    }
    

    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.
    function copySelectedRowData() {
        const selectedData = gridRef.current.getRowData(clickedCell.id.rowID);
        copyData(selectedData);
        closeContextMenu();
    }
    
    function copySelectedCellData() {
        const selectedData = clickedCell.value;
        copyData(selectedData);
        closeContextMenu();
    }
    
    function copySelectedData() {
        const selectedData = gridRef.current.getSelectedData(null,null);
        copyData(selectedData);
        closeContextMenu();
    }
    
    function copyData(data: any[]) {
        const tempElement = document.createElement('input');
        document.body.appendChild(tempElement);
        tempElement.setAttribute('id', 'temp_id');
        (document.getElementById('temp_id') as HTMLInputElement).value = JSON.stringify(data);
        tempElement.select();
        document.execCommand('copy');
        document.body.removeChild(tempElement);
        setSelectedData(JSON.stringify(data));
    }
    

    The IgrGrid 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 className="container sample">
            <div className="wrapper" onClick={closeContextMenu}>
                <IgrGrid
                    autoGenerate="false"
                    data={northWindData}
                    primaryKey="ProductID"
                    ref={gridRef}
                    contextMenu={rightClick}>
                <IgrColumn field="ProductID" header="Product ID">
                </IgrColumn>
                <IgrColumn field="ProductName" header="Product Name">
                </IgrColumn>
                <IgrColumn field="UnitsInStock" header="Units In Stock" dataType="number">
                </IgrColumn>
                <IgrColumn field="UnitPrice" header="Units Price" dataType="number">
                </IgrColumn>
                <IgrColumn field="Discontinued" dataType="boolean">
                </IgrColumn>
                <IgrColumn field="OrderDate" header="Order Date" dataType="date">
                </IgrColumn>
                </IgrGrid>
                <div className="selected-data-area">
                    {selectedData}
                </div>
            </div>
        </div>
        <div style={{display: "none"}} className="contextmenu" ref={contextMenuRef}>
            <span className="item" onClick={copySelectedCellData}>
                <IgrIcon ref={iconRef} collection='material' name="content_copy"></IgrIcon>Copy Cell Data
            </span>
            <span className="item" onClick={copySelectedRowData}>
                <IgrIcon collection='material' name="content_copy"></IgrIcon>Copy Row Data
            </span>
            {isCellWithinRange && (
            <span className="item" onClick={copySelectedData}>
                <IgrIcon collection='material' name="content_copy"></IgrIcon>Copy Cells Data
            </span>)}
        </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

    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.