Grid API
The IOGridApi<T> interface provides imperative methods for controlling the grid programmatically. Access it via a React ref on the OGrid component.
Accessing the API
import { useRef } from 'react';
import { OGrid, IOGridApi } from '@alaarab/ogrid-react-radix';
interface Employee {
id: number;
name: string;
department: string;
}
function EmployeeGrid() {
const apiRef = useRef<IOGridApi<Employee>>(null);
const handleClearFilters = () => {
apiRef.current?.setFilterModel({});
};
return (
<>
<button onClick={handleClearFilters}>Clear Filters</button>
<OGrid<Employee>
ref={apiRef}
columns={columns}
data={data}
getRowId={(e) => e.id}
/>
</>
);
}
IOGridApi<T>
interface IOGridApi<T> {
setRowData: (data: T[]) => void;
setLoading: (loading: boolean) => void;
getColumnState: () => IGridColumnState;
applyColumnState: (state: Partial<IGridColumnState>) => void;
setFilterModel: (filters: IFilters) => void;
getSelectedRows: () => RowId[];
setSelectedRows: (rowIds: RowId[]) => void;
selectAll: () => void;
deselectAll: () => void;
}
Data Methods
| Method | Signature | Description |
|---|---|---|
setRowData | (data: T[]) => void | Replaces the grid's data with a new array. Useful for client-side mode when you want to update data imperatively rather than through the data prop. |
setLoading | (loading: boolean) => void | Sets the loading state of the grid. When true, the grid displays a loading indicator. |
Column State Methods
| Method | Signature | Description |
|---|---|---|
getColumnState | () => IGridColumnState | Returns the current column state including visibility, sort, order, widths, and filters. See IGridColumnState below. |
applyColumnState | (state: Partial<IGridColumnState>) => void | Applies a partial column state. Any fields not included in the object are left unchanged. Useful for restoring saved state. |
Filter Methods
| Method | Signature | Description |
|---|---|---|
setFilterModel | (filters: IFilters) => void | Sets the filter state. Pass an empty object {} to clear all filters. See Types for IFilters. |
Row Selection Methods
| Method | Signature | Description |
|---|---|---|
getSelectedRows | () => RowId[] | Returns an array of selected row IDs. |
setSelectedRows | (rowIds: RowId[]) => void | Sets the selected rows to the given IDs. Any previously selected rows not in the array are deselected. |
selectAll | () => void | Selects all rows in the current data set. |
deselectAll | () => void | Deselects all rows. |
IGridColumnState
Represents the complete column state of the grid. Returned by getColumnState() and accepted by applyColumnState().
interface IGridColumnState {
visibleColumns: string[];
sort?: { field: string; direction: 'asc' | 'desc' };
columnOrder?: string[];
columnWidths?: Record<string, number>;
filters?: IFilters;
}
| Name | Type | Description |
|---|---|---|
visibleColumns | string[] | Array of visible column IDs. |
sort | { field: string; direction: 'asc' | 'desc' } | Current sort configuration, or undefined if no sort is active. |
columnOrder | string[] | Current column display order as an array of column IDs. |
columnWidths | Record<string, number> | Map of column IDs to their current pixel widths. Only includes columns that have been explicitly sized. |
filters | IFilters | Current filter state. |