React Grid Row Selection

    The Ignite UI for React Row Selection feature in React Grid allows users to interactively select, highlight, or deselect a single or multiple rows of data. There are several selection modes available in the IgrGrid:

    • None Selection
    • Multiple Selection
    • Single Selection

    React Row Selection Example

    The sample below demonstrates the three types of IgrGrid's row selection behavior. Use the drop-down below to enable each of the available selection modes. Use the checkbox to hide or show the row selector checkboxes.

    Setup

    In order to setup row selection in the IgrGrid, you just need to set the rowSelection property. This property accepts GridSelectionMode enumeration.

    GridSelectionMode exposes the following modes:

    • None
    • Single
    • Multiple

    Below we will take a look at each of them in more detail.

    None Selection

    In the IgrGrid by default row selection is disabled (rowSelection is None). So you can not select or deselect a row through interaction with the IgrGrid UI, the only way to complete these actions is to use the provided API methods.

    Single Selection

    Single row selection can now be easily set up, the only thing you need to do, is to set rowSelection to Single property. This gives you the opportunity to select only one row within a grid. You can select a row by clicking on a cell or pressing the space key when you focus on a cell of the row, and of course you can select a row by clicking on the row selector field. When row is selected or deselected RowSelectionChanging event is emitted.

    function handleRowSelection(args: IgrRowSelectionEventArgs) {
        if (args.detail.added.length && args.detail.added[0] === 3) {
            args.detail.cancel = true;
        }
    }
    
    <IgrGrid rowSelection="single" autoGenerate="true" allowFiltering="true" rowSelectionChanging={handleRowSelection}>
    </IgrGrid>
    

    Multiple Selection

    To enable multiple row selection in the IgrGrid just set the rowSelection property to Multiple. This will enable a row selector field on each row and in the IgrGrid header. The row selector allows users to select multiple rows, with the selection persisting through scrolling, paging, and data operations, such as sorting and filtering. The row also can be selected by clicking on a cell or by pressing the space key when a cell is focused. If you have selected one row and click on another while holding the shift key, this will select the whole range of rows. In this selection mode, when you click on a single row, the previous selected rows will be deselected. If you click while holding the ctrl key, the row will be toggled and the previous selection will be preserved.

    <IgrGrid primaryKey="ProductID" rowSelection="multiple"
            allowFiltering="true" autoGenerate="true">
    </IgrGrid>
    

    Notes

    • Row selection will trigger RowSelectionChanging event. This event gives you information about the new selection, old selection, the rows that have been added and removed from the old selection. Also the event is cancellable, so this allows you to prevent selection.
    • When row selection is enabled row selectors are displayed, but if you don't want to show them, you can set hideRowSelectors to true.
    • When you switch between row selection modes at runtime, this will clear the previous row selection state.

    API usage

    Select Rows Programmatically

    The code snippet below can be used to select one or multiple rows simultaneously (via primaryKey). Additionally, the second parameter of this method is a boolean property through which you may choose whether the previous row selection will be cleared or not. The previous selection is preserved by default.

    function onClickSelect() {
        gridRef.current.selectRows([1,2,5], true);
    }
    
    <IgrGrid primaryKey="ProductID" rowSelection="multiple" autoGenerate="true" ref={gridRef}>
    </IgrGrid>
    <button onClick={onClickSelect}>Select 1,2 and 5</button>
    

    This will add the rows which correspond to the data entries with IDs 1, 2 and 5 to the IgrGrid selection.

    Deselect Rows

    If you need to deselect rows programmatically, you can use the deselectRows method.

    function onClickDeselect() {
        gridRef.current.deselectRows([1,2,5]);
    }
    
    <IgrGrid primaryKey="ProductID" rowSelection="multiple" autoGenerate="true" ref={gridRef}>
    </IgrGrid>
    <button onClick={onClickDeselect}>Deselect 1,2 and 5</button>
    

    Row Selection Event

    When there is some change in the row selection RowSelectionChanging event is emitted. RowSelectionChanging exposes the following arguments:

    • OldSelection - array of row IDs that contains the previous state of the row selection.
    • NewSelection - array of row IDs that match the new state of the row selection.
    • Added - array of row IDs that are currently added to the selection.
    • Removed - array of row IDs that are currently removed according old selection state.
    • Event - the original event that triggered row selection change.
    • Cancel - allows you the prevent the row selection change.
    function handleRowSelectionChange(args: IgrRowSelectionEventArgs) {
        args.detail.cancel = true; // this will cancel the row selection
    }
    
    <IgrGrid rowSelectionChanging={handleRowSelectionChange}>
    </IgrGrid>
    

    Select All Rows

    Another useful API method that IgrGrid provides is selectAllRows. By default this method will select all data rows, but if filtering is applied, it will select only the rows that match the filter criteria. If you call the method with false parameter, SelectAllRows(false) will always select all data in the grid, even if filtering is applied.

    Note Keep in mind that selectAllRows will not select the rows that are deleted.

    Deselect All Rows

    IgrGrid provides a deselectAllRows method, which by default will deselect all data rows, but if filtering is applied will deselect only the rows that match the filter criteria. If you call the method with false parameter, DeselectAllRows(false) will always clear all row selection state even if filtering is applied.

    How to get Selected Rows

    If you need to see which rows are currently selected, you can get their row IDs with the selectedRows getter.

    function getSelectedRows() {
        return gridRef.current.selectedRows;
    }
    

    Additionally, assigning row IDs to selectedRows will allow you to change the grid's selection state.

    const mySelectedRows = [1,2,3];
    
    <IgrGrid primaryKey="ProductID" rowSelection="multiple" autoGenerate="false" selectedRows={mySelectedRows}>
    </IgrGrid>
    

    Row Selector Templates

    You can template header and row selectors in the IgrGrid and also access their contexts which provide useful functionality for different scenarios.

    By default, the IgrGrid handles all row selection interactions on the row selector's parent container or on the row itself, leaving just the state visualization for the template. Overriding the base functionality should generally be done using the RowSelectionChanging event. In case you implement a custom template with a Click handler which overrides the base functionality, you should stop the event's propagation to preserve the correct row state.

    Row Template

    To create a custom row selector template, within the IgrGrid you can use the rowSelectorTemplate property. From the template you can access the implicitly provided context variable, with properties that give you information about the row's state.

    The selected property shows whether the current row is selected or not while the index property can be used to access the row index.

    function rowSelectorTemplate(ctx: IgrRowSelectorTemplateContext) {
        if (ctx.dataContext.implicit.selected) {
            return (
                <>
                    <div style={{justifyContent: 'space-evenly', display: 'flex', width: '70px'}}>
                        <span> ${ctx.dataContext.implicit.index}</span>
                        <IgrCheckbox checked></IgrCheckbox>
                    </div>
                </>
            );
        } else {
            return (
                <>
                    <div style={{justifyContent: 'space-evenly', display: 'flex', width: '70px'}}>
                        <span> ${ctx.dataContext.implicit.index}</span>
                        <IgrCheckbox checked></IgrCheckbox>
                    </div>
                </>
            );
        }
    }
    
    <IgrGrid primaryKey="ProductID" rowSelection="multiple" autoGenerate="false" rowSelectorTemplate={rowSelectorTemplate}>
    </IgrGrid>
    

    The rowID property can be used to get a reference of an IgrGrid row. This is useful when you implement a click handler on the row selector element.

    public rowSelectorTemplate(ctx: IgrRowSelectorTemplateContext) {
        return (
            <>
                <IgrCheckbox onClick={(event) => onSelectorClick(event, ctx.dataContext.implicit.key)}>
                </IgrCheckbox>
            </>
        );
    }
    

    In the above example we are using an IgrCheckbox and we bind rowContext.selected to its checked property. See this in action in our Row Numbering Demo.

    Header Template

    To create a custom header selector template, within the IgrGrid, you can use the headSelectorTemplate property. From the template you can access the implicitly provided context variable, with properties that give you information about the header's state.

    The SelectedCount property shows you how many rows are currently selected while totalCount shows you how many rows there are in the IgrGrid in total.

    function headSelectorTemplate(ctx: IgrHeadSelectorTemplateContext) {
        return (
            <>
                {ctx.dataContext.implicit.selectedCount} / {ctx.dataContext.implicit.totalCount}
            </>
        );
    };
    

    The SelectedCount and TotalCount properties can be used to determine if the head selector should be checked or indeterminate (partially selected).

    function headSelectorTemplate(ctx: IgcHeadSelectorTemplateContext) {
        const implicit: any = ctx.dataContext.implicit;
        if (implicit.selectedCount > 0 && implicit.selectedCount === implicit.totalCount) {
                return (
                    <>
                        <IgrCheckbox checked></IgrCheckbox>
                    </>
                );
            } else if (implicit.selectedCount > 0 && implicit.selectedCount !== implicit.totalCount) {
                return (
                    <>
                        <IgrCheckbox indeterminate></IgrCheckbox>
                    </>
                );
            }
            return (
                <>
                    <IgrCheckbox ></IgrCheckbox>
                </>
            );
    }
    
    <IgrGrid primaryKey="ProductID" rowSelection="multiple" autoGenerate="true" headSelectorTemplate={headSelectorTemplate}>
    </IgrGrid>
    

    Row Numbering Demo

    This demo shows the usage of custom header and row selectors. The latter uses index to display row numbers and an IgrCheckbox bound to selected.

    Excel Style Row Selectors Demo

    This demo uses custom templates to resemble Excel-like header and row selectors.

    Conditional Selection Demo

    This demo prevents some rows from being selected using the RowSelectionChanging event and a custom template with disabled checkbox for non-selectable rows.

    API References

    Additional Resources

    Our community is active and always welcoming to new ideas.