|
1
|
--- |
|
2
|
sidebar_label: "Data Table" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Data Table - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
This component is available after [Medusa v2.4.0+](https://github.com/medusajs/medusa/releases/tag/v2.4.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
The [DataTable component in Medusa UI](!ui!/components/data-table) allows you to display data in a table with sorting, filtering, and pagination. It's used across the Medusa Admin dashboard to showcase a list of items, such as a list of products. |
|
20
|
|
|
21
|
 |
|
22
|
|
|
23
|
You can use this component in your Admin Extensions to display data in a table format, especially if you're retrieving them from API routes of the Medusa application. |
|
24
|
|
|
25
|
This guide focuses on how to use the `DataTable` component while fetching data from the backend. Refer to the [Medusa UI documentation](!ui!/components/data-table) for detailed information about the DataTable component and its different usages. |
|
26
|
|
|
27
|
## Example: DataTable with Data Fetching |
|
28
|
|
|
29
|
In this example, you'll create a UI widget that shows the list of products retrieved from the [List Products API Route](!api!/admin/products/list-products) in a data table with pagination, filtering, searching, and sorting. |
|
30
|
|
|
31
|
Start by initializing the columns in the data table. To do that, use the `createDataTableColumnHelper` from Medusa UI: |
|
32
|
|
|
33
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
34
|
import { |
|
35
|
createDataTableColumnHelper, |
|
36
|
} from "@medusajs/ui" |
|
37
|
import { |
|
38
|
HttpTypes, |
|
39
|
} from "@medusajs/framework/types" |
|
40
|
|
|
41
|
const columnHelper = createDataTableColumnHelper<HttpTypes.AdminProduct>() |
|
42
|
|
|
43
|
const columns = [ |
|
44
|
columnHelper.accessor("title", { |
|
45
|
header: "Title", |
|
46
|
// Enables sorting for the column. |
|
47
|
enableSorting: true, |
|
48
|
// If omitted, the header will be used instead if it's a string, |
|
49
|
// otherwise the accessor key (id) will be used. |
|
50
|
sortLabel: "Title", |
|
51
|
// If omitted the default value will be "A-Z" |
|
52
|
sortAscLabel: "A-Z", |
|
53
|
// If omitted the default value will be "Z-A" |
|
54
|
sortDescLabel: "Z-A", |
|
55
|
}), |
|
56
|
columnHelper.accessor("status", { |
|
57
|
header: "Status", |
|
58
|
cell: ({ getValue }) => { |
|
59
|
const status = getValue() |
|
60
|
return ( |
|
61
|
<Badge color={status === "published" ? "green" : "grey"} size="xsmall"> |
|
62
|
{status === "published" ? "Published" : "Draft"} |
|
63
|
</Badge> |
|
64
|
) |
|
65
|
}, |
|
66
|
}), |
|
67
|
] |
|
68
|
``` |
|
69
|
|
|
70
|
`createDataTableColumnHelper` utility creates a column helper that helps you define the columns for the data table. The column helper has an `accessor` method that accepts two parameters: |
|
71
|
|
|
72
|
1. The column's key in the table's data. |
|
73
|
2. An object with the following properties: |
|
74
|
- `header`: The column's header. |
|
75
|
- `cell`: (optional) By default, a data's value for a column is displayed as a string. Use this property to specify custom rendering of the value. It accepts a function that returns a string or a React node. The function receives an object that has a `getValue` property function to retrieve the raw value of the cell. |
|
76
|
- `enableSorting`: (optional) A boolean that enables sorting data by this column. |
|
77
|
- `sortLabel`: (optional) The label for the sorting button. If omitted, the `header` will be used instead if it's a string, otherwise the accessor key (id) will be used. |
|
78
|
- `sortAscLabel`: (optional) The label for the ascending sorting button. If omitted, the default value will be "A-Z". |
|
79
|
- `sortDescLabel`: (optional) The label for the descending sorting button. If omitted, the default value will be "Z-A". |
|
80
|
|
|
81
|
Next, you'll define the filters that can be applied to the data table. You'll configure filtering by product status. |
|
82
|
|
|
83
|
To define the filters, add the following: |
|
84
|
|
|
85
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
86
|
// other imports... |
|
87
|
import { |
|
88
|
// ... |
|
89
|
createDataTableFilterHelper, |
|
90
|
} from "@medusajs/ui" |
|
91
|
|
|
92
|
const filterHelper = createDataTableFilterHelper<HttpTypes.AdminProduct>() |
|
93
|
|
|
94
|
const filters = [ |
|
95
|
filterHelper.accessor("status", { |
|
96
|
type: "select", |
|
97
|
label: "Status", |
|
98
|
options: [ |
|
99
|
{ |
|
100
|
label: "Published", |
|
101
|
value: "published", |
|
102
|
}, |
|
103
|
{ |
|
104
|
label: "Draft", |
|
105
|
value: "draft", |
|
106
|
}, |
|
107
|
], |
|
108
|
}), |
|
109
|
] |
|
110
|
``` |
|
111
|
|
|
112
|
`createDataTableFilterHelper` utility creates a filter helper that helps you define the filters for the data table. The filter helper has an `accessor` method that accepts two parameters: |
|
113
|
|
|
114
|
1. The key of a column in the table's data. |
|
115
|
2. An object with the following properties: |
|
116
|
- `type`: The type of filter. It can be either: |
|
117
|
- `select`: A select dropdown allowing users to choose multiple values. |
|
118
|
- `radio`: A radio button allowing users to choose one value. |
|
119
|
- `date`: A date picker allowing users to choose a date. |
|
120
|
- `label`: The filter's label. |
|
121
|
- `options`: An array of objects with `label` and `value` properties. The `label` is the option's label, and the `value` is the value to filter by. |
|
122
|
|
|
123
|
You'll now start creating the UI widget's component. Start by adding the necessary state variables: |
|
124
|
|
|
125
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
126
|
// other imports... |
|
127
|
import { |
|
128
|
// ... |
|
129
|
DataTablePaginationState, |
|
130
|
DataTableFilteringState, |
|
131
|
DataTableSortingState, |
|
132
|
} from "@medusajs/ui" |
|
133
|
import { useMemo, useState } from "react" |
|
134
|
|
|
135
|
// ... |
|
136
|
|
|
137
|
const limit = 15 |
|
138
|
|
|
139
|
const CustomPage = () => { |
|
140
|
const [pagination, setPagination] = useState<DataTablePaginationState>({ |
|
141
|
pageSize: limit, |
|
142
|
pageIndex: 0, |
|
143
|
}) |
|
144
|
const [search, setSearch] = useState<string>("") |
|
145
|
const [filtering, setFiltering] = useState<DataTableFilteringState>({}) |
|
146
|
const [sorting, setSorting] = useState<DataTableSortingState | null>(null) |
|
147
|
|
|
148
|
const offset = useMemo(() => { |
|
149
|
return pagination.pageIndex * limit |
|
150
|
}, [pagination]) |
|
151
|
const statusFilters = useMemo(() => { |
|
152
|
return (filtering.status || []) as ProductStatus |
|
153
|
}, [filtering]) |
|
154
|
|
|
155
|
// TODO add data fetching logic |
|
156
|
} |
|
157
|
``` |
|
158
|
|
|
159
|
In the component, you've added the following state variables: |
|
160
|
|
|
161
|
- `pagination`: An object of type `DataTablePaginationState` that holds the pagination state. It has two properties: |
|
162
|
- `pageSize`: The number of items to show per page. |
|
163
|
- `pageIndex`: The current page index. |
|
164
|
- `search`: A string that holds the search query. |
|
165
|
- `filtering`: An object of type `DataTableFilteringState` that holds the filtering state. |
|
166
|
- `sorting`: An object of type `DataTableSortingState` that holds the sorting state. |
|
167
|
|
|
168
|
You've also added two memoized variables: |
|
169
|
|
|
170
|
- `offset`: How many items to skip when fetching data based on the current page. |
|
171
|
- `statusFilters`: The selected status filters, if any. |
|
172
|
|
|
173
|
Next, you'll fetch the products from the Medusa application. Assuming you have the JS SDK configured as explained in [this guide](../../../js-sdk/page.mdx), add the following imports at the top of the file: |
|
174
|
|
|
175
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
176
|
import { sdk } from "../../lib/config" |
|
177
|
import { useQuery } from "@tanstack/react-query" |
|
178
|
``` |
|
179
|
|
|
180
|
This imports the JS SDK instance and `useQuery` from [Tanstack Query](https://tanstack.com/query/latest). |
|
181
|
|
|
182
|
Then, replace the `TODO` in the component with the following: |
|
183
|
|
|
184
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
185
|
const { data, isLoading } = useQuery({ |
|
186
|
queryFn: () => sdk.admin.product.list({ |
|
187
|
limit, |
|
188
|
offset, |
|
189
|
q: search, |
|
190
|
status: statusFilters, |
|
191
|
order: sorting ? `${sorting.desc ? "-" : ""}${sorting.id}` : undefined, |
|
192
|
}), |
|
193
|
queryKey: [["products", limit, offset, search, statusFilters, sorting?.id, sorting?.desc]], |
|
194
|
}) |
|
195
|
|
|
196
|
// TODO configure data table |
|
197
|
``` |
|
198
|
|
|
199
|
You use the `useQuery` hook to fetch the products from the Medusa application. In the `queryFn`, you call the `sdk.admin.product.list` method to fetch the products. You pass the following query parameters to the method: |
|
200
|
|
|
201
|
- `limit`: The number of products to fetch per page. |
|
202
|
- `offset`: The number of products to skip based on the current page. |
|
203
|
- `q`: The search query, if set. |
|
204
|
- `status`: The status filters, if set. |
|
205
|
- `order`: The sorting order, if set. |
|
206
|
|
|
207
|
So, whenever the user changes the current page, search query, status filters, or sorting, the products are fetched based on the new parameters. |
|
208
|
|
|
209
|
Next, you'll configure the data table. Medusa UI provides a `useDataTable` hook that helps you configure the data table. Add the following imports at the top of the file: |
|
210
|
|
|
211
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
212
|
import { |
|
213
|
// ... |
|
214
|
useDataTable, |
|
215
|
} from "@medusajs/ui" |
|
216
|
import { useNavigate } from "react-router-dom" |
|
217
|
``` |
|
218
|
|
|
219
|
Then, replace the `TODO` in the component with the following: |
|
220
|
|
|
221
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
222
|
const navigate = useNavigate() |
|
223
|
|
|
224
|
const table = useDataTable({ |
|
225
|
columns, |
|
226
|
data: data?.products || [], |
|
227
|
getRowId: (row) => row.id, |
|
228
|
rowCount: data?.count || 0, |
|
229
|
isLoading, |
|
230
|
pagination: { |
|
231
|
state: pagination, |
|
232
|
onPaginationChange: setPagination, |
|
233
|
}, |
|
234
|
search: { |
|
235
|
state: search, |
|
236
|
onSearchChange: setSearch, |
|
237
|
}, |
|
238
|
filtering: { |
|
239
|
state: filtering, |
|
240
|
onFilteringChange: setFiltering, |
|
241
|
}, |
|
242
|
filters, |
|
243
|
sorting: { |
|
244
|
// Pass the pagination state and updater to the table instance |
|
245
|
state: sorting, |
|
246
|
onSortingChange: setSorting, |
|
247
|
}, |
|
248
|
onRowClick: (event, row) => { |
|
249
|
// Handle row click, for example |
|
250
|
navigate(`/products/${row.id}`) |
|
251
|
}, |
|
252
|
}) |
|
253
|
|
|
254
|
// TODO render component |
|
255
|
``` |
|
256
|
|
|
257
|
The `useDataTable` hook accepts an object with the following properties: |
|
258
|
|
|
259
|
<TypeList |
|
260
|
types={[ |
|
261
|
{ name: "columns", type: "`array`", description: "The columns to display in the data table. You created this using the `createDataTableColumnHelper` utility." }, |
|
262
|
{ name: "data", type: "`array`", description: "The products fetched from the Medusa application." }, |
|
263
|
{ name: "getRowId", type: "`function`", description: "A function that returns the unique ID of a row." }, |
|
264
|
{ name: "rowCount", type: "`number`", description: "The total number of products that can be retrieved. This is used to determine the number of pages." }, |
|
265
|
{ name: "isLoading", type: "`boolean`", description: "A boolean that indicates if the data is being fetched." }, |
|
266
|
{ name: "pagination", type: "`object`", description: "An object to configure pagination.", children: [ |
|
267
|
{ name: "state", type: "`object`", description: "The pagination React state variable." }, |
|
268
|
{ name: "onPaginationChange", type: "`function`", description: "A function that updates the pagination state." } |
|
269
|
]}, |
|
270
|
{ name: "search", type: "`object`", description: "An object to configure searching.", children: [ |
|
271
|
{ name: "state", type: "`string`", description: "The search query React state variable." }, |
|
272
|
{ name: "onSearchChange", type: "`function`", description: "A function that updates the search query state." } |
|
273
|
]}, |
|
274
|
{ name: "filtering", type: "`object`", description: "An object to configure filtering.", children: [ |
|
275
|
{ name: "state", type: "`object`", description: "The filtering React state variable." }, |
|
276
|
{ name: "onFilteringChange", type: "`function`", description: "A function that updates the filtering state." } |
|
277
|
]}, |
|
278
|
{ name: "filters", type: "`array`", description: "The filters to display in the data table. You created this using the `createDataTableFilterHelper` utility." }, |
|
279
|
{ name: "sorting", type: "`object`", description: "An object to configure sorting.", children: [ |
|
280
|
{ name: "state", type: "`object`", description: "The sorting React state variable." }, |
|
281
|
{ name: "onSortingChange", type: "`function`", description: "A function that updates the sorting state." } |
|
282
|
]}, |
|
283
|
{ name: "onRowClick", type: "`function`", description: "A function that allows you to perform an action when the user clicks on a row. In this example, you navigate to the product's detail page.", children: [ |
|
284
|
{ name: "event", type: "`mouseevent`", description: "An instance of the [MouseClickEvent](https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent) object." }, |
|
285
|
{ name: "row", type: "`object`", description: "The data of the row that was clicked." } |
|
286
|
]} |
|
287
|
]} |
|
288
|
sectionTitle="Example: DataTable with Data Fetching" |
|
289
|
/> |
|
290
|
|
|
291
|
Finally, you'll render the data table. But first, add the following imports at the top of the page: |
|
292
|
|
|
293
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
294
|
import { |
|
295
|
// ... |
|
296
|
DataTable, |
|
297
|
} from "@medusajs/ui" |
|
298
|
import { SingleColumnLayout } from "../../layouts/single-column" |
|
299
|
import { Container } from "../../components/container" |
|
300
|
``` |
|
301
|
|
|
302
|
Aside from the `DataTable` component, you also import the [LayoutComposer](../../layouts/layout-composer/page.mdx) and [Container](../container/page.mdx) components to lay out the page. These components ensure a style consistent to other pages in the admin dashboard. |
|
303
|
|
|
304
|
Then, replace the `TODO` in the component with the following: |
|
305
|
|
|
306
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
307
|
return ( |
|
308
|
<SingleColumnLayout> |
|
309
|
<Container> |
|
310
|
<DataTable instance={table}> |
|
311
|
<DataTable.Toolbar className="flex flex-col items-start justify-between gap-2 md:flex-row md:items-center"> |
|
312
|
<Heading>Products</Heading> |
|
313
|
<div className="flex gap-2"> |
|
314
|
<DataTable.FilterMenu tooltip="Filter" /> |
|
315
|
<DataTable.SortingMenu tooltip="Sort" /> |
|
316
|
<DataTable.Search placeholder="Search..." /> |
|
317
|
</div> |
|
318
|
</DataTable.Toolbar> |
|
319
|
<DataTable.Table /> |
|
320
|
<DataTable.Pagination /> |
|
321
|
</DataTable> |
|
322
|
</Container> |
|
323
|
</SingleColumnLayout> |
|
324
|
) |
|
325
|
``` |
|
326
|
|
|
327
|
You render the `DataTable` component and pass the `table` instance as a prop. In the `DataTable` component, you render a toolbar showing a heading, filter menu, sorting menu, and a search input. You also show pagination after the table. |
|
328
|
|
|
329
|
Lastly, export the component and the UI widget's configuration at the end of the file: |
|
330
|
|
|
331
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
332
|
// other imports... |
|
333
|
import { defineRouteConfig } from "@medusajs/admin-sdk" |
|
334
|
import { ChatBubbleLeftRight } from "@medusajs/icons" |
|
335
|
|
|
336
|
// ... |
|
337
|
|
|
338
|
export const config = defineRouteConfig({ |
|
339
|
label: "Custom", |
|
340
|
icon: ChatBubbleLeftRight, |
|
341
|
}) |
|
342
|
|
|
343
|
export default CustomPage |
|
344
|
``` |
|
345
|
|
|
346
|
If you start your Medusa application and go to `localhost:9000/app/custom`, you'll see the data table showing the list of products with pagination, filtering, searching, and sorting functionalities. |
|
347
|
|
|
348
|
### Full Example Code |
|
349
|
|
|
350
|
```tsx title="src/admin/routes/custom/page.tsx" |
|
351
|
import { defineRouteConfig } from "@medusajs/admin-sdk" |
|
352
|
import { ChatBubbleLeftRight } from "@medusajs/icons" |
|
353
|
import { |
|
354
|
Badge, |
|
355
|
createDataTableColumnHelper, |
|
356
|
createDataTableFilterHelper, |
|
357
|
DataTable, |
|
358
|
DataTableFilteringState, |
|
359
|
DataTablePaginationState, |
|
360
|
DataTableSortingState, |
|
361
|
Heading, |
|
362
|
useDataTable, |
|
363
|
} from "@medusajs/ui" |
|
364
|
import { useQuery } from "@tanstack/react-query" |
|
365
|
import { SingleColumnLayout } from "../../layouts/single-column" |
|
366
|
import { sdk } from "../../lib/config" |
|
367
|
import { useMemo, useState } from "react" |
|
368
|
import { Container } from "../../components/container" |
|
369
|
import { HttpTypes, ProductStatus } from "@medusajs/framework/types" |
|
370
|
|
|
371
|
const columnHelper = createDataTableColumnHelper<HttpTypes.AdminProduct>() |
|
372
|
|
|
373
|
const columns = [ |
|
374
|
columnHelper.accessor("title", { |
|
375
|
header: "Title", |
|
376
|
// Enables sorting for the column. |
|
377
|
enableSorting: true, |
|
378
|
// If omitted, the header will be used instead if it's a string, |
|
379
|
// otherwise the accessor key (id) will be used. |
|
380
|
sortLabel: "Title", |
|
381
|
// If omitted the default value will be "A-Z" |
|
382
|
sortAscLabel: "A-Z", |
|
383
|
// If omitted the default value will be "Z-A" |
|
384
|
sortDescLabel: "Z-A", |
|
385
|
}), |
|
386
|
columnHelper.accessor("status", { |
|
387
|
header: "Status", |
|
388
|
cell: ({ getValue }) => { |
|
389
|
const status = getValue() |
|
390
|
return ( |
|
391
|
<Badge color={status === "published" ? "green" : "grey"} size="xsmall"> |
|
392
|
{status === "published" ? "Published" : "Draft"} |
|
393
|
</Badge> |
|
394
|
) |
|
395
|
}, |
|
396
|
}), |
|
397
|
] |
|
398
|
|
|
399
|
const filterHelper = createDataTableFilterHelper<HttpTypes.AdminProduct>() |
|
400
|
|
|
401
|
const filters = [ |
|
402
|
filterHelper.accessor("status", { |
|
403
|
type: "select", |
|
404
|
label: "Status", |
|
405
|
options: [ |
|
406
|
{ |
|
407
|
label: "Published", |
|
408
|
value: "published", |
|
409
|
}, |
|
410
|
{ |
|
411
|
label: "Draft", |
|
412
|
value: "draft", |
|
413
|
}, |
|
414
|
], |
|
415
|
}), |
|
416
|
] |
|
417
|
|
|
418
|
const limit = 15 |
|
419
|
|
|
420
|
const CustomPage = () => { |
|
421
|
const [pagination, setPagination] = useState<DataTablePaginationState>({ |
|
422
|
pageSize: limit, |
|
423
|
pageIndex: 0, |
|
424
|
}) |
|
425
|
const [search, setSearch] = useState<string>("") |
|
426
|
const [filtering, setFiltering] = useState<DataTableFilteringState>({}) |
|
427
|
const [sorting, setSorting] = useState<DataTableSortingState | null>(null) |
|
428
|
|
|
429
|
const offset = useMemo(() => { |
|
430
|
return pagination.pageIndex * limit |
|
431
|
}, [pagination]) |
|
432
|
const statusFilters = useMemo(() => { |
|
433
|
return (filtering.status || []) as ProductStatus |
|
434
|
}, [filtering]) |
|
435
|
|
|
436
|
const { data, isLoading } = useQuery({ |
|
437
|
queryFn: () => sdk.admin.product.list({ |
|
438
|
limit, |
|
439
|
offset, |
|
440
|
q: search, |
|
441
|
status: statusFilters, |
|
442
|
order: sorting ? `${sorting.desc ? "-" : ""}${sorting.id}` : undefined, |
|
443
|
}), |
|
444
|
queryKey: [["products", limit, offset, search, statusFilters, sorting?.id, sorting?.desc]], |
|
445
|
}) |
|
446
|
|
|
447
|
const table = useDataTable({ |
|
448
|
columns, |
|
449
|
data: data?.products || [], |
|
450
|
getRowId: (row) => row.id, |
|
451
|
rowCount: data?.count || 0, |
|
452
|
isLoading, |
|
453
|
pagination: { |
|
454
|
state: pagination, |
|
455
|
onPaginationChange: setPagination, |
|
456
|
}, |
|
457
|
search: { |
|
458
|
state: search, |
|
459
|
onSearchChange: setSearch, |
|
460
|
}, |
|
461
|
filtering: { |
|
462
|
state: filtering, |
|
463
|
onFilteringChange: setFiltering, |
|
464
|
}, |
|
465
|
filters, |
|
466
|
sorting: { |
|
467
|
// Pass the pagination state and updater to the table instance |
|
468
|
state: sorting, |
|
469
|
onSortingChange: setSorting, |
|
470
|
}, |
|
471
|
}) |
|
472
|
|
|
473
|
return ( |
|
474
|
<SingleColumnLayout> |
|
475
|
<Container> |
|
476
|
<DataTable instance={table}> |
|
477
|
<DataTable.Toolbar className="flex flex-col items-start justify-between gap-2 md:flex-row md:items-center"> |
|
478
|
<Heading>Products</Heading> |
|
479
|
<div className="flex gap-2"> |
|
480
|
<DataTable.FilterMenu tooltip="Filter" /> |
|
481
|
<DataTable.SortingMenu tooltip="Sort" /> |
|
482
|
<DataTable.Search placeholder="Search..." /> |
|
483
|
</div> |
|
484
|
</DataTable.Toolbar> |
|
485
|
<DataTable.Table /> |
|
486
|
<DataTable.Pagination /> |
|
487
|
</DataTable> |
|
488
|
</Container> |
|
489
|
</SingleColumnLayout> |
|
490
|
) |
|
491
|
} |
|
492
|
|
|
493
|
export const config = defineRouteConfig({ |
|
494
|
label: "Custom", |
|
495
|
icon: ChatBubbleLeftRight, |
|
496
|
}) |
|
497
|
|
|
498
|
export default CustomPage |
|
499
|
``` |