Raw
1 ---
2 sidebar_label: "Data Table Hooks"
3 ---
4
5 import { TypeList } from "docs-ui"
6
7 export const metadata = {
8 title: `Data Table Hooks - Admin Components`,
9 }
10
11 # {metadata.title}
12
13 <Note>
14
15 These hooks are available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0).
16
17 </Note>
18
19 The data table hooks build the columns, filters, query parameters, and empty state of a [DataTable](!ui!/components/data-table) component. The Medusa Admin uses them in its list pages, such as the sales channels list, to keep tables consistent across the dashboard.
20
21 ```tsx
22 import {
23 useDataTableDateColumns,
24 useDataTableDateFilters,
25 useSalesChannelTableColumns,
26 useSalesChannelTableEmptyState,
27 useSalesChannelTableFilters,
28 useSalesChannelTableQuery,
29 } from "@medusajs/dashboard/hooks"
30 ```
31
32 ---
33
34 ## useDataTableDateColumns
35
36 The `useDataTableDateColumns` hook returns the sortable `created_at` and `updated_at` columns of a data table. Each column shows the formatted date and a tooltip with the date and time, both translated to the Medusa Admin's active language.
37
38 ### Example
39
40 Append the date columns to your table's own columns:
41
42 ```tsx
43 import {
44 useDataTableDateColumns,
45 } from "@medusajs/dashboard/hooks"
46 import { createDataTableColumnHelper } from "@medusajs/ui"
47 import { useMemo } from "react"
48
49 const columnHelper = createDataTableColumnHelper()
50
51 const useColumns = () => {
52 const dateColumns = useDataTableDateColumns()
53
54 return useMemo(
55 () => [
56 columnHelper.accessor("name", {
57 header: "Name",
58 enableSorting: true,
59 }),
60 ...dateColumns,
61 ],
62 [dateColumns]
63 )
64 }
65 ```
66
67 The hook's type parameter is the table's row type, which must have the `created_at` and `updated_at` properties. For example, use `useDataTableDateColumns<HttpTypes.AdminProduct>()`.
68
69 ### Returns
70
71 Returns a `DataTableColumnDef<TData>[]` with the `created_at` and `updated_at` column definitions, in that order. Both columns have sorting enabled. Pass them in the `columns` prop of the `DataTable` component. `TData` is the type of a single row in the table.
72
73 ---
74
75 ## useDataTableDateFilters
76
77 The `useDataTableDateFilters` hook returns the `created_at` and `updated_at` filters of a data table. Each filter has the presets today, last seven days, last thirty days, last ninety days, and last twelve months, along with a custom range option.
78
79 ### Example
80
81 Append the date filters to your table's own filters:
82
83 ```tsx
84 import {
85 useDataTableDateFilters,
86 } from "@medusajs/dashboard/hooks"
87 import { createDataTableFilterHelper } from "@medusajs/ui"
88 import { useMemo } from "react"
89
90 const filterHelper = createDataTableFilterHelper()
91
92 const useFilters = () => {
93 const dateFilters = useDataTableDateFilters()
94
95 return useMemo(
96 () => [
97 filterHelper.accessor("status", {
98 label: "Status",
99 type: "radio",
100 options: [
101 { label: "Draft", value: "draft" },
102 { label: "Published", value: "published" },
103 ],
104 }),
105 ...dateFilters,
106 ],
107 [dateFilters]
108 )
109 }
110 ```
111
112 The applied filters are set as query parameters whose values are JSON strings, such as `{"$gte":"2025-04-01T00:00:00.000Z"}`. Parse them with `JSON.parse` before you send them to the Medusa backend.
113
114 ### Parameters
115
116 <TypeList
117 types={[
118 {
119 name: "disableRangeOption",
120 type: "`boolean`",
121 optional: true,
122 defaultValue: "false",
123 description: "Whether to remove the custom range option, so that the user can only choose one of the presets.",
124 },
125 ]}
126 />
127
128 ### Returns
129
130 Returns a `DataTableFilter[]` with the `created_at` and `updated_at` filter definitions, in that order. Pass them in the `filters` prop of the `DataTable` component.
131
132 ---
133
134 ## useSalesChannelTableColumns
135
136 The `useSalesChannelTableColumns` hook returns the columns of a sales channels data table. The Medusa Admin uses it in the sales channels list page and in the forms that select a product's sales channels.
137
138 ### Example
139
140 ```tsx
141 import { DataTable } from "@medusajs/dashboard/components"
142 import {
143 useSalesChannels,
144 useSalesChannelTableColumns,
145 } from "@medusajs/dashboard/hooks"
146
147 const SalesChannelTable = () => {
148 const columns = useSalesChannelTableColumns()
149 const { sales_channels, count, isPending } =
150 useSalesChannels()
151
152 return (
153 <DataTable
154 data={sales_channels ?? []}
155 columns={columns}
156 rowCount={count}
157 getRowId={(row) => row.id}
158 isLoading={isPending}
159 />
160 )
161 }
162 ```
163
164 ### Returns
165
166 Returns a `DataTableColumnDef<HttpTypes.AdminSalesChannel>[]` with the sortable `name`, `description`, and `is_disabled` columns, followed by the `created_at` and `updated_at` columns of the `useDataTableDateColumns` hook. The `is_disabled` column renders a status cell that shows whether the sales channel is enabled or disabled.
167
168 ---
169
170 ## useSalesChannelTableFilters
171
172 The `useSalesChannelTableFilters` hook returns the filters of a sales channels data table.
173
174 ### Example
175
176 ```tsx
177 import { DataTable } from "@medusajs/dashboard/components"
178 import {
179 useSalesChannelTableColumns,
180 useSalesChannelTableFilters,
181 } from "@medusajs/dashboard/hooks"
182
183 const SalesChannelTable = ({ salesChannels, count }) => {
184 const columns = useSalesChannelTableColumns()
185 const filters = useSalesChannelTableFilters()
186
187 return (
188 <DataTable
189 data={salesChannels}
190 columns={columns}
191 filters={filters}
192 rowCount={count}
193 getRowId={(row) => row.id}
194 />
195 )
196 }
197 ```
198
199 ### Returns
200
201 Returns a `DataTableFilter[]` with an `is_disabled` radio filter that has the enabled and disabled options, followed by the `created_at` and `updated_at` filters of the `useDataTableDateFilters` hook.
202
203 ---
204
205 ## useSalesChannelTableQuery
206
207 The `useSalesChannelTableQuery` hook reads the sales channel table's query parameters from the URL and returns the parameters to pass to the list sales channels request.
208
209 ### Example
210
211 ```tsx
212 import {
213 useSalesChannels,
214 useSalesChannelTableQuery,
215 } from "@medusajs/dashboard/hooks"
216
217 const PAGE_SIZE = 20
218
219 const SalesChannelTable = () => {
220 const searchParams = useSalesChannelTableQuery({
221 pageSize: PAGE_SIZE,
222 })
223
224 const { sales_channels, count } =
225 useSalesChannels(searchParams)
226
227 // ...
228 }
229 ```
230
231 The hook reads the `offset`, `q`, `order`, `created_at`, `updated_at`, and `is_disabled` query parameters, and it parses the values that aren't strings.
232
233 ### Parameters
234
235 The hook accepts an object with the following properties:
236
237 <TypeList
238 types={[
239 {
240 name: "prefix",
241 type: "`string`",
242 optional: false,
243 description: "A prefix for the table's query parameters. Pass it when the page has more than one table, so that each table has its own query parameters.",
244 },
245 {
246 name: "pageSize",
247 type: "`number`",
248 optional: false,
249 description: "The number of sales channels per page, which the hook sets as the request's `limit`. The default value is `20`.",
250 },
251 ]}
252 />
253
254 ### Returns
255
256 Returns an `HttpTypes.AdminSalesChannelListParams` object with the query parameters of the list sales channels request, including `limit`, `offset`, `q`, `order`, `created_at`, `updated_at`, and `is_disabled`.
257
258 ---
259
260 ## useSalesChannelTableEmptyState
261
262 The `useSalesChannelTableEmptyState` hook returns the translated empty state content of a sales channels data table.
263
264 ### Example
265
266 ```tsx
267 import { DataTable } from "@medusajs/dashboard/components"
268 import {
269 useSalesChannelTableColumns,
270 useSalesChannelTableEmptyState,
271 } from "@medusajs/dashboard/hooks"
272
273 const SalesChannelTable = ({ salesChannels, count }) => {
274 const columns = useSalesChannelTableColumns()
275 const emptyState = useSalesChannelTableEmptyState()
276
277 return (
278 <DataTable
279 data={salesChannels}
280 columns={columns}
281 rowCount={count}
282 getRowId={(row) => row.id}
283 emptyState={emptyState}
284 />
285 )
286 }
287 ```
288
289 ### Returns
290
291 Returns a `DataTableEmptyStateProps` object with an `empty` property, shown when the store has no sales channels, and a `filtered` property, shown when the applied filters match no sales channels. Each has a `heading` and a `description`.