ഉള്ളടക്കത്തിലേക്ക് പോകുക
ബീറ്റ · പരീക്ഷണാത്മകംReact 19പിയർ ഡിപൻഡൻസികളില്ല

Grid API & Export

The imperative ref API, CSV/Excel/PDF export, and the framework-free utilities.

ബീറ്റ കമ്പോണന്റുകൾ മാറാൻ സാധ്യതയുണ്ട്; അവ നിങ്ങളുടെ കോഡ് തകരാറിലാക്കിയേക്കാം. സ്വന്തം ഉത്തരവാദിത്തത്തിൽ ഉപയോഗിക്കുക, ബഗ് ട്രാക്കർ വഴി അഭിപ്രായം അറിയിക്കുക.

ഈ പേജ് ഇതുവരെ വിവർത്തനം ചെയ്തിട്ടില്ല, അതിനാൽ ഇംഗ്ലീഷ് പതിപ്പാണ് താഴെ കാണിക്കുന്നത്.
ഈ പേജിൽ

Driving the grid from code, and getting data back out of it. Start at Data Grid for installation and the props table.

Grid API (ref)

tsx
const gridRef = useRef<GridApi<Employee>>(null);
 
<DataGrid ref={gridRef} data={data} columns={columns} />;
 
gridRef.current?.toggleSort("salary");

State

MethodSignatureDescription
getState() => GridStateCurrent state, including controlled keys.
setState(updater: (prev: GridState) => GridState) => voidUpdate any part of the state.

Sorting, filtering and pagination

MethodSignatureDescription
toggleSort(columnId: string, multi?: boolean) => voidCycle a column through ascending, descending and off.
setSorting(sorting: SortItem[]) => voidReplace all sorts.
setFilter(columnId: string, filter: Omit<ColumnFilter, "columnId"> | null) => voidSet or remove (null) a column filter.
clearFilters() => voidRemove all column filters and the search text.
setGlobalFilter(value: string) => voidSet the search text.
setPageIndex(pageIndex: number) => voidGo to a page (zero-based).
setPageSize(pageSize: number) => voidChange the page size, keeping the first visible row on screen.
setDensity(density: "compact" | "standard" | "comfortable") => voidChange row density.

Selection

MethodSignatureDescription
isRowSelectable(row: T) => booleanWhether enableRowSelection allows this row.
toggleRowSelected(rowId: string, options?: { value?: boolean; range?: boolean }) => voidToggle (or set with value) a row. range: true selects from the last toggled row.
toggleAllRowsSelected(value: boolean) => voidSelect or deselect all rows matching the filters (current page in server mode). Multiple mode only.
clearSelection() => voidDeselect everything.
getSelectedRowIds() => string[]Selected ids, including rows not in the current data.
getSelectedRows() => T[]Selected rows that exist in the current data.

Columns

MethodSignatureDescription
setColumnVisibility(columnId: string, visible: boolean) => voidShow or hide a column.
setColumnWidth(columnId: string, width: number | null) => voidSet a width, or null to reset to the column's width.
pinColumn(columnId: string, side: "left" | "right" | false) => voidPin to the start or end, or unpin.
moveColumn(columnId: string, targetId: string, placement: "before" | "after") => voidMove a column next to another. It takes the target's pin side.
resetColumns() => voidRestore order, visibility, widths and pinning from the column definitions.
MethodSignatureDescription
scrollToRow(rowIndex: number) => voidScroll a row of the current page into view.
focusCell(rowIndex: number, columnId: string) => voidScroll to and focus a cell. rowIndex is within the current page; -1 is the header.
startEditing(rowId: string, columnId: string) => voidOpen the editor for an editable cell.
cancelEditing() => voidClose the open editor without saving.

Export

MethodSignatureDescription
getRows(scope?: ExportScope) => T[]Rows for a scope (default "filtered").
exportCsv(options?: ExportOptions<T>) => Promise<void>Download a CSV file.
exportExcel(options?: ExportOptions<T>) => Promise<void>Download an .xlsx file.
exportPdf(options?: PdfExportOptions<T>) => Promise<void>Download a .pdf file.
print(options?: PrintExportOptions<T>) => Promise<void>Open the browser's print dialog with a table laid out for paper.
copyToClipboard() => Promise<void>Copy selected rows as tab-separated text, or the focused cell's text.

Export

FormatDetails
CSVUTF-8 with a byte order mark so Excel opens it correctly. Uses display text (format). Text starting with =, +, - or @ is prefixed with ' so spreadsheets don't run it as a formula.
ExcelA real .xlsx file: numbers, booleans and dates keep their types, the header row is bold and frozen, with auto-filter and column widths. No library is needed.
PDFA real .pdf file, written by the grid: chosen paper size and orientation, a repeated header row, page numbers and your own header and footer. Uses the standard PDF fonts, so nothing is embedded and text must be Latin-1 (see below). No library is needed.
Printapi.print() opens the browser's print dialog instead, for paper or for text the standard fonts cannot encode.

Exports include the visible columns in their current order, excluding columns with exportable: false. Export code is only downloaded the first time someone exports.

tsx
gridRef.current?.exportExcel({ fileName: "employees", scope: "selected" });
gridRef.current?.exportPdf({
  title: "Employee report",
  orientation: "portrait",
  paperSize: "A4",
  footer: { left: "Confidential", right: "Page {page} of {pages}" },
});
gridRef.current?.exportCsv({ rows: allRowsFromServer });
ExportOptionsTypeDefaultDescription
fileNamestringexportFileNameFile name without extension.
scope"filtered" | "all" | "selected" | "page""filtered"filtered: rows matching filters, in sorted order. all: every row in data. selected: selected rows. page: the current page.
rowsT[]—Export these rows instead of a scope.
titlestringthe file namePDF only: heading and default PDF file name.
orientation"portrait" | "landscape""landscape"PDF only.
paperSize"A3" | "A4" | "A5" | "letter" | "legal""A4"PDF only.
marginnumber36PDF only: page margin in points, 72 to the inch.
fontSizenumber9PDF only: body text size in points. Row height and the bands follow from it.
headerPdfBand | null{ left: "{title}" }PDF only: repeated at the top of every page. null removes it.
footerPdfBand | nulldate and page numbersPDF only: repeated at the bottom of every page. null removes it.
themePartial<PdfTheme>zincPDF only: text, muted, border, headerBg, headerText and stripe as #rrggbb. stripe: null turns off row striping.

A band has a left, center and right slot. Each takes plain text, with these tokens substituted as the page is drawn:

TokenBecomes
{title}The title option.
{page}The current page number.
{pages}The total page count.
{date}Today, in the browser's locale.
{time}Now, in the browser's locale.
tsx
gridRef.current?.exportPdf({
  title: "Q3 headcount",
  header: { left: "{title}", right: "Acme Inc." },
  footer: { left: "Generated {date}", right: "Page {page} of {pages}" },
  theme: { headerBg: "#e0f2fe", stripe: null },
});
The PDF uses Helvetica, which every viewer provides, so no font is embedded and the file stays small. That encoding covers Latin-1 and common typographic characters; anything outside it, such as Malayalam or CJK, is written as '?'. Use api.print() for those.

Cells are a single line and are clipped with an ellipsis when they do not fit, so every row is the same height and a page break never lands inside one. Column proportions follow the grid's own widths, scaled to the page.

Utilities

Everything below is exported from @/component-lib/data-grid. The same functions (without React) are exported from @/component-lib/data-grid/core, which is safe to import in server code.

ExportSignatureDescription
createColumnHelper<T>() => { field, accessor, display }Typed column builders.
filterRows(rows, columns, filters, globalFilter, formatters) => GridRow<T>[]Apply column filters and search, like client mode.
sortRows(rows, columns, sorting) => GridRow<T>[]Stable multi-column sort, like client mode.
paginate(rows, pagination, { enabled, server, rowCount? }) => PageResult<T>Slice a page and compute pageCount, rowCount, pageOffset.
buildRows(data, getRowId) => GridRow<T>[]Wrap data as { id, index, original } rows.
createRowIdGetter(getRowId?) => (row, index) => stringTurn a getRowId prop value into a function.
resolveColumns(defs: ColumnDef<T>[]) => ResolvedColumn<T>[]Apply column defaults.
createFormatters(locale?, labels?) => FormattersDate and yes/no formatting used by filters, search and export.
formatCellValue(column, value, row, formatters) => stringDisplay text of a cell.
getFilterOperators(column) => FilterOperator[]Operators available for a column.
isFilterActive(filter) => booleanWhether a filter has a usable value.
toggleSorting(sorting, columnId, multi) => SortItem[]The header-click sort cycle.
createInitialState(options) => GridStateThe grid's starting state for a set of props.
compileFilter(column, filter, formatters) => ((row: T) => boolean) | nullBuild a predicate for one filter (null when the filter is inactive).
computeLayout(columns, state, leading?) => ColumnLayout<T>Column order, widths and pin sections for a state. Used to build custom grid UIs.
createGridEngine(options) => GridEngine<T>The state engine behind DataGrid (store, selection, editing, navigation, export). Used to build custom grid UIs.
defaultLocaleTextLocaleTextDefault English text.

All types (ColumnDef, GridState, GridQuery, GridApi, SortItem, ColumnFilter, CellContext, EditorProps, CellEditEvent, ExportOptions, LocaleText, …) are exported as well.