|
1
|
--- |
|
2
|
sidebar_label: "Configure View Configurations" |
|
3
|
tags: |
|
4
|
- settings |
|
5
|
- how to |
|
6
|
- server |
|
7
|
- admin |
|
8
|
products: |
|
9
|
- settings |
|
10
|
--- |
|
11
|
|
|
12
|
import { Prerequisites, Table } from "docs-ui" |
|
13
|
|
|
14
|
export const metadata = { |
|
15
|
title: `Configure View Configurations`, |
|
16
|
} |
|
17
|
|
|
18
|
# {metadata.title} |
|
19
|
|
|
20
|
In this guide, you'll learn how to configure the data tables that use view configurations in the dashboard. You'll learn how to set the default columns and their order, add computed columns, change how a column's value renders, and show a configurable data table in your admin customizations. |
|
21
|
|
|
22
|
<Prerequisites |
|
23
|
items={[ |
|
24
|
{ |
|
25
|
text: "Medusa v2.18.0 or later", |
|
26
|
link: "https://github.com/medusajs/medusa/releases/tag/v2.18.0" |
|
27
|
}, |
|
28
|
{ |
|
29
|
text: "View configurations feature flag enabled", |
|
30
|
link: "../view-configurations/page.mdx#enable-the-feature-flag" |
|
31
|
} |
|
32
|
]} |
|
33
|
/> |
|
34
|
|
|
35
|
## How Column Configurations are Generated |
|
36
|
|
|
37
|
The Settings Module discovers the entities in your application from the registered modules' [query graph](!docs!/learn/fundamentals/query), including your custom modules' data models. For each entity, it generates the column configurations that are used in its configurable data table in the Medusa Admin dashboard. |
|
38
|
|
|
39
|
You can then override these generated column configurations, as explained in the following sections. You can change the default visible columns, their order, how their values render, and add computed columns. |
|
40
|
|
|
41
|
<Note title="Glossary"> |
|
42
|
|
|
43
|
In this guide, the terms "entity" and "data model" are used interchangeably. They both refer to a [data model](!docs!/learn/fundamentals/data-models) defined in a module, such as a core data model like `Product` or a custom one like `Brand`. |
|
44
|
|
|
45
|
</Note> |
|
46
|
|
|
47
|
--- |
|
48
|
|
|
49
|
## How to Override Entity Column Configurations |
|
50
|
|
|
51
|
In the Settings Module's configurations, you can override the generated columns of a core or custom data model. These overrides are applied to the configurable data table in the Medusa Admin dashboard when the view configuration feature is enabled, and only to the system default view configuration. |
|
52
|
|
|
53
|
<Note title="Tip"> |
|
54
|
|
|
55
|
Admin users can still customize the columns of a data table in the dashboard, and their customizations are saved either as system default or user-specific view configurations. The overrides you define here are applied only if the user hasn't customized the columns of the data table. |
|
56
|
|
|
57
|
</Note> |
|
58
|
|
|
59
|
In this section, you'll learn how to configure the default columns of a custom data model `Brand` defined in a custom module. You can use the same approach to configure the default columns of any core or custom data model. The sections after this one explain all the different configuration overrides you can set for an entity's columns. |
|
60
|
|
|
61
|
<Prerequisites |
|
62
|
items={[ |
|
63
|
{ |
|
64
|
text: "A custom module with a data model defined", |
|
65
|
link: "!docs!/learn/customization/custom-features/module" |
|
66
|
} |
|
67
|
]} |
|
68
|
/> |
|
69
|
|
|
70
|
### Step 1: Configure the Settings Module |
|
71
|
|
|
72
|
To override the generated columns of an entity, pass the `entityOverrides` option to the Settings Module in `medusa-config.ts`. Its value is an object whose keys are data model names as passed to the `MedusaService` extended by the module's service, and whose values are the entity's override configuration. |
|
73
|
|
|
74
|
<Note title="Tip"> |
|
75
|
|
|
76
|
For core data models, the entity key is the pascal case name of the entity, such as `Product`, `Order`, or `SalesChannel`. |
|
77
|
|
|
78
|
</Note> |
|
79
|
|
|
80
|
For example: |
|
81
|
|
|
82
|
```ts title="medusa-config.ts" |
|
83
|
module.exports = defineConfig({ |
|
84
|
// ... |
|
85
|
modules: [ |
|
86
|
{ |
|
87
|
resolve: "@medusajs/medusa/settings", |
|
88
|
options: { |
|
89
|
entityOverrides: { |
|
90
|
Brand: { |
|
91
|
defaultVisibleFields: [ |
|
92
|
"name", |
|
93
|
"description", |
|
94
|
], |
|
95
|
defaultFieldOrdering: { name: 100 }, |
|
96
|
}, |
|
97
|
}, |
|
98
|
}, |
|
99
|
}, |
|
100
|
], |
|
101
|
}) |
|
102
|
``` |
|
103
|
|
|
104
|
This configuration overrides the default columns of the `Brand` data model, assuming you defined it in a custom module. The `name` and `description` columns are shown by default, and the `name` column appears first. |
|
105
|
|
|
106
|
<Note title="Tip"> |
|
107
|
|
|
108
|
The Settings Module is registered by default, so you only add it to the `modules` array to pass when you want to pass options to it. |
|
109
|
|
|
110
|
</Note> |
|
111
|
|
|
112
|
### Step 2: Show a Configurable Data Table in Your Admin Customizations |
|
113
|
|
|
114
|
<Prerequisites |
|
115
|
items={[ |
|
116
|
{ |
|
117
|
text: "An API route that lists the entity's records", |
|
118
|
link: "!docs!/learn/customization/customize-admin/route#1-get-brands-api-route" |
|
119
|
} |
|
120
|
]} |
|
121
|
/> |
|
122
|
|
|
123
|
Next, you can show a configurable, view-aware data table for an entity in your admin customizations, such as a [custom UI route](!docs!/learn/fundamentals/admin/ui-routes). |
|
124
|
|
|
125
|
To do that, use the `ConfigurableDataTable` component with an adapter created by the `createTableAdapter` utility, both available through the admin dashboard. |
|
126
|
|
|
127
|
For example, create the file `src/admin/routes/brands/page.tsx` with the following content: |
|
128
|
|
|
129
|
```tsx title="src/admin/routes/brands/page.tsx" |
|
130
|
import { defineRouteConfig } from "@medusajs/admin-sdk" |
|
131
|
import { ConfigurableDataTable } from "@medusajs/dashboard/components" |
|
132
|
import { createTableAdapter } from "@medusajs/dashboard/lib" |
|
133
|
import { useQuery } from "@tanstack/react-query" |
|
134
|
import { sdk } from "../../lib/sdk" |
|
135
|
|
|
136
|
const adapter = createTableAdapter({ |
|
137
|
entity: "brand", |
|
138
|
useData: (fields, params) => { |
|
139
|
const { data, isLoading, isError, error } = |
|
140
|
useQuery({ |
|
141
|
queryKey: ["brands", fields, params], |
|
142
|
queryFn: () => |
|
143
|
sdk.client.fetch("/admin/brands", { |
|
144
|
query: { fields, ...params }, |
|
145
|
}), |
|
146
|
}) |
|
147
|
|
|
148
|
return { |
|
149
|
data: data?.brands, |
|
150
|
count: data?.count, |
|
151
|
isLoading, |
|
152
|
isError, |
|
153
|
error, |
|
154
|
} |
|
155
|
}, |
|
156
|
getRowHref: (row) => `/brands/${row.id}`, |
|
157
|
}) |
|
158
|
|
|
159
|
const BrandsPage = () => { |
|
160
|
return ( |
|
161
|
<ConfigurableDataTable |
|
162
|
adapter={adapter} |
|
163
|
heading="Brands" |
|
164
|
/> |
|
165
|
) |
|
166
|
} |
|
167
|
|
|
168
|
export const config = defineRouteConfig({ |
|
169
|
label: "Brands", |
|
170
|
}) |
|
171
|
|
|
172
|
export default BrandsPage |
|
173
|
``` |
|
174
|
|
|
175
|
This API route retrieves the list of brands from the `/admin/brands` API route and shows them in a configurable data table. The table uses the default columns defined in the Settings Module's configuration, and admin users can customize the columns and save their view configurations. |
|
176
|
|
|
177
|
If you go to `/admin/brands` in your Medusa Admin dashboard, you'll see the configurable data table for the `Brand` data model. Based on the configurations you defined in `medusa-config.ts`, it shows the `name` and `description` columns, and the `name` column appears first. You can customize the configurations, such as the visible columns and their order, and save your view configuration. |
|
178
|
|
|
179
|
<Note title="Tip"> |
|
180
|
|
|
181
|
If you don't need saved views or dynamic columns, use the [DataTable component](!ui!/components/data-table) from `@medusajs/ui` instead. It's a stable, fully-supported table primitive. |
|
182
|
|
|
183
|
</Note> |
|
184
|
|
|
185
|
--- |
|
186
|
|
|
187
|
## Set Default Visible Fields and Ordering |
|
188
|
|
|
189
|
To set which columns are shown by default and their order, use the following properties in the entity's override configuration: |
|
190
|
|
|
191
|
- `defaultVisibleFields`: An array of field names that are shown by default, in the order they're listed. |
|
192
|
- `defaultFieldOrdering`: An object whose keys are field names, and whose values are numbers. Columns are ordered by these numbers in ascending order, so a column with a lower number appears first. |
|
193
|
|
|
194
|
For example: |
|
195
|
|
|
196
|
```ts title="medusa-config.ts" |
|
197
|
module.exports = defineConfig({ |
|
198
|
// ... |
|
199
|
modules: [ |
|
200
|
{ |
|
201
|
resolve: "@medusajs/medusa/settings", |
|
202
|
options: { |
|
203
|
entityOverrides: { |
|
204
|
Brand: { |
|
205
|
defaultVisibleFields: ["name", "description"], |
|
206
|
defaultFieldOrdering: { |
|
207
|
name: 100, |
|
208
|
description: 200, |
|
209
|
}, |
|
210
|
}, |
|
211
|
Product: { |
|
212
|
defaultVisibleFields: ["title", "collection.title"], |
|
213
|
defaultFieldOrdering: { |
|
214
|
title: 100, |
|
215
|
"collection.title": 200, |
|
216
|
}, |
|
217
|
}, |
|
218
|
}, |
|
219
|
}, |
|
220
|
}, |
|
221
|
], |
|
222
|
}) |
|
223
|
``` |
|
224
|
|
|
225
|
You override the default columns of the `Brand` and `Product` data models. The `Brand` override shows the `name` and `description` columns, and the `Product` override shows the `title` column and the title of its collection. |
|
226
|
|
|
227
|
The `Product` override adds a relation's field to the visible columns: `collection.title` shows the title of a product's collection as a column. Use a dotted path to reference a scalar field of a related entity, and use the same dotted path as the key in `defaultFieldOrdering`. |
|
228
|
|
|
229
|
For a core entity like `Product`, your configuration is merged with the built-in configuration. For a custom data model like `Brand`, your configuration is the entire configuration. |
|
230
|
|
|
231
|
<Note title="Tip"> |
|
232
|
|
|
233
|
Space the ordering numbers by `100` to leave room to insert columns later. |
|
234
|
|
|
235
|
</Note> |
|
236
|
|
|
237
|
--- |
|
238
|
|
|
239
|
## Default Column Render Modes |
|
240
|
|
|
241
|
The Settings Module infers how to render each column's value, which is used to decide the column's render mode. The Medusa Admin dashboard defines cell renderers for these render modes, which are used to render the column's value in the configurable data table. |
|
242
|
|
|
243
|
The Settings Module and Medusa Admin dashboard support the following built-in render modes: |
|
244
|
|
|
245
|
<Table> |
|
246
|
<Table.Header> |
|
247
|
<Table.Row> |
|
248
|
<Table.HeaderCell> |
|
249
|
|
|
250
|
Render Mode |
|
251
|
|
|
252
|
</Table.HeaderCell> |
|
253
|
<Table.HeaderCell> |
|
254
|
|
|
255
|
Description |
|
256
|
|
|
257
|
</Table.HeaderCell> |
|
258
|
</Table.Row> |
|
259
|
</Table.Header> |
|
260
|
<Table.Body> |
|
261
|
<Table.Row> |
|
262
|
<Table.Cell> |
|
263
|
|
|
264
|
`text` |
|
265
|
|
|
266
|
</Table.Cell> |
|
267
|
<Table.Cell> |
|
268
|
|
|
269
|
Shows the value as plain text. |
|
270
|
|
|
271
|
</Table.Cell> |
|
272
|
</Table.Row> |
|
273
|
<Table.Row> |
|
274
|
<Table.Cell> |
|
275
|
|
|
276
|
`number` |
|
277
|
|
|
278
|
</Table.Cell> |
|
279
|
<Table.Cell> |
|
280
|
|
|
281
|
Formats the value as a localized number. |
|
282
|
|
|
283
|
</Table.Cell> |
|
284
|
</Table.Row> |
|
285
|
<Table.Row> |
|
286
|
<Table.Cell> |
|
287
|
|
|
288
|
`currency` |
|
289
|
|
|
290
|
</Table.Cell> |
|
291
|
<Table.Cell> |
|
292
|
|
|
293
|
Formats the value as a price using the same row's `currency_code` property. |
|
294
|
|
|
295
|
</Table.Cell> |
|
296
|
</Table.Row> |
|
297
|
<Table.Row> |
|
298
|
<Table.Cell> |
|
299
|
|
|
300
|
`date`, `datetime`, or `timestamp` |
|
301
|
|
|
302
|
</Table.Cell> |
|
303
|
<Table.Cell> |
|
304
|
|
|
305
|
Formats the value as a date. |
|
306
|
|
|
307
|
</Table.Cell> |
|
308
|
</Table.Row> |
|
309
|
<Table.Row> |
|
310
|
<Table.Cell> |
|
311
|
|
|
312
|
`boolean` |
|
313
|
|
|
314
|
</Table.Cell> |
|
315
|
<Table.Cell> |
|
316
|
|
|
317
|
Shows the value as a Yes or No badge. |
|
318
|
|
|
319
|
</Table.Cell> |
|
320
|
</Table.Row> |
|
321
|
<Table.Row> |
|
322
|
<Table.Cell> |
|
323
|
|
|
324
|
`email` |
|
325
|
|
|
326
|
</Table.Cell> |
|
327
|
<Table.Cell> |
|
328
|
|
|
329
|
Shows the value as a `mailto:` link. |
|
330
|
|
|
331
|
</Table.Cell> |
|
332
|
</Table.Row> |
|
333
|
<Table.Row> |
|
334
|
<Table.Cell> |
|
335
|
|
|
336
|
`phone` |
|
337
|
|
|
338
|
</Table.Cell> |
|
339
|
<Table.Cell> |
|
340
|
|
|
341
|
Shows the value as a `tel:` link. |
|
342
|
|
|
343
|
</Table.Cell> |
|
344
|
</Table.Row> |
|
345
|
<Table.Row> |
|
346
|
<Table.Cell> |
|
347
|
|
|
348
|
`url` |
|
349
|
|
|
350
|
</Table.Cell> |
|
351
|
<Table.Cell> |
|
352
|
|
|
353
|
Shows the value as an external link. |
|
354
|
|
|
355
|
</Table.Cell> |
|
356
|
</Table.Row> |
|
357
|
<Table.Row> |
|
358
|
<Table.Cell> |
|
359
|
|
|
360
|
`image` |
|
361
|
|
|
362
|
</Table.Cell> |
|
363
|
<Table.Cell> |
|
364
|
|
|
365
|
Shows the value as an image. |
|
366
|
|
|
367
|
</Table.Cell> |
|
368
|
</Table.Row> |
|
369
|
<Table.Row> |
|
370
|
<Table.Cell> |
|
371
|
|
|
372
|
`json` |
|
373
|
|
|
374
|
</Table.Cell> |
|
375
|
<Table.Cell> |
|
376
|
|
|
377
|
Shows the value as formatted JSON. |
|
378
|
|
|
379
|
</Table.Cell> |
|
380
|
</Table.Row> |
|
381
|
<Table.Row> |
|
382
|
<Table.Cell> |
|
383
|
|
|
384
|
`handle` |
|
385
|
|
|
386
|
</Table.Cell> |
|
387
|
<Table.Cell> |
|
388
|
|
|
389
|
Shows the value as a URL path prefixed with `/`. |
|
390
|
|
|
391
|
</Table.Cell> |
|
392
|
</Table.Row> |
|
393
|
<Table.Row> |
|
394
|
<Table.Cell> |
|
395
|
|
|
396
|
`count` |
|
397
|
|
|
398
|
</Table.Cell> |
|
399
|
<Table.Cell> |
|
400
|
|
|
401
|
Shows the number of items in the array specified in the entity configuration's `metadata.list_field`. |
|
402
|
|
|
403
|
</Table.Cell> |
|
404
|
</Table.Row> |
|
405
|
<Table.Row> |
|
406
|
<Table.Cell> |
|
407
|
|
|
408
|
`badges` |
|
409
|
|
|
410
|
</Table.Cell> |
|
411
|
<Table.Cell> |
|
412
|
|
|
413
|
Shows the items in the array specified in the entity configuration's `metadata.list_field` as badges. The label of the badges is determined by `metadata.display_field`. |
|
414
|
|
|
415
|
</Table.Cell> |
|
416
|
</Table.Row> |
|
417
|
<Table.Row> |
|
418
|
<Table.Cell> |
|
419
|
|
|
420
|
`status` |
|
421
|
|
|
422
|
</Table.Cell> |
|
423
|
<Table.Cell> |
|
424
|
|
|
425
|
Shows a status pill resolved from the entity configuration's `metadata.resolver` or `metadata.status_variants`. |
|
426
|
|
|
427
|
</Table.Cell> |
|
428
|
</Table.Row> |
|
429
|
<Table.Row> |
|
430
|
<Table.Cell> |
|
431
|
|
|
432
|
`name` |
|
433
|
|
|
434
|
</Table.Cell> |
|
435
|
<Table.Cell> |
|
436
|
|
|
437
|
Shows a full name from the `first_name` and `last_name` fields at the entity configuration's `metadata.name_source`. |
|
438
|
|
|
439
|
</Table.Cell> |
|
440
|
</Table.Row> |
|
441
|
<Table.Row> |
|
442
|
<Table.Cell> |
|
443
|
|
|
444
|
`address` |
|
445
|
|
|
446
|
</Table.Cell> |
|
447
|
<Table.Cell> |
|
448
|
|
|
449
|
Shows a formatted address from the object at the entity configuration's `metadata.address_field`. |
|
450
|
|
|
451
|
</Table.Cell> |
|
452
|
</Table.Row> |
|
453
|
<Table.Row> |
|
454
|
<Table.Cell> |
|
455
|
|
|
456
|
`country_code` |
|
457
|
|
|
458
|
</Table.Cell> |
|
459
|
<Table.Cell> |
|
460
|
|
|
461
|
Shows a country flag from the code at the entity configuration's `metadata.country_code_field`. |
|
462
|
|
|
463
|
</Table.Cell> |
|
464
|
</Table.Row> |
|
465
|
</Table.Body> |
|
466
|
</Table> |
|
467
|
|
|
468
|
### How Render Modes are Inferred |
|
469
|
|
|
470
|
By default, the Settings Module infers how to render a column's value in the following order of precedence: |
|
471
|
|
|
472
|
1. **Name checking:** It applies the following name checks and assigns the corresponding render mode if the field name matches: |
|
473
|
- `*_at` / `created_at` / `updated_at` / `deleted_at` → `datetime` |
|
474
|
- `*_date` → `date` |
|
475
|
- `*total` / `*amount` / `*price` / `subtotal` / `tax_total` / `shipping_total` / `discount_total` → `currency` |
|
476
|
- `*status` / `state` / `payment_status` / `fulfillment_status` → `status` |
|
477
|
- `email` / `*_email` → `email` |
|
478
|
- `phone` / `*_phone` → `phone` |
|
479
|
- `*country_code` → `country_code` |
|
480
|
- `url` / `*_url` → `url` |
|
481
|
- `thumbnail` / `avatar` / `*_image` → `image` |
|
482
|
- `id` / `*_id` → `id` |
|
483
|
- `display_id` → `display_id` |
|
484
|
- `count` / `*_count` / `*quantity` → `number` |
|
485
|
- `is_*` / `has_*` / `can_*` → `boolean` |
|
486
|
- `metadata` → `json` |
|
487
|
2. **Type checking:** If the field name doesn't match any of the above, it checks the field's type and assigns the corresponding render mode: |
|
488
|
- `boolean` → `boolean` |
|
489
|
- `text` → `text` |
|
490
|
- `number` → `number` |
|
491
|
- `dateTime` → `datetime` |
|
492
|
- `json` → `json` |
|
493
|
- `id` → `id` |
|
494
|
3. **Fallback:** If the field name and type don't match any of the above, it defaults to `text`. |
|
495
|
|
|
496
|
### Override Field Render Modes |
|
497
|
|
|
498
|
To override the inferred render mode of a field, use the `fieldRenderModes` property. Its value is an object whose keys are field names, and whose values are render modes: |
|
499
|
|
|
500
|
```ts title="medusa-config.ts" |
|
501
|
module.exports = defineConfig({ |
|
502
|
// ... |
|
503
|
modules: [ |
|
504
|
{ |
|
505
|
resolve: "@medusajs/medusa/settings", |
|
506
|
options: { |
|
507
|
entityOverrides: { |
|
508
|
Brand: { |
|
509
|
defaultVisibleFields: ["name", "description", "asset"], |
|
510
|
fieldRenderModes: { |
|
511
|
asset: "image", |
|
512
|
}, |
|
513
|
}, |
|
514
|
}, |
|
515
|
}, |
|
516
|
}, |
|
517
|
], |
|
518
|
}) |
|
519
|
``` |
|
520
|
|
|
521
|
In this example, the `asset` field of the `Brand` data model is rendered as an image, even if its name and type don't match any of the built-in render modes. |
|
522
|
|
|
523
|
For render modes that require additional configuration through the `metadata` property, such as `country_code`, you can set the required configuration in the entity's override configuration: |
|
524
|
|
|
525
|
```ts title="medusa-config.ts" |
|
526
|
module.exports = defineConfig({ |
|
527
|
// ... |
|
528
|
modules: [ |
|
529
|
{ |
|
530
|
resolve: "@medusajs/medusa/settings", |
|
531
|
options: { |
|
532
|
entityOverrides: { |
|
533
|
Brand: { |
|
534
|
defaultVisibleFields: ["name", "description", "country"], |
|
535
|
fieldRenderModes: { |
|
536
|
country: "country_code", |
|
537
|
}, |
|
538
|
fieldMetadata: { |
|
539
|
country: { country_code_field: "country" }, |
|
540
|
}, |
|
541
|
}, |
|
542
|
}, |
|
543
|
}, |
|
544
|
}, |
|
545
|
], |
|
546
|
}) |
|
547
|
``` |
|
548
|
|
|
549
|
In this example, the `country` field of the `Brand` data model is rendered as a country flag, and the `country_code_field` metadata is set to the same field name. |
|
550
|
|
|
551
|
--- |
|
552
|
|
|
553
|
## Define a Custom Render Mode |
|
554
|
|
|
555
|
In your admin dashboard customization, you can define a custom render mode to render a column's value in a specific way. You can then use that render mode in the entity's override configuration to render a column's value with your custom logic. |
|
556
|
|
|
557
|
Cell renderers are defined in the `src/admin/lib/cell-renderers.tsx` file of your Medusa project. You can define multiple cell renderers in this file, each defined with the `defineCellRenderer` utility. |
|
558
|
|
|
559
|
For example, create the file `src/admin/lib/cell-renderers.tsx` with the following content: |
|
560
|
|
|
561
|
```tsx title="src/admin/lib/cell-renderers.tsx" |
|
562
|
import { defineCellRenderer } from "@medusajs/dashboard/lib" |
|
563
|
|
|
564
|
defineCellRenderer("capitalized", { |
|
565
|
render: (value) => { |
|
566
|
if (!value) { |
|
567
|
return "-" |
|
568
|
} |
|
569
|
|
|
570
|
const text = String(value) |
|
571
|
return text.charAt(0).toUpperCase() + text.slice(1) |
|
572
|
}, |
|
573
|
}) |
|
574
|
``` |
|
575
|
|
|
576
|
In this example, the `capitalized` render mode capitalizes the first letter of the column's value. |
|
577
|
|
|
578
|
The `defineCellRenderer` utility accepts the following parameters: |
|
579
|
|
|
580
|
1. A string that is the render mode's name, which you can use in the entity's override configuration. |
|
581
|
2. A configuration object with the following properties: |
|
582
|
- `render`: A function that returns the content of the cell. It receives the following parameters: |
|
583
|
1. The column's value. For regular columns, this is the value of the field. For computed columns, this is the full row. |
|
584
|
2. The full row of data, which includes all the fields of the entity. |
|
585
|
3. The column's configuration as an `AdminColumn` object, including properties like `id`, `name`, `field` (the field path), `data_type`, `render_mode`, and `metadata`. For computed columns, it also has a `computed` object with `type`, `required_fields`, and `optional_fields`. |
|
586
|
4. A translation function to translate strings in the cell's content. |
|
587
|
- `align`: (optional) The alignment of the cell's content. Its value can be `left`, `center`, or `right`. The default value is `left`. |
|
588
|
- `truncateTooltip`: (optional) Whether to show a tooltip with the full content when the cell's content is truncated. The default value is `true`. |
|
589
|
|
|
590
|
You can then apply the `capitalized` render mode to a regular field in the `Brand` entity's override configuration using the `fieldRenderModes` property: |
|
591
|
|
|
592
|
```ts title="medusa-config.ts" |
|
593
|
module.exports = defineConfig({ |
|
594
|
// ... |
|
595
|
modules: [ |
|
596
|
{ |
|
597
|
resolve: "@medusajs/medusa/settings", |
|
598
|
options: { |
|
599
|
entityOverrides: { |
|
600
|
Brand: { |
|
601
|
fieldRenderModes: { |
|
602
|
name: "capitalized", |
|
603
|
}, |
|
604
|
}, |
|
605
|
}, |
|
606
|
}, |
|
607
|
}, |
|
608
|
], |
|
609
|
}) |
|
610
|
``` |
|
611
|
|
|
612
|
This applies the `capitalized` render mode to the `name` column, so a brand's name renders with its first letter capitalized. Because the renderer only uses the column's own value, no other fields need to be fetched. |
|
613
|
|
|
614
|
### Use Metadata in a Custom Render Mode |
|
615
|
|
|
616
|
A custom render mode can accept configuration through the column's `metadata`, which makes the render mode reusable across fields. Set the metadata for a regular field with the `fieldMetadata` property, and read it in the renderer from the third parameter's `metadata` property. |
|
617
|
|
|
618
|
For example, add a `truncated` render mode that shortens a value to a maximum length: |
|
619
|
|
|
620
|
```tsx title="src/admin/lib/cell-renderers.tsx" |
|
621
|
import { defineCellRenderer } from "@medusajs/dashboard/lib" |
|
622
|
|
|
623
|
defineCellRenderer("truncated", { |
|
624
|
render: (value, _row, column) => { |
|
625
|
if (!value) { |
|
626
|
return "-" |
|
627
|
} |
|
628
|
|
|
629
|
const maxLength = column.metadata?.max_length ?? 50 |
|
630
|
const text = String(value) |
|
631
|
|
|
632
|
return text.length > maxLength |
|
633
|
? `${text.slice(0, maxLength)}...` |
|
634
|
: text |
|
635
|
}, |
|
636
|
}) |
|
637
|
``` |
|
638
|
|
|
639
|
Then apply the `truncated` render mode to a regular field and pass its configuration with the `fieldMetadata` property: |
|
640
|
|
|
641
|
```ts title="medusa-config.ts" |
|
642
|
module.exports = defineConfig({ |
|
643
|
// ... |
|
644
|
modules: [ |
|
645
|
{ |
|
646
|
resolve: "@medusajs/medusa/settings", |
|
647
|
options: { |
|
648
|
entityOverrides: { |
|
649
|
Brand: { |
|
650
|
fieldRenderModes: { |
|
651
|
description: "truncated", |
|
652
|
}, |
|
653
|
fieldMetadata: { |
|
654
|
description: { max_length: 50 }, |
|
655
|
}, |
|
656
|
}, |
|
657
|
}, |
|
658
|
}, |
|
659
|
}, |
|
660
|
], |
|
661
|
}) |
|
662
|
``` |
|
663
|
|
|
664
|
This applies the `truncated` render mode to the `description` column and truncates its value to `50` characters. The renderer reads `max_length` from the column's `metadata`, which is populated from the `fieldMetadata` property. You can reuse the same render mode on other fields with a different `max_length`. |
|
665
|
|
|
666
|
--- |
|
667
|
|
|
668
|
## Add Computed Columns |
|
669
|
|
|
670
|
A computed column is a column whose value is computed from other fields or with custom logic, rather than a direct property of the entity. For example, you can add a `products_count` column for the `Brand` data model that shows the number of products associated with a brand. |
|
671
|
|
|
672
|
To add computed columns to an entity's data table, use the `computedColumns` property on the entity's override configuration. Its value is an array of objects, each having the following properties: |
|
673
|
|
|
674
|
- `id`: The column's unique identifier. |
|
675
|
- `name`: The column's display name. |
|
676
|
- `context`: (optional) When to use the column. Its value can be `display` to show the column, `filter` to only allow filtering by it, or `both`. The default value is `display`. |
|
677
|
- `renderMode`: How the column's value renders. This is required for columns with `context: display` (the default). Its value can either be a [built-in render mode](#default-column-render-modes) or a [custom render mode](#define-a-custom-render-mode). |
|
678
|
- `requiredFields`: An array of fields needed to compute the column's value. |
|
679
|
- `metadata`: An object of additional configuration for the column, which are used by the render mode's cell renderer. For example, the `count` render mode uses the `metadata.list_field` property to count the items in a relation. |
|
680
|
|
|
681
|
For example: |
|
682
|
|
|
683
|
```ts title="medusa-config.ts" |
|
684
|
module.exports = defineConfig({ |
|
685
|
// ... |
|
686
|
modules: [ |
|
687
|
{ |
|
688
|
resolve: "@medusajs/medusa/settings", |
|
689
|
options: { |
|
690
|
entityOverrides: { |
|
691
|
Brand: { |
|
692
|
computedColumns: [ |
|
693
|
{ |
|
694
|
id: "products_count", |
|
695
|
name: "Product Count", |
|
696
|
renderMode: "count", |
|
697
|
requiredFields: ["products.id"], |
|
698
|
metadata: { list_field: "products" }, |
|
699
|
}, |
|
700
|
], |
|
701
|
}, |
|
702
|
}, |
|
703
|
}, |
|
704
|
}, |
|
705
|
], |
|
706
|
}) |
|
707
|
``` |
|
708
|
|
|
709
|
This adds a `products_count` column to the Brand's configurable data table that shows the number of products associated with a brand, assuming there's a link between them. |
|
710
|
|
|
711
|
The `count` render mode counts the items in the relation set by `metadata.list_field` (`products` here), whereas `requiredFields` only ensures that relation is fetched onto the row. |