Control how selected rows and cells appear.
Row Selections Copy Link
When row selection is enabled, you can set the color of selected rows using the selectedRowBackgroundColor parameter. If your grid uses alternating row colours we recommend setting this to a semi-transparent colour so that the alternating row colours are visible below it.
const myTheme = themeQuartz.withParams({
// bright green, 10% opacity
selectedRowBackgroundColor: 'rgba(0, 255, 0, 0.1)',
// alternating row colours will be visible through the semi-transparent
// selection background colour
oddRowBackgroundColor: '#8881',
});"use client";
import React, {
useCallback,
useMemo,
useRef,
useState,
StrictMode,
} from "react";
import { createRoot } from "react-dom/client";
import { AgGridReact, AgGridProvider } from "ag-grid-react";
import {
AllCommunityModule,
ColDef,
ColGroupDef,
GridApi,
GridOptions,
ModuleRegistry,
RowSelectionOptions,
Theme,
enableDevValidations,
themeQuartz,
} from "ag-grid-community";
import { IOlympicData } from "./interfaces";
import { useFetchJson } from "./useFetchJson";
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
const modules = [AllCommunityModule];
const myTheme = themeQuartz.withParams({
// bright green, 10% opacity
selectedRowBackgroundColor: "rgba(0, 255, 0, 0.1)",
// alternating row colors will be visible through the semi-transparent
// selection background color
oddRowBackgroundColor: "#8881",
});
const GridExample = () => {
const containerStyle = useMemo(() => ({ width: "100%", height: "100%" }), []);
const gridStyle = useMemo(() => ({ height: "100%", width: "100%" }), []);
const [columnDefs, setColumnDefs] = useState<ColDef[]>([
{ field: "athlete", minWidth: 170 },
{ field: "age" },
{ field: "country" },
{ field: "year" },
{ field: "date" },
{ field: "sport" },
{ field: "gold" },
{ field: "silver" },
{ field: "bronze" },
{ field: "total" },
]);
const theme = useMemo<Theme | "legacy">(() => {
return myTheme;
}, []);
const rowSelection = useMemo<
RowSelectionOptions | "single" | "multiple"
>(() => {
return { mode: "multiRow" };
}, []);
const defaultColDef = useMemo<ColDef>(() => {
return {
editable: true,
filter: true,
};
}, []);
const { data, loading } = useFetchJson<IOlympicData>(
"https://www.ag-grid.com/example-assets/olympic-winners.json",
);
const onFirstDataRendered = useCallback((params) => {
params.api.forEachNode((node) => {
if (
node.rowIndex === 2 ||
node.rowIndex === 3 ||
node.rowIndex === 4 ||
node.rowIndex === 5 ||
node.rowIndex === 6
) {
node.setSelected(true);
}
});
}, []);
return (
<AgGridProvider modules={modules}>
<div style={containerStyle}>
<div style={gridStyle}>
<AgGridReact<IOlympicData>
rowData={data}
loading={loading}
columnDefs={columnDefs}
theme={theme}
rowSelection={rowSelection}
defaultColDef={defaultColDef}
onFirstDataRendered={onFirstDataRendered}
/>
</div>
</div>
</AgGridProvider>
);
};
const root = createRoot(document.getElementById("root")!);
root.render(
<StrictMode>
<GridExample />
</StrictMode>,
);
export interface IOlympicData {
athlete: string,
age: number,
country: string,
year: number,
date: string,
sport: string,
gold: number,
silver: number,
bronze: number,
total: number
} import { useState, useEffect } from 'react';
/**
* Fetch example Json data
* Not recommended for production use!
*/
export const useFetchJson = <T,>(url:string, limit?: number) => {
const [data, setData] = useState<T[]>();
const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchData = async () => {
setLoading(true);
// Note error handling is omitted here for brevity
const response = await fetch(url);
const json = await response.json();
const data = limit ? json.slice(0, limit) : json;
setData(data);
setLoading(false);
};
fetchData();
}, [url, limit]);
return { data, loading };
}; Cell Selections Copy Link
Cell selections can be created by clicking and dragging on the grid. Copying from a selection will briefly highlight the range of cells (^ Ctrlâ Command+C). There are several parameters to control the selection and highlight style:
const myTheme = themeQuartz.withParams({
// colour and style of border around selection
rangeSelectionBorderColor: 'rgb(193, 0, 97)',
rangeSelectionBorderStyle: 'dashed',
// background colour of selection - you can use a semi-transparent colour
// and it wil overlay on top of the existing cells
rangeSelectionBackgroundColor: 'rgb(255, 0, 128, 0.1)',
// colour used to indicate that data has been copied from the cell range
rangeSelectionHighlightColor: 'rgb(60, 188, 0, 0.3)',
// alternating row colours will be visible through the semi-transparent
// selection background colour
oddRowBackgroundColor: '#8881',
});"use client";
import React, {
useCallback,
useMemo,
useRef,
useState,
StrictMode,
} from "react";
import { createRoot } from "react-dom/client";
import { AgGridReact, AgGridProvider } from "ag-grid-react";
import {
CellSelectionOptions,
ColDef,
ColGroupDef,
GridApi,
GridOptions,
GridReadyEvent,
ModuleRegistry,
Theme,
enableDevValidations,
themeQuartz,
} from "ag-grid-community";
import { AllEnterpriseModule } from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";
if (process.env.NODE_ENV !== "production") {
enableDevValidations();
}
const modules = [AllEnterpriseModule];
const myTheme = themeQuartz.withParams({
// color and style of border around selection
rangeSelectionBorderColor: "rgb(193, 0, 97)",
rangeSelectionBorderStyle: "dashed",
// background color of selection - you can use a semi-transparent color
// and it wil overlay on top of the existing cells
rangeSelectionBackgroundColor: "rgb(255, 0, 128, 0.1)",
// color used to indicate that data has been copied form the cell range
rangeSelectionHighlightColor: "rgb(60, 188, 0, 0.3)",
// alternating row colors will be visible through the semi-transparent
// selection background color
oddRowBackgroundColor: "#8881",
});
const GridExample = () => {
const containerStyle = useMemo(() => ({ width: "100%", height: "100%" }), []);
const gridStyle = useMemo(() => ({ height: "100%", width: "100%" }), []);
const [rowData, setRowData] = useState<IOlympicData[]>();
const theme = useMemo<Theme | "legacy">(() => {
return myTheme;
}, []);
const [columnDefs, setColumnDefs] = useState<ColDef[]>([
{ 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" },
]);
const defaultColDef = useMemo<ColDef>(() => {
return {
flex: 1,
minWidth: 100,
};
}, []);
const onGridReady = useCallback((params: GridReadyEvent) => {
fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
.then((resp) => resp.json())
.then((data: IOlympicData[]) => {
setRowData(data);
params.api.addCellRange({
rowStartIndex: 1,
rowEndIndex: 5,
columns: ["age", "country", "year", "date"],
});
});
}, []);
return (
<AgGridProvider modules={modules}>
<div style={containerStyle}>
<div style={gridStyle}>
<AgGridReact<IOlympicData>
rowData={rowData}
theme={theme}
columnDefs={columnDefs}
defaultColDef={defaultColDef}
cellSelection={true}
onGridReady={onGridReady}
/>
</div>
</div>
</AgGridProvider>
);
};
const root = createRoot(document.getElementById("root")!);
root.render(
<StrictMode>
<GridExample />
</StrictMode>,
);
export interface IOlympicData {
athlete: string,
age: number,
country: string,
year: number,
date: string,
sport: string,
gold: number,
silver: number,
bronze: number,
total: number
} Cell Selection for Integrated Charts Copy Link
When using integrated charts with cell selections, the grid uses different colors to indicate the purpose of the cell ranges:
rangeSelectionChartBackgroundColor- background color for cells used as chart datarangeSelectionChartCategoryBackgroundColor- background color for cells used as categories / axis labels