Configure the grid to allow users to select rows by clicking them, focusing a row and pressing ␣ Space or via API.
Enabling Row Selection Copy Link
Row selection is configured with the rowSelection grid property. Setting rowSelection.mode to either 'multiRow' or 'singleRow' will allow you to select rows by clicking, focusing a row and pressing ␣ Space, or via the selection API.
const gridOptions = {
rowSelection: {
mode: 'singleRow',
},
// other grid options ...
}The following example illustrates a basic row selection configuration. Use the select control to choose the default single row or multi-row configuration.
See detailed documentation on the two row selection modes:
import {
ClientSideRowModelModule,
GridApi,
GridOptions,
ModuleRegistry,
RowSelectionMode,
RowSelectionModule,
createGrid,
enableDevValidations,
} from "ag-grid-community";
import {
ColumnMenuModule,
ColumnsToolPanelModule,
ContextMenuModule,
RowGroupingModule,
} from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";
if (process.env.NODE_ENV !== "production") {
// Enable extended validations only for development
enableDevValidations();
}
ModuleRegistry.registerModules([
RowSelectionModule,
ClientSideRowModelModule,
ColumnsToolPanelModule,
ColumnMenuModule,
ContextMenuModule,
RowGroupingModule,
]);
let gridApi: GridApi<IOlympicData>;
const gridOptions: GridOptions<IOlympicData> = {
columnDefs: [
{ field: "athlete", minWidth: 150 },
{ field: "age", maxWidth: 90 },
{ field: "country", minWidth: 150 },
{ field: "year", maxWidth: 90 },
{ field: "date", minWidth: 150 },
{ field: "sport", minWidth: 150 },
{ field: "gold" },
{ field: "silver" },
{ field: "bronze" },
{ field: "total" },
],
defaultColDef: {
flex: 1,
minWidth: 100,
},
rowSelection: {
mode: "singleRow",
},
};
const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);
fetch("https://www.ag-grid.com/example-assets/small-olympic-winners.json")
.then((response) => response.json())
.then((data: IOlympicData[]) => gridApi.setGridOption("rowData", data));
function getSelectValue(id: string): RowSelectionMode {
return (
(document.querySelector<HTMLSelectElement>(id)
?.value as RowSelectionMode) ?? "singleRow"
);
}
function updateSelectionOptions() {
gridApi.setGridOption("rowSelection", {
mode: getSelectValue("#input-selection-mode"),
});
}
if (typeof window !== "undefined") {
// Attach external event handlers to window so they can be called from index.html
(<any>window).updateSelectionOptions = updateSelectionOptions;
}
.example-wrapper {
display: flex;
flex-direction: column;
height: 100%;
}
.example-header {
margin-bottom: 10px;
}
#myGrid {
flex: 1 1 0px;
width: 100%;
}
<div class="example-wrapper">
<div class="example-header">
<label>
<span>Selection mode: </span>
<select id="input-selection-mode" onchange="updateSelectionOptions()">
<option value="singleRow">singleRow</option>
<option value="multiRow">multiRow</option>
</select>
</label>
</div>
<div id="myGrid" style="height: 100%"></div>
</div>
export interface IOlympicData {
athlete: string,
age: number,
country: string,
year: number,
date: string,
sport: string,
gold: number,
silver: number,
bronze: number,
total: number
} Row Selection with Enterprise Features Copy Link
Row selection can be used when using row grouping, tree data and the server-side row model. See the respective sections of the documentation: