@mini_7/material-react-table-mcp 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +68 -0
- package/data/MIGRATION.md +136 -0
- package/data/api.json +5379 -0
- package/data/examples/advanced.tsx +309 -0
- package/data/examples/aggregation-and-grouping.tsx +153 -0
- package/data/examples/aggregation-multi.tsx +105 -0
- package/data/examples/alternate-column-filtering.tsx +49 -0
- package/data/examples/alternate-detail-panel.tsx +81 -0
- package/data/examples/alternate-pagination.tsx +23 -0
- package/data/examples/basic.tsx +109 -0
- package/data/examples/chart-detail-panel.tsx +89 -0
- package/data/examples/column-actions-space.tsx +44 -0
- package/data/examples/column-alignment.tsx +70 -0
- package/data/examples/custom-column-actions.tsx +109 -0
- package/data/examples/custom-column-filtering-ui.tsx +89 -0
- package/data/examples/custom-headless.tsx +125 -0
- package/data/examples/custom-top-toolbar.tsx +88 -0
- package/data/examples/customize-display-columns.tsx +85 -0
- package/data/examples/customize-filter-components.tsx +101 -0
- package/data/examples/customize-filter-modes.tsx +73 -0
- package/data/examples/customize-filter-variants.tsx +116 -0
- package/data/examples/customize-global-filter-component.tsx +52 -0
- package/data/examples/customize-remove-column-grouping.tsx +94 -0
- package/data/examples/customize-row-selection.tsx +54 -0
- package/data/examples/customize-table-styles.tsx +100 -0
- package/data/examples/disable-column-actions.tsx +37 -0
- package/data/examples/disable-column-hiding.tsx +90 -0
- package/data/examples/disable-density-toggle.tsx +49 -0
- package/data/examples/dynamic-columns.tsx +189 -0
- package/data/examples/editing-crud-cell.tsx +377 -0
- package/data/examples/editing-crud-modal.tsx +370 -0
- package/data/examples/editing-crud-row.tsx +333 -0
- package/data/examples/editing-crud-table.tsx +369 -0
- package/data/examples/editing-crud-tree.tsx +416 -0
- package/data/examples/enable-cell-actions.tsx +76 -0
- package/data/examples/enable-click-to-copy.tsx +39 -0
- package/data/examples/enable-column-grouping.tsx +120 -0
- package/data/examples/enable-column-ordering.tsx +48 -0
- package/data/examples/enable-column-pinning.tsx +69 -0
- package/data/examples/enable-column-resizing.tsx +56 -0
- package/data/examples/enable-column-virtualization.tsx +27 -0
- package/data/examples/enable-detail-panel-conditionally.tsx +78 -0
- package/data/examples/enable-detail-panel-virtualized.tsx +80 -0
- package/data/examples/enable-expanding-tree.tsx +110 -0
- package/data/examples/enable-filter-facet-values.tsx +65 -0
- package/data/examples/enable-row-dragging.tsx +137 -0
- package/data/examples/enable-row-numbers-original.tsx +48 -0
- package/data/examples/enable-row-numbers-static.tsx +48 -0
- package/data/examples/enable-row-ordering.tsx +57 -0
- package/data/examples/enable-row-pinning-select.tsx +76 -0
- package/data/examples/enable-row-pinning-static.tsx +50 -0
- package/data/examples/enable-row-pinning-sticky.tsx +69 -0
- package/data/examples/enable-row-selection.tsx +96 -0
- package/data/examples/enable-row-virtualization.tsx +101 -0
- package/data/examples/enable-sticky-header.tsx +59 -0
- package/data/examples/expanding-tree-expanded.tsx +121 -0
- package/data/examples/expanding-tree-flat-parse.tsx +125 -0
- package/data/examples/expanding-tree-root-expanded.tsx +151 -0
- package/data/examples/export-to-csv.tsx +114 -0
- package/data/examples/export-to-pdf.tsx +104 -0
- package/data/examples/external-toolbar.tsx +108 -0
- package/data/examples/font-awesome-icons.tsx +106 -0
- package/data/examples/infinite-scrolling.tsx +208 -0
- package/data/examples/lazy-detail-panel.tsx +262 -0
- package/data/examples/lazy-sub-rows.tsx +190 -0
- package/data/examples/linear-progress.tsx +66 -0
- package/data/examples/loading.tsx +50 -0
- package/data/examples/localization-i18n-ar.tsx +65 -0
- package/data/examples/localization-i18n-az.tsx +60 -0
- package/data/examples/localization-i18n-bg.tsx +60 -0
- package/data/examples/localization-i18n-cs.tsx +60 -0
- package/data/examples/localization-i18n-da.tsx +60 -0
- package/data/examples/localization-i18n-de.tsx +60 -0
- package/data/examples/localization-i18n-el.tsx +60 -0
- package/data/examples/localization-i18n-en.tsx +60 -0
- package/data/examples/localization-i18n-es.tsx +60 -0
- package/data/examples/localization-i18n-et.tsx +60 -0
- package/data/examples/localization-i18n-fa.tsx +66 -0
- package/data/examples/localization-i18n-fi.tsx +60 -0
- package/data/examples/localization-i18n-fr.tsx +60 -0
- package/data/examples/localization-i18n-he.tsx +66 -0
- package/data/examples/localization-i18n-hr.tsx +60 -0
- package/data/examples/localization-i18n-hu.tsx +60 -0
- package/data/examples/localization-i18n-hy.tsx +65 -0
- package/data/examples/localization-i18n-id.tsx +60 -0
- package/data/examples/localization-i18n-it.tsx +60 -0
- package/data/examples/localization-i18n-ja.tsx +60 -0
- package/data/examples/localization-i18n-ko.tsx +60 -0
- package/data/examples/localization-i18n-nl.tsx +60 -0
- package/data/examples/localization-i18n-no.tsx +60 -0
- package/data/examples/localization-i18n-np.tsx +60 -0
- package/data/examples/localization-i18n-pl.tsx +60 -0
- package/data/examples/localization-i18n-pt-BR.tsx +60 -0
- package/data/examples/localization-i18n-pt.tsx +60 -0
- package/data/examples/localization-i18n-ro.tsx +60 -0
- package/data/examples/localization-i18n-ru.tsx +60 -0
- package/data/examples/localization-i18n-sk.tsx +60 -0
- package/data/examples/localization-i18n-sr-Cyrl-RS.tsx +60 -0
- package/data/examples/localization-i18n-sr-Latn-RS.tsx +60 -0
- package/data/examples/localization-i18n-sv.tsx +60 -0
- package/data/examples/localization-i18n-tr.tsx +60 -0
- package/data/examples/localization-i18n-uk.tsx +60 -0
- package/data/examples/localization-i18n-vi.tsx +60 -0
- package/data/examples/localization-i18n-zh-hans.tsx +60 -0
- package/data/examples/localization-i18n-zh-hant.tsx +60 -0
- package/data/examples/manual-selection.tsx +105 -0
- package/data/examples/minimal.tsx +78 -0
- package/data/examples/mui-theme.tsx +136 -0
- package/data/examples/multi-sorting.tsx +58 -0
- package/data/examples/persistent-state.tsx +184 -0
- package/data/examples/react-query.tsx +182 -0
- package/data/examples/remote.tsx +150 -0
- package/data/examples/row-actions-buttons.tsx +89 -0
- package/data/examples/row-actions-menu-items.tsx +64 -0
- package/data/examples/single-row-selection.tsx +98 -0
- package/data/examples/virtualized.tsx +138 -0
- package/data/guides/accessibility.md +119 -0
- package/data/guides/aggregation.md +210 -0
- package/data/guides/async-loading.md +132 -0
- package/data/guides/best-practices.md +363 -0
- package/data/guides/cell-actions.md +139 -0
- package/data/guides/click-to-copy.md +67 -0
- package/data/guides/column-actions.md +69 -0
- package/data/guides/column-filtering.md +366 -0
- package/data/guides/column-grouping.md +192 -0
- package/data/guides/column-hiding.md +133 -0
- package/data/guides/column-ordering-dnd.md +99 -0
- package/data/guides/column-pinning.md +72 -0
- package/data/guides/column-resizing.md +157 -0
- package/data/guides/column-size.md +162 -0
- package/data/guides/customize-components.md +395 -0
- package/data/guides/customize-icons.md +35 -0
- package/data/guides/data-columns.md +292 -0
- package/data/guides/density-toggle.md +46 -0
- package/data/guides/detail-panel.md +195 -0
- package/data/guides/display-columns.md +135 -0
- package/data/guides/editing.md +367 -0
- package/data/guides/expanding-sub-rows.md +194 -0
- package/data/guides/full-screen-toggle.md +63 -0
- package/data/guides/global-filtering.md +237 -0
- package/data/guides/localization.md +57 -0
- package/data/guides/memoization.md +157 -0
- package/data/guides/pagination.md +171 -0
- package/data/guides/row-actions.md +116 -0
- package/data/guides/row-numbers.md +24 -0
- package/data/guides/row-ordering-dnd.md +51 -0
- package/data/guides/row-pinning.md +96 -0
- package/data/guides/row-selection.md +237 -0
- package/data/guides/sorting.md +190 -0
- package/data/guides/state-management.md +165 -0
- package/data/guides/sticky-header.md +41 -0
- package/data/guides/table-event-listeners.md +103 -0
- package/data/guides/toolbar-customization.md +251 -0
- package/data/guides/virtualization.md +183 -0
- package/data/index.json +1930 -0
- package/data/reference/mrt-components.md +183 -0
- package/data/reference/mrt-hooks.md +125 -0
- package/data/skills/composable-components/SKILL.md +189 -0
- package/data/skills/customization/SKILL.md +190 -0
- package/data/skills/drag-and-drop-ordering/SKILL.md +227 -0
- package/data/skills/editing/SKILL.md +243 -0
- package/data/skills/filtering/SKILL.md +213 -0
- package/data/skills/getting-started/SKILL.md +213 -0
- package/data/skills/localization/SKILL.md +182 -0
- package/data/skills/migrate-v3-to-v4/SKILL.md +161 -0
- package/data/skills/state-and-server-data/SKILL.md +200 -0
- package/data/skills/virtualization/SKILL.md +203 -0
- package/dist/data.js +137 -0
- package/dist/index.js +232 -0
- package/package.json +50 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
## Data (Accessor) Columns Guide
|
|
2
|
+
|
|
3
|
+
Data columns are used to display data and are the default columns that are created when you create a column with an `accessorKey` or `accessorFn`.
|
|
4
|
+
|
|
5
|
+
The table can perform processing on the data of a data column, such as sorting, filtering, grouping, etc.
|
|
6
|
+
|
|
7
|
+
The other type of column that you can make is a display column, which you can learn more about in the [next section](/docs/guides/display-columns).
|
|
8
|
+
|
|
9
|
+
### Accessors (Connect a column to data)
|
|
10
|
+
|
|
11
|
+
Each column definition must have at least an `accessorKey` (or a combination of an `id` and `accessorFn`) and a `header` property. The `accessorKey`/`accessorFn` property is the key that will be used to join the data from the `data` keys. The `header` property is used to display the column header, but is also used in other places in the table.
|
|
12
|
+
|
|
13
|
+
> Note: Do NOT have your accessors resolve JSX or markup. That's what custom [`Cell`](#custom-cell-render) renders are for. Accessors should only return primitive data so that the table can sort, filter, search, and group properly.
|
|
14
|
+
|
|
15
|
+
#### Method 1 - Using an accessorKey (Recommended)
|
|
16
|
+
|
|
17
|
+
The simplest and most common way to define a column is to use the `accessorKey` column option. The `accessorKey` column option is the key that will be used to join the data from the `data` keys.
|
|
18
|
+
|
|
19
|
+
The `accessorKey` must match one of the keys in your data, or else no data will show up in the column. The `accessorKey` also supports dot notation, so you can access nested data.
|
|
20
|
+
|
|
21
|
+
By default, the `accessorKey` will double as the `id` for the column, but if you need the id of the column to be different than the accessorKey, you can use the `id` property in addition.
|
|
22
|
+
|
|
23
|
+
```tsx
|
|
24
|
+
const columns = useMemo<MRT_ColumnDef<Customer>[]>( //TS helps with the autocomplete while writing columns
|
|
25
|
+
() => [
|
|
26
|
+
{
|
|
27
|
+
accessorKey: 'username', //normal recommended usage of an accessorKey
|
|
28
|
+
header: 'Username',
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
accessorKey: 'name.firstName', //example of dot notation used to access nested data
|
|
32
|
+
header: 'First Name',
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
accessorKey: 'name.lastName', //example of dot notation used to access nested data
|
|
36
|
+
header: 'Last Name',
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
accessorKey: 'customerAge',
|
|
40
|
+
id: 'age' //id overridden, usually not necessary to do this, but can be helpful
|
|
41
|
+
header: 'Age',
|
|
42
|
+
},
|
|
43
|
+
],
|
|
44
|
+
[],
|
|
45
|
+
);
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
#### Method 2 - Using an accessorFn and id
|
|
49
|
+
|
|
50
|
+
You can alternatively use the `accessorFn` column option. Here are at least three ways you can use it.
|
|
51
|
+
|
|
52
|
+
In each case, the `id` property is now required since there is no `accessorKey` for MRT to derive it from.
|
|
53
|
+
|
|
54
|
+
```tsx
|
|
55
|
+
const columns = useMemo<MRT_ColumnDef<Customer>[]>(
|
|
56
|
+
() => [
|
|
57
|
+
{
|
|
58
|
+
//simple accessorFn that works the same way as an `accessorKey`
|
|
59
|
+
accessorFn: (row) => row.username,
|
|
60
|
+
id: 'username',
|
|
61
|
+
header: 'Username',
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
//accessorFn function that combines multiple data together
|
|
65
|
+
accessorFn: (row) => `${row.firstName} ${row.lastName}`,
|
|
66
|
+
id: 'name',
|
|
67
|
+
header: 'Name',
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
//accessorFn used to access nested data, though you could just use dot notation in an accessorKey
|
|
71
|
+
accessorFn: (row) => row.personalInfo.age,
|
|
72
|
+
id: 'age',
|
|
73
|
+
header: 'Age',
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
[],
|
|
77
|
+
);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
#### Method 3 - Using createMRTColumnHelper
|
|
81
|
+
|
|
82
|
+
> New in V3 (After many requests)
|
|
83
|
+
|
|
84
|
+
Alternatively you can use the `createMRTColumnHelper` utility function to define your columns definitions in a slightly more type-safe way. Instantiate a `columnHelper` by passing in your `TData` type as a generic argument. Then the first argument of the `columnHelper.accessor()` method can be either an `accessorKey` or an `accessorFn`. Then you can specify the rest of the column options as the second argument.
|
|
85
|
+
|
|
86
|
+
```tsx
|
|
87
|
+
const columnHelper = createMRTColumnHelper<Customer>(); //TS now knows the shape of your data
|
|
88
|
+
|
|
89
|
+
const columns = [
|
|
90
|
+
//accessorKey as first argument, rest of column options as second argument
|
|
91
|
+
columnHelper.accessor('name', {
|
|
92
|
+
//TS should provide autocomplete for valid accessorKeys
|
|
93
|
+
header: 'Last Name',
|
|
94
|
+
}),
|
|
95
|
+
//accessorFn as first argument, rest of column options as second argument
|
|
96
|
+
columnHelper.accessor((row) => Number(row.age), {
|
|
97
|
+
//TS should provide autocomplete for valid properties on your data
|
|
98
|
+
header: 'Age',
|
|
99
|
+
id: 'age', //id is required when using accessorFn
|
|
100
|
+
}),
|
|
101
|
+
];
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Custom Header Render
|
|
105
|
+
|
|
106
|
+
If you want to pass in custom JSX to render the header, you can pass in a `Header` option in addition to the `header` string property.
|
|
107
|
+
|
|
108
|
+
> The `header` (lowercase) property is still required and still must only be a string because it is used within multiple components in the table and has string manipulation methods performed on it.
|
|
109
|
+
|
|
110
|
+
```tsx
|
|
111
|
+
const columns = useMemo(
|
|
112
|
+
() => [
|
|
113
|
+
{
|
|
114
|
+
accessorKey: 'name',
|
|
115
|
+
header: 'Name',
|
|
116
|
+
Header: ({ column }) => (
|
|
117
|
+
<i style={{ color: 'red' }}>{column.columnDef.header}</i> //re-use the header we already defined
|
|
118
|
+
), //arrow function
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
accessorKey: 'age',
|
|
122
|
+
header: 'Age',
|
|
123
|
+
Header: <i style={{ color: 'red' }}>Age</i>, //plain jsx with no function
|
|
124
|
+
},
|
|
125
|
+
],
|
|
126
|
+
[],
|
|
127
|
+
);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Custom Cell Render
|
|
131
|
+
|
|
132
|
+
Similarly, the data cells in a column can have a custom JSX render with the `Cell` option. This is one of the most common features used in MRT.
|
|
133
|
+
|
|
134
|
+
Using the `Cell` column option should be the only way that you use to render custom JSX in table cells. Do not put JSX in an accessorFn, or else the table will not be able to sort, filter, search, or group properly.
|
|
135
|
+
|
|
136
|
+
```tsx
|
|
137
|
+
const columns = useMemo(
|
|
138
|
+
() => [
|
|
139
|
+
{
|
|
140
|
+
accessorFn: (row) => `${row.firstName} ${row.lastName}`,
|
|
141
|
+
header: 'Name',
|
|
142
|
+
//Add a link in a cell render
|
|
143
|
+
Cell: ({ renderedCellValue, row }) => (
|
|
144
|
+
<Link to={`/profile/${row.original.username}`}>
|
|
145
|
+
{renderedCellValue}
|
|
146
|
+
</Link>
|
|
147
|
+
),
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
accessorKey: 'salary',
|
|
151
|
+
header: 'Salary',
|
|
152
|
+
//Format a number in a cell render
|
|
153
|
+
Cell: ({ cell }) => (
|
|
154
|
+
<span>${cell.getValue<number>().toLocaleString()}</span>
|
|
155
|
+
),
|
|
156
|
+
},
|
|
157
|
+
{
|
|
158
|
+
accessorKey: 'profileImage',
|
|
159
|
+
header: 'Profile Image',
|
|
160
|
+
//Render images in a cell render
|
|
161
|
+
Cell: ({ cell }) => <img src={cell.getValue<string>()} />,
|
|
162
|
+
},
|
|
163
|
+
],
|
|
164
|
+
[],
|
|
165
|
+
);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### Custom Footer Render
|
|
169
|
+
|
|
170
|
+
If you want to pass in custom JSX to render the footer, you can pass in a `Footer` option. If no custom markup is needed, you can just use the `footer` string property.
|
|
171
|
+
|
|
172
|
+
The footer cells can be a good place to put totals or other summary information.
|
|
173
|
+
|
|
174
|
+
```tsx
|
|
175
|
+
const columns = useMemo(
|
|
176
|
+
() => [
|
|
177
|
+
{
|
|
178
|
+
accessorKey: 'name',
|
|
179
|
+
header: 'Name',
|
|
180
|
+
footer: 'Name', //simple string header
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
accessorKey: 'age',
|
|
184
|
+
header: 'Age',
|
|
185
|
+
//Custom footer markup for a aggregation calculation
|
|
186
|
+
Footer: () => (
|
|
187
|
+
<Stack>
|
|
188
|
+
Max Age:
|
|
189
|
+
<Box color="warning.main">{Math.round(maxAge)}</Box>
|
|
190
|
+
</Stack>
|
|
191
|
+
),
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
[],
|
|
195
|
+
);
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
> See the [Customize Components Guide](/docs/guides/customize-components) for more ways to style and customize header and cell components.
|
|
199
|
+
|
|
200
|
+
### Set Column Widths
|
|
201
|
+
|
|
202
|
+
This topic is covered in detail in the [Column Size Guide](/docs/guides/column-size), but here is a brief overview.
|
|
203
|
+
|
|
204
|
+
Setting a CSS (sx or style) width prop in the `muiTableHeadCellProps`, `muiTableBodyCellProps`, etc. might not work well, and is redundant. MRT/TanStack Table has an official way to set column widths with the `size`, `minSize`, `maxSize`, and `grow` column options.
|
|
205
|
+
|
|
206
|
+
```tsx
|
|
207
|
+
const columns = [
|
|
208
|
+
{
|
|
209
|
+
accessorKey: 'id',
|
|
210
|
+
header: 'ID',
|
|
211
|
+
size: 50, //small column
|
|
212
|
+
grow: false, //don't allow this column to grow (if layoutMode is grid)
|
|
213
|
+
},
|
|
214
|
+
{
|
|
215
|
+
accessorKey: 'username',
|
|
216
|
+
header: 'Username',
|
|
217
|
+
minSize: 100, //min size enforced during resizing
|
|
218
|
+
maxSize: 200, //max size enforced during resizing
|
|
219
|
+
size: 180, //medium column
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
accessorKey: 'email',
|
|
223
|
+
header: 'Email',
|
|
224
|
+
size: 300, //large column
|
|
225
|
+
},
|
|
226
|
+
];
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
There is a lot of different behaviors for column widths depending on what other features are enabled or how they are configured. See the [Column Size Guide](/docs/guides/column-size) for more details on the `layoutMode`s, and how and why they are enabled and how they affect column widths.
|
|
230
|
+
|
|
231
|
+
### Set Column Alignment
|
|
232
|
+
|
|
233
|
+
By default, all columns are left-aligned. You can change the alignment of a column by setting the `align` option to either `"center"`, `"right"`, or `"justify"` in the `muiTableHeadCellProps` and `muiTableBodyCellProps` props/column options.
|
|
234
|
+
|
|
235
|
+
```tsx
|
|
236
|
+
const columns = [
|
|
237
|
+
{
|
|
238
|
+
accessorKey: 'id',
|
|
239
|
+
header: 'ID',
|
|
240
|
+
//right align the header, body, and footer cells each individually
|
|
241
|
+
muiTableHeadCellProps: {
|
|
242
|
+
align: 'right',
|
|
243
|
+
},
|
|
244
|
+
muiTableBodyCellProps: {
|
|
245
|
+
align: 'right',
|
|
246
|
+
},
|
|
247
|
+
muiTableFooterCellProps: {
|
|
248
|
+
align: 'right',
|
|
249
|
+
},
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
accessorKey: 'username',
|
|
253
|
+
header: 'Username',
|
|
254
|
+
//center align the header, body, and footer cells each individually
|
|
255
|
+
muiTableHeadCellProps: {
|
|
256
|
+
align: 'center',
|
|
257
|
+
},
|
|
258
|
+
muiTableBodyCellProps: {
|
|
259
|
+
align: 'center',
|
|
260
|
+
},
|
|
261
|
+
muiTableFooterCellProps: {
|
|
262
|
+
align: 'center',
|
|
263
|
+
},
|
|
264
|
+
},
|
|
265
|
+
];
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
> Live example: `column-alignment` (call get_mrt_example with that id).
|
|
269
|
+
|
|
270
|
+
### Enable or Disable Features Per Column
|
|
271
|
+
|
|
272
|
+
In the same way that you can pass props to the main `<MaterialReactTable />` component to enable or disable features, you can also specify options on the column definitions to enable or disable features on a per-column basis.
|
|
273
|
+
|
|
274
|
+
```tsx
|
|
275
|
+
const columns = useMemo(
|
|
276
|
+
() => [
|
|
277
|
+
{
|
|
278
|
+
accessorKey: 'salary',
|
|
279
|
+
header: 'Salary',
|
|
280
|
+
enableClickToCopy: true, //enable click to copy on this column
|
|
281
|
+
},
|
|
282
|
+
{
|
|
283
|
+
accessorKey: 'profileImage',
|
|
284
|
+
header: 'Profile Image',
|
|
285
|
+
enableSorting: false, //disable sorting on this column
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
[],
|
|
289
|
+
);
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
> See all the column options you can use in the [Column Options API Reference](/docs/api/column-options).
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
## Density Toggle Feature Guide
|
|
2
|
+
|
|
3
|
+
Material React Table includes a density toggle button in the top toolbar by default that lets you toggle between three different density levels. This is a great feature to include to help with accessibility for different user preferences, but it can also easily be disabled if desired.
|
|
4
|
+
|
|
5
|
+
### Relevant Table Options
|
|
6
|
+
|
|
7
|
+
Relevant table options (get_mrt_api has full details):
|
|
8
|
+
|
|
9
|
+
- `enableDensityToggle` - type `boolean` - default `true`
|
|
10
|
+
- `onDensityChange` - type `OnChangeFn<MRT_DensityState>`
|
|
11
|
+
|
|
12
|
+
### Relevant State
|
|
13
|
+
|
|
14
|
+
Relevant state options (get_mrt_api has full details):
|
|
15
|
+
|
|
16
|
+
- `density` - type `'comfortable' | 'compact' | 'spacious'` - default `'comfortable'`
|
|
17
|
+
|
|
18
|
+
### Default Density
|
|
19
|
+
|
|
20
|
+
By default, Material React Table will render with a medium `comfortable` density.
|
|
21
|
+
|
|
22
|
+
A density toggle is shown by default to let a user change the density to cycle through `spacious`, `comfortable`, and `compact` densities.
|
|
23
|
+
|
|
24
|
+
> When a `compact` density is set, whitespace is set to `nowrap` by default to keep the rows as short in height as possible. This can be overridden in the `muiTableBodyCellProps` styles or sx prop.
|
|
25
|
+
|
|
26
|
+
#### Change Default Density
|
|
27
|
+
|
|
28
|
+
If you want to change the default density, you can set it in either the `initialState` table option or the `state` table option.
|
|
29
|
+
|
|
30
|
+
```tsx
|
|
31
|
+
const table = useMaterialReactTable({
|
|
32
|
+
data,
|
|
33
|
+
columns,
|
|
34
|
+
initialState: { density: 'compact' },
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return <MaterialReactTable table={table} />;
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Disable or Hide the Density Toggle
|
|
41
|
+
|
|
42
|
+
You can change the default density and disable the density toggle itself if you want.
|
|
43
|
+
|
|
44
|
+
In this example, the density toggle is disabled and a `compact` density is set by default in the `initialState` table option.
|
|
45
|
+
|
|
46
|
+
> Live example: `disable-density-toggle` (call get_mrt_example with that id).
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
## Detail Panel Feature Guide
|
|
2
|
+
|
|
3
|
+
Material React Table has multiple kinds of expanding features. This guide will show you how to use the detail panel feature to expand a single row to show more information for that row.
|
|
4
|
+
|
|
5
|
+
If you are looking for how to expand multiple rows from a tree data structure, see the [Expanding Sub-Rows](/docs/guides/expanding-sub-rows) guide.
|
|
6
|
+
|
|
7
|
+
### Relevant Table Options
|
|
8
|
+
|
|
9
|
+
Relevant table options (get_mrt_api has full details):
|
|
10
|
+
|
|
11
|
+
- `displayColumnDefOptions` - type `{ [key: string]: MRT_DisplayColumnDef<TData> }` - Customize and override the column definition options for the built-in display columns. (Select, Expand, Row Actions, etc.)
|
|
12
|
+
- `enableExpandAll` - type `boolean` - default `true`
|
|
13
|
+
- `muiDetailPanelProps` - type `TableCellProps | ({ row, table }) => TableCellProps`
|
|
14
|
+
- `muiExpandAllButtonProps` - type `IconButtonProps | ({ table }) => IconButtonProps`
|
|
15
|
+
- `muiExpandButtonProps` - type `IconButtonProps | ({ row, table }) => IconButtonProps`
|
|
16
|
+
- `positionExpandColumn` - type `'first' | 'last'` - default `'first'`
|
|
17
|
+
- `renderDetailPanel` - type `({ row, table }) => ReactNode`
|
|
18
|
+
|
|
19
|
+
### Relevant State
|
|
20
|
+
|
|
21
|
+
Relevant state options (get_mrt_api has full details):
|
|
22
|
+
|
|
23
|
+
- `expanded` - type `Record<string, boolean> | boolean` - default `{}`
|
|
24
|
+
|
|
25
|
+
### Render Detail Panel
|
|
26
|
+
|
|
27
|
+
To add a detail panel to a row, all you need to do is add a `renderDetailPanel` table option.
|
|
28
|
+
|
|
29
|
+
The recommended way to access the row data for the detail panel is to pull from the `original` object on a row. This gives you the original data for the row, not transformed or filtered by TanStack Table.
|
|
30
|
+
|
|
31
|
+
> Using `row.getValue('columnId')` will not work for data that does not have its own column. Using `row.original.columnId` is recommended for detail panels since the data in the detail panel usually does not have its own column.
|
|
32
|
+
|
|
33
|
+
### Disable Expand All Button
|
|
34
|
+
|
|
35
|
+
If you don't want to show the expand all button, you can set the `enableExpandAll` table option to `false`.
|
|
36
|
+
|
|
37
|
+
```jsx
|
|
38
|
+
const table = useMaterialReactTable({
|
|
39
|
+
data,
|
|
40
|
+
columns,
|
|
41
|
+
enableExpandAll: false,
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Enable Detail Panel Conditionally Per Row
|
|
46
|
+
|
|
47
|
+
If the return value of your `renderDetailPanel` function returns `null` or a falsy value for a row, the expand button will be disabled for that row.
|
|
48
|
+
|
|
49
|
+
```jsx
|
|
50
|
+
const table = useMaterialReactTable({
|
|
51
|
+
columns,
|
|
52
|
+
data,
|
|
53
|
+
renderDetailPanel: ({ row }) =>
|
|
54
|
+
row.original.someCondition ? <DetailPanelContent /> : null,
|
|
55
|
+
});
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
One thing to note about the implementation of conditional detail panels is that additional `<tr>` elements will still be created for all rows, even if they do not have detail panel content. It is implemented this way in order to avoid bugs with row virtualization, or striped row CSS.
|
|
59
|
+
|
|
60
|
+
### Only Allow One Detail Panel Open At A Time
|
|
61
|
+
|
|
62
|
+
If you want to only allow one detail panel to be open at a time, all you have to do is add your own `onClick` logic to the `muiExpandButtonProps` table option.
|
|
63
|
+
|
|
64
|
+
```jsx
|
|
65
|
+
const table = useMaterialReactTable({
|
|
66
|
+
data,
|
|
67
|
+
columns,
|
|
68
|
+
renderDetailPanel: ({ row }) => <DetailPanelContent />,
|
|
69
|
+
muiExpandButtonProps: ({ row, table }) => ({
|
|
70
|
+
onClick: () => table.setExpanded({ [row.id]: !row.getIsExpanded() }), //set only this row to be expanded
|
|
71
|
+
}),
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Rotate Expand Icon
|
|
76
|
+
|
|
77
|
+
If you don't like the default rotation styles for the expand icons, you can pass in custom CSS to the `muiExpandButtonProps` and `muiExpandAllButtonProps` table options.
|
|
78
|
+
|
|
79
|
+
### Replace Expand Icon
|
|
80
|
+
|
|
81
|
+
You can easily use a custom expand icon either by following the [Custom Icons Guide](/docs/guides/custom-icons) or by passing in custom `children` to the `muiExpandButtonProps` and `muiExpandAllButtonProps` table options.
|
|
82
|
+
|
|
83
|
+
```jsx
|
|
84
|
+
const table = useMaterialReactTable({
|
|
85
|
+
data,
|
|
86
|
+
columns,
|
|
87
|
+
// icons, //or manage icons globally
|
|
88
|
+
muiExpandButtonProps: ({ row }) => ({
|
|
89
|
+
children: row.getIsExpanded() ? <MinusIcon /> : <AddIcon />,
|
|
90
|
+
}),
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### Customize or Style Detail Panel
|
|
95
|
+
|
|
96
|
+
You can use the `muiDetailPanelProps` table option to pass in custom props to the detail panel. These props are passed to the `<td>` element that contains the detail panel content.
|
|
97
|
+
|
|
98
|
+
If you need to customize the `<tr>` element containing the detail panel cell, you can just use the `muiTableBodyRowProps` table option that you use for customizing all rows. There is a `isDetailPanel` parameter that is available to you to target only detail panel rows.
|
|
99
|
+
|
|
100
|
+
```jsx
|
|
101
|
+
const table = useMaterialReactTable({
|
|
102
|
+
data,
|
|
103
|
+
columns,
|
|
104
|
+
muiDetailPanelProps: ({ row }) => ({
|
|
105
|
+
sx: {
|
|
106
|
+
//...
|
|
107
|
+
},
|
|
108
|
+
}),
|
|
109
|
+
muiTableBodyRowProps: ({ isDetailPanel, row }) => ({
|
|
110
|
+
sx: {
|
|
111
|
+
// isDetailPanel ? ... : ...
|
|
112
|
+
},
|
|
113
|
+
}),
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
> Live example: `enable-detail-panel-conditionally` (call get_mrt_example with that id).
|
|
118
|
+
|
|
119
|
+
### Expand Detail Panel By Default
|
|
120
|
+
|
|
121
|
+
If you want some or all rows to be expanded by default, you can specify that in the `initialState.expanded` table option. Pass `true` to expand all rows, or specify which rowIds should be expanded.
|
|
122
|
+
|
|
123
|
+
```jsx
|
|
124
|
+
const table = useMaterialReactTable({
|
|
125
|
+
data,
|
|
126
|
+
columns,
|
|
127
|
+
initialState: {
|
|
128
|
+
expanded: true,
|
|
129
|
+
// or expand specific rows by default
|
|
130
|
+
// expanded: {
|
|
131
|
+
// 1: true,
|
|
132
|
+
// 2: true,
|
|
133
|
+
// },
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
### Position Expand Column Last
|
|
139
|
+
|
|
140
|
+
If you want to position the expand column last, you can set the `positionExpandColumn` table option to `'last'`.
|
|
141
|
+
|
|
142
|
+
Alternatively though, you could use the [Column Pinning Feature](/docs/guides/column-pinning) to pin the expand column to the right side of the table.
|
|
143
|
+
|
|
144
|
+
> Live example: `alternate-detail-panel` (call get_mrt_example with that id).
|
|
145
|
+
|
|
146
|
+
### Detail Panel With Charts
|
|
147
|
+
|
|
148
|
+
The detail panel can be used to show a variety of content. Here's an example of a detail panel rendering charts with the [MUI X Charts](https://mui.com/x/react-charts/getting-started/) library.
|
|
149
|
+
|
|
150
|
+
> Live example: `chart-detail-panel` (call get_mrt_example with that id).
|
|
151
|
+
|
|
152
|
+
### Detail Panels with Virtualization
|
|
153
|
+
|
|
154
|
+
> New in v2.6.0
|
|
155
|
+
|
|
156
|
+
If you are using row virtualization, detail panels will now work more properly as of version 2.6.0. However, there are some caveats to be aware of. In order for row virtualization to work well, many of the animation/transitions have been disabled. This means that the detail panel will not animate open and closed. It will simply appear and disappear.
|
|
157
|
+
|
|
158
|
+
You also may need to specify some more accurate row height estimations for the row virtualizer in order to achieve the best scrollbar behavior. See the [Row Virtualization Guide](/docs/guides/row-virtualization) for the full details on this topic, but here's an example of how you might do that.
|
|
159
|
+
|
|
160
|
+
```jsx
|
|
161
|
+
const table = useMaterialReactTable({
|
|
162
|
+
data,
|
|
163
|
+
columns,
|
|
164
|
+
enableRowVirtualization: true,
|
|
165
|
+
renderDetailPanel: ({ row }) => <DetailPanelContent />,
|
|
166
|
+
rowVirtualizerOptions: ({ table }) => {
|
|
167
|
+
const { density, expanded } = table.getState();
|
|
168
|
+
return {
|
|
169
|
+
//adjust to your needs
|
|
170
|
+
estimateSize: (index) =>
|
|
171
|
+
index % 2 === 1 //even rows are normal rows, odd rows are detail panels
|
|
172
|
+
? //Estimate open detail panels as 80px tall, closed detail panels as 0px tall
|
|
173
|
+
expanded === true
|
|
174
|
+
? 80
|
|
175
|
+
: 0
|
|
176
|
+
: //estimate normal row heights
|
|
177
|
+
density === 'compact'
|
|
178
|
+
? 37
|
|
179
|
+
: density === 'comfortable'
|
|
180
|
+
? 58
|
|
181
|
+
: 73,
|
|
182
|
+
};
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
> Live example: `enable-detail-panel-virtualized` (call get_mrt_example with that id).
|
|
188
|
+
|
|
189
|
+
### Lazy Detail Panels
|
|
190
|
+
|
|
191
|
+
Fetching the additional data for the detail panels only after the user clicks to expand the row can be a good way to improve performance, and it is pretty easy to implement. It's even easier if you are using React Query.
|
|
192
|
+
|
|
193
|
+
> Live example: `lazy-detail-panel` (call get_mrt_example with that id).
|
|
194
|
+
|
|
195
|
+
View Extra Storybook **[Examples](https://www.material-react-table.dev/?path=/story/features-detail-panel-examples-)**
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
## Display (Built-in) Columns Guide
|
|
2
|
+
|
|
3
|
+
Display columns are used to display non-data elements in a table. They only require an `id` and `header` in the column definition. They do not need an `accessorKey` or `accessorFn`, as they are not meant to connect to your data at all.
|
|
4
|
+
|
|
5
|
+
Display columns do not have any processing features, such as sorting, filtering, grouping, etc. enabled on them by default.
|
|
6
|
+
|
|
7
|
+
### Relevant Table Options
|
|
8
|
+
|
|
9
|
+
Relevant table options (get_mrt_api has full details):
|
|
10
|
+
|
|
11
|
+
- `defaultDisplayColumn` - type `Partial<MRT_DisplayColumnDef<TData>>` - Default column options to use for all display column defs supplied to the table. This is useful for providing default cell/header/footer renderers, or turning on or off certain features for all display columns.
|
|
12
|
+
- `displayColumnDefOptions` - type `{ [key: string]: MRT_DisplayColumnDef<TData> }` - Customize and override the column definition options for the built-in display columns. (Select, Expand, Row Actions, etc.)
|
|
13
|
+
|
|
14
|
+
### Built-in MRT Display Columns
|
|
15
|
+
|
|
16
|
+
Material React Table has a few built-in display columns that are created automatically when certain features are enabled.
|
|
17
|
+
|
|
18
|
+
- `mrt-row-pin` - created when `enableRowPinning` table option is `true` with certain `rowPinningDisplayMode` values
|
|
19
|
+
- `mrt-row-drag` - created when `enableRowDragging` or `enableRowOrdering` table option are `true`
|
|
20
|
+
- `mrt-row-actions` - created when `enableRowActions` (or sometimes when `enableEditing`) props are `true`
|
|
21
|
+
- `mrt-row-expand` - created when `enableExpanding`, `enableGrouping`, or `renderDetailPanel` props are `true`
|
|
22
|
+
- `mrt-row-select` - created when `enableRowSelection` table option is `true`
|
|
23
|
+
- `mrt-row-numbers` - created when `enableRowNumbers` table option is `true`
|
|
24
|
+
- `mrt-row-spacer` - created when `layoutMode` is `"grid-no-grow"` (column resizing)
|
|
25
|
+
|
|
26
|
+
Display columns are, for the most part, the same as a data column, except they do not have an accessor to access data. When a display column is created internally by Material React Table, the following options are all set to false by default:
|
|
27
|
+
|
|
28
|
+
```tsx
|
|
29
|
+
const defaultDisplayColumnDefOptions = {
|
|
30
|
+
columnDefType: 'display',
|
|
31
|
+
enableClickToCopy: false,
|
|
32
|
+
enableColumnActions: false,
|
|
33
|
+
enableColumnDragging: false,
|
|
34
|
+
enableColumnFilter: false,
|
|
35
|
+
enableColumnOrdering: false,
|
|
36
|
+
enableEditing: false,
|
|
37
|
+
enableGlobalFilter: false,
|
|
38
|
+
enableGrouping: false,
|
|
39
|
+
enableHiding: false,
|
|
40
|
+
enableResizing: false,
|
|
41
|
+
enableSorting: false,
|
|
42
|
+
} as Partial<MRT_ColumnDef>;
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
All of these values are able to be overridden if needed, and you'll learn about that in the next section down below.
|
|
46
|
+
|
|
47
|
+
### Customize Built-in MRT Display Columns
|
|
48
|
+
|
|
49
|
+
It is possible to change and override the default behavior of built-in display columns. Whether you want to change the default column width, add some styles, or enable some features, such as column actions or column drag and drop, you can do it with the `displayColumnDefOptions` table option.
|
|
50
|
+
|
|
51
|
+
#### Default Display Column Table Option
|
|
52
|
+
|
|
53
|
+
First of all, if you want to enable or disable some feature for all display columns, you can just use the `defaultDisplayColumn` table option. This will apply to all display columns, including any custom display columns you create.
|
|
54
|
+
|
|
55
|
+
```jsx
|
|
56
|
+
const table = useMaterialReactTable({
|
|
57
|
+
columns,
|
|
58
|
+
data,
|
|
59
|
+
defaultDisplayColumn: {
|
|
60
|
+
enableColumnOrdering: true,
|
|
61
|
+
enableColumnResizing: true,
|
|
62
|
+
minSize: 100,
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
#### Display Column Definition Options Table Option
|
|
68
|
+
|
|
69
|
+
Let's say you need to adjust the width of the Actions column to be wide enough to fit all of your action buttons. You could do that as follows:
|
|
70
|
+
|
|
71
|
+
```jsx
|
|
72
|
+
const table = useMaterialReactTable({
|
|
73
|
+
columns,
|
|
74
|
+
data,
|
|
75
|
+
displayColumnDefOptions: { 'mrt-row-actions': { size: 300 } }, //change width of actions column to 300px
|
|
76
|
+
enableRowActions: true,
|
|
77
|
+
renderRowActions: ({ row }) => (
|
|
78
|
+
<Box>
|
|
79
|
+
<Button>Action 1</Button>
|
|
80
|
+
<Button>Action 2</Button>
|
|
81
|
+
<Button>Action 3</Button>
|
|
82
|
+
</Box>
|
|
83
|
+
),
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return <MaterialReactTable table={table} />;
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Or maybe you want to enable a feature that is off by default for display columns, such as column ordering or pinning.
|
|
90
|
+
|
|
91
|
+
```jsx
|
|
92
|
+
const table = useMaterialReactTable({
|
|
93
|
+
columns,
|
|
94
|
+
data,
|
|
95
|
+
displayColumnDefOptions: {
|
|
96
|
+
'mrt-row-numbers': {
|
|
97
|
+
enableOrdering: true,
|
|
98
|
+
enablePinning: true,
|
|
99
|
+
enableColumnActions: true,
|
|
100
|
+
size: 40,
|
|
101
|
+
grow: true, //new in v2.8
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
enableRowNumbers: true,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return <MaterialReactTable table={table} />;
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Here is a full example and demo for customizing display columns.
|
|
111
|
+
|
|
112
|
+
> Live example: `customize-display-columns` (call get_mrt_example with that id).
|
|
113
|
+
|
|
114
|
+
### Create your own Display Columns
|
|
115
|
+
|
|
116
|
+
You do not have to use the built in Row Actions feature. You can just create your own display columns instead. It is as easy as creating a normal column, only specifying the `columnDefType` as `display`.
|
|
117
|
+
|
|
118
|
+
```jsx
|
|
119
|
+
const columns = [
|
|
120
|
+
{
|
|
121
|
+
id: 'sendEmail',
|
|
122
|
+
header: 'Send Email',
|
|
123
|
+
columnDefType: 'display', //turns off data column features like sorting, filtering, etc.
|
|
124
|
+
enableColumnOrdering: true, //but you can turn back any of those features on if you want like this
|
|
125
|
+
Cell: ({ row }) => (
|
|
126
|
+
<Button onClick={() => sendEmail(row.original.userId)}>Send Email</Button>
|
|
127
|
+
),
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
id: 'name',
|
|
131
|
+
header: 'Name',
|
|
132
|
+
accessorKey: 'name',
|
|
133
|
+
},
|
|
134
|
+
];
|
|
135
|
+
```
|