1 ---
2 sidebar_label: "Table Utilities"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `Table Utilities - Admin Components`,
9 }
10
11 # {metadata.title}
12
13 <Note>
14
15 These utilities are available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0).
16
17 </Note>
18
19 The `@medusajs/dashboard/lib` subpath exports the utilities that back the Medusa Admin's configurable tables. Use them to define which data a [ConfigurableDataTable](../../components/configurable-data-table/page.mdx) shows, and how it renders each cell.
20
21 ```tsx
22 import {
23 createTableAdapter,
24 defineCellRenderer,
25 } from "@medusajs/dashboard/lib"
26 ```
27
28 <Note title="Tip">
29
30 Refer to the [View Configurations guide](../../../commerce-modules/settings/configure-view-configurations/page.mdx) for full examples of both utilities.
31
32 </Note>
33
34 ---
35
36 ## createTableAdapter
37
38 Creates a type-safe table adapter that tells a `ConfigurableDataTable` which entity to render, how to fetch its data, and how each row behaves. The utility applies defaults for `getRowId`, `pageSize`, and `queryPrefix`, then merges your adapter on top of them.
39
40 ```tsx
41 import { createTableAdapter } from "@medusajs/dashboard/lib"
42 import { sdk } from "../lib/sdk"
43 import { useQuery } from "@tanstack/react-query"
44
45 export const brandTableAdapter = createTableAdapter({
46 entity: "brand",
47 entityName: "Brands",
48 useData: (fields, params) => {
49 const { data, isLoading, isError, error } = useQuery({
50 queryKey: ["brands", fields, params],
51 queryFn: () => sdk.client.fetch("/admin/brands", {
52 query: { ...params, fields },
53 }),
54 })
55
56 return {
57 data: data?.brands,
58 count: data?.count,
59 isLoading,
60 isError,
61 error,
62 }
63 },
64 getRowHref: (brand) => `/brands/${brand.id}`,
65 })
66 ```
67
68 ### Parameters
69
70 <TypeList
71 types={[
72 {
73 name: "adapter",
74 type: "`object`",
75 optional: false,
76 description: `The adapter's configuration. \`TData\` is the [row type](../../components/configurable-data-table/page.mdx#type-argument), which \`createTableAdapter\` infers from the \`data\` that \`useData\` returns.`,
77 children: [
78 {
79 name: "entity",
80 type: "`string`",
81 optional: false,
82 description: "The entity type whose columns the table requests from the Medusa backend, such as `orders`.",
83 },
84 {
85 name: "useData",
86 type: "`(fields: string, params: any) => object`",
87 optional: false,
88 description: "A hook that the table calls with the fields that the visible columns require and the current search parameters. It must return the following object.",
89 children: [
90 {
91 name: "data",
92 type: "`TData[]` \\| `undefined`",
93 optional: false,
94 description: "The records to show in the table.",
95 },
96 {
97 name: "count",
98 type: "`number` \\| `undefined`",
99 optional: false,
100 description: "The total number of records across all pages, used for pagination.",
101 },
102 {
103 name: "isLoading",
104 type: "`boolean`",
105 optional: false,
106 description: "Whether the records are loading. The table shows its loading skeleton while this is `true`.",
107 },
108 {
109 name: "isError",
110 type: "`boolean`",
111 optional: false,
112 description: "Whether fetching the records failed.",
113 },
114 {
115 name: "error",
116 type: "`any`",
117 optional: false,
118 description: "The error thrown while fetching the records, if any. The table throws it so that the closest error boundary handles it.",
119 },
120 ],
121 },
122 {
123 name: "getRowId",
124 type: "`(row: TData) => string`",
125 optional: true,
126 description: "Returns a row's unique ID. Defaults to the row's `id` property.",
127 },
128 {
129 name: "getRowHref",
130 type: "`(row: TData) => string` \\| `undefined`",
131 optional: true,
132 description: "Returns the URL a row links to. Return `undefined` to make the row non-clickable.",
133 },
134 {
135 name: "filters",
136 type: "`DataTableFilter[]`",
137 optional: true,
138 description: `The table's filters. When you omit it, the table resolves the filters from the entity's columns. Refer to [Medusa UI](!ui!/components/data-table) for the type's details.`,
139 },
140 {
141 name: "transformColumns",
142 type: "`(columns: AdminColumn[]) => AdminColumn[]`",
143 optional: true,
144 description: "Transforms the columns that the Medusa backend returns, such as to disable sorting or filtering on a column.",
145 },
146 {
147 name: "getColumns",
148 type: "`(apiColumns: any[]) => DataTableColumnDef<TData, any>[]`",
149 optional: true,
150 description: "Builds the table's columns yourself instead of using the default column generation.",
151 },
152 {
153 name: "getColumnAlignment",
154 type: "`(column: AdminColumn) => DataTableColumnAlignment` \\| `undefined`",
155 optional: true,
156 description: "Overrides a column's alignment. Return `undefined` to use the render mode's alignment.",
157 },
158 {
159 name: "emptyState",
160 type: "`DataTableEmptyStateProps`",
161 optional: true,
162 description: `The content the table shows when there are no rows. Refer to [Medusa UI](!ui!/components/data-table) for the type's details.`,
163 },
164 {
165 name: "pageSize",
166 type: "`number`",
167 optional: true,
168 defaultValue: "20",
169 description: "The number of rows per page.",
170 },
171 {
172 name: "queryPrefix",
173 type: "`string`",
174 optional: true,
175 defaultValue: "\"\"",
176 description: "A prefix for the table's query parameters. Set it when a page renders more than one table.",
177 },
178 {
179 name: "viewConfigurationKey",
180 type: "`string`",
181 optional: true,
182 description: "The key that saved view configurations are stored under. Set it when more than one table renders the same entity. Defaults to `entity`.",
183 },
184 {
185 name: "entityName",
186 type: "`string`",
187 optional: true,
188 description: "The entity's display name, used in the table's headings.",
189 },
190 {
191 name: "renderRowActions",
192 type: "`(row: TData) => ReactNode`",
193 optional: true,
194 description: "Renders a row's actions. When you pass it, the table adds a trailing actions column.",
195 },
196 {
197 name: "defaultFilters",
198 type: "`Record<string, any>`",
199 optional: true,
200 description: "Filters applied when no saved view configuration is active. An active view's filters take precedence.",
201 },
202 {
203 name: "filterParamMap",
204 type: "`Record<string, string>`",
205 optional: true,
206 description: "Maps a filter's ID to the query parameter key that the list route accepts, such as `sales_channels.id` to `sales_channel_id`.",
207 },
208 {
209 name: "enableRowSelection",
210 type: "`boolean`",
211 optional: true,
212 description: "Whether to add a leading checkbox column. Required to use `commands`.",
213 },
214 {
215 name: "commands",
216 type: "`DataTableCommand[]`",
217 optional: true,
218 description: `The bulk actions that operate on the selected rows. Refer to [Medusa UI](!ui!/components/data-table) for the type's details.`,
219 },
220 ],
221 },
222 ]}
223 />
224
225 ### Returns
226
227 Returns the same `TableAdapter<TData>` object with the default `getRowId`, `pageSize`, and `queryPrefix` applied. Pass it to a [ConfigurableDataTable](../../components/configurable-data-table/page.mdx).
228
229 ---
230
231 ## defineCellRenderer
232
233 Registers a cell renderer for a render mode in the global registry. A column opts into the renderer through its render mode. Registering an existing render mode replaces it, which lets you override a built-in renderer.
234
235 Call it once when your [admin extension](!docs!/learn/fundamentals/admin) loads.
236
237 ```tsx
238 import { defineCellRenderer } from "@medusajs/dashboard/lib"
239 import { Badge } from "@medusajs/ui"
240
241 defineCellRenderer("brand_tier", {
242 render: (value, row, column, t) => (
243 <Badge size="xsmall">{value ?? "-"}</Badge>
244 ),
245 align: "center",
246 truncateTooltip: false,
247 })
248 ```
249
250 ### Parameters
251
252 <TypeList
253 types={[
254 {
255 name: "type",
256 type: "`RenderMode`",
257 optional: false,
258 description: "The render mode to register the renderer for. Pass a `BuiltInRenderMode` to replace a built-in renderer, or any other string for a custom mode.",
259 },
260 {
261 name: "def",
262 type: "`object`",
263 optional: false,
264 description: "The renderer's definition.",
265 children: [
266 {
267 name: "render",
268 type: "`(value, row, column, t) => React.ReactNode`",
269 optional: false,
270 description: "The function that renders the cell's content.",
271 children: [
272 {
273 name: "value",
274 type: "`any`",
275 optional: false,
276 description: "The value of the row's field that the column points to.",
277 },
278 {
279 name: "row",
280 type: "`TData`",
281 optional: false,
282 description: "The full row that the cell belongs to.",
283 },
284 {
285 name: "column",
286 type: "`AdminColumn`",
287 optional: false,
288 description: "The column's definition, including its `metadata` property.",
289 },
290 {
291 name: "t",
292 type: "`TFunction`",
293 optional: false,
294 description: "The i18next translation function.",
295 },
296 ],
297 },
298 {
299 name: "align",
300 type: "`\"left\"` \\| `\"center\"` \\| `\"right\"`",
301 optional: true,
302 description: "The alignment the renderer reads best at. The adapter's `getColumnAlignment` overrides it.",
303 },
304 {
305 name: "truncateTooltip",
306 type: "`boolean`",
307 optional: true,
308 defaultValue: "true",
309 description: "Whether the table shows a tooltip with the full value when the cell's content is truncated. Set it to `false` when the renderer handles its own overflow.",
310 },
311 ],
312 },
313 ]}
314 />