|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: "The DataTableInput Component" |
| 4 | +--- |
| 5 | + |
| 6 | +# `<DataTableInput>` |
| 7 | + |
| 8 | +`<DataTableInput>` is an [Enterprise Edition](https://react-admin-ee.marmelab.com)<img class="icon" src="./img/premium.svg" alt="React Admin Enterprise Edition icon" /> input from `@react-admin/ra-form-layout` that lets users select one or many choices in a dialog containing a [`<DataTable>`](./DataTable.md). |
| 9 | + |
| 10 | +It combines: |
| 11 | + |
| 12 | +- an input showing the current selection(s), |
| 13 | +- and a dialog with sorting, filtering, and pagination to pick records. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +`<DataTableInput>` is a good alternative to [`<SelectInput>`](./SelectInput.md), [`<AutocompleteInput>`](./AutocompleteInput.md), and [`<AutocompleteArrayInput>`](./AutocompleteArrayInput.md) when you have many choices, or when users need more than one field to identify each choice. |
| 20 | + |
| 21 | +First, install the `@react-admin/ra-form-layout` package: |
| 22 | + |
| 23 | +```sh |
| 24 | +npm install --save @react-admin/ra-form-layout |
| 25 | +# or |
| 26 | +yarn add @react-admin/ra-form-layout |
| 27 | +``` |
| 28 | + |
| 29 | +**Tip**: `@react-admin/ra-form-layout` is hosted in a private npm registry. You need an [Enterprise Edition](https://react-admin-ee.marmelab.com/) subscription to access it. |
| 30 | + |
| 31 | +Test it live in [the Enterprise Storybook](https://react-admin.github.io/ra-enterprise/?path=/story/ra-form-layout-listinputs-datatableinput--basic). |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +Like `<SelectInput>`, `<DataTableInput>` can be used with a `choices` array. The difference is that you define table columns as children with `<DataTable.Col>`. |
| 36 | + |
| 37 | +```tsx |
| 38 | +import { Edit, SimpleForm, DataTable, TextInput } from 'react-admin'; |
| 39 | +import { DataTableInput } from '@react-admin/ra-form-layout'; |
| 40 | + |
| 41 | +const suppliers = [ |
| 42 | + { |
| 43 | + id: 101, |
| 44 | + company_name: 'Acme Industrial', |
| 45 | + category: 'Machining', |
| 46 | + city: 'Detroit', |
| 47 | + country: 'US', |
| 48 | + lead_time_days: 7, |
| 49 | + reliability_score: 98, |
| 50 | + }, |
| 51 | + { |
| 52 | + id: 102, |
| 53 | + company_name: 'Nordic Components', |
| 54 | + category: 'Electronics', |
| 55 | + city: 'Stockholm', |
| 56 | + country: 'SE', |
| 57 | + lead_time_days: 11, |
| 58 | + reliability_score: 95, |
| 59 | + }, |
| 60 | +]; |
| 61 | + |
| 62 | +export const ProductEdit = () => ( |
| 63 | + <Edit> |
| 64 | + <SimpleForm> |
| 65 | + <TextInput source="sku" /> |
| 66 | + <TextInput source="name" /> |
| 67 | + <DataTableInput |
| 68 | + source="preferred_supplier_id" |
| 69 | + resource="suppliers" |
| 70 | + choices={suppliers} |
| 71 | + optionText="company_name" |
| 72 | + > |
| 73 | + <DataTable.Col source="company_name" /> |
| 74 | + <DataTable.Col source="category" /> |
| 75 | + <DataTable.Col source="city" /> |
| 76 | + <DataTable.Col source="country" /> |
| 77 | + <DataTable.NumberCol source="lead_time_days" label="Lead Time (days)" /> |
| 78 | + <DataTable.NumberCol source="reliability_score" label="Reliability %" /> |
| 79 | + </DataTableInput> |
| 80 | + </SimpleForm> |
| 81 | + </Edit> |
| 82 | +); |
| 83 | +``` |
| 84 | + |
| 85 | +## With Reference Inputs |
| 86 | + |
| 87 | +You can use `<DataTableInput>` as a child of: |
| 88 | + |
| 89 | +- [`<ReferenceInput>`](./ReferenceInput.md) |
| 90 | +- [`<ReferenceArrayInput>`](./ReferenceArrayInput.md) |
| 91 | +- [`<ReferenceManyToManyInput>`](./ReferenceManyToManyInput.md)<img class="icon" src="./img/premium.svg" alt="React Admin Enterprise Edition icon" /> |
| 92 | + |
| 93 | +In that case, choices are fetched automatically from the reference resource. |
| 94 | + |
| 95 | +```tsx |
| 96 | +import { |
| 97 | + DataTable, |
| 98 | + Edit, |
| 99 | + ReferenceArrayInput, |
| 100 | + SimpleForm, |
| 101 | + TextInput, |
| 102 | +} from 'react-admin'; |
| 103 | +import { DataTableInput } from '@react-admin/ra-form-layout'; |
| 104 | + |
| 105 | +export const ReleaseEdit = () => ( |
| 106 | + <Edit> |
| 107 | + <SimpleForm> |
| 108 | + <TextInput source="version" /> |
| 109 | + <ReferenceArrayInput source="reviewer_ids" reference="users"> |
| 110 | + <DataTableInput multiple> |
| 111 | + <DataTable.Col source="full_name" /> |
| 112 | + <DataTable.Col source="team" /> |
| 113 | + <DataTable.Col source="role" /> |
| 114 | + <DataTable.Col source="timezone" /> |
| 115 | + <DataTable.Col source="location" /> |
| 116 | + </DataTableInput> |
| 117 | + </ReferenceArrayInput> |
| 118 | + </SimpleForm> |
| 119 | + </Edit> |
| 120 | +); |
| 121 | +``` |
| 122 | + |
| 123 | +## Multiple Selection |
| 124 | + |
| 125 | +Set `multiple` to `true` when the field stores an array of identifiers. |
| 126 | + |
| 127 | +```tsx |
| 128 | +import { Create, DataTable, SimpleForm } from 'react-admin'; |
| 129 | +import { DataTableInput } from '@react-admin/ra-form-layout'; |
| 130 | + |
| 131 | +const tags = [ |
| 132 | + { id: 'backend', name: 'Backend', group: 'Engineering' }, |
| 133 | + { id: 'frontend', name: 'Frontend', group: 'Engineering' }, |
| 134 | + { id: 'design', name: 'Design', group: 'Product' }, |
| 135 | +]; |
| 136 | + |
| 137 | +export const ArticleCreate = () => ( |
| 138 | + <Create> |
| 139 | + <SimpleForm> |
| 140 | + <DataTableInput source="tag_ids" choices={tags} multiple> |
| 141 | + <DataTable.Col source="name" /> |
| 142 | + <DataTable.Col source="group" /> |
| 143 | + </DataTableInput> |
| 144 | + </SimpleForm> |
| 145 | + </Create> |
| 146 | +); |
| 147 | +``` |
| 148 | + |
| 149 | +## Props |
| 150 | + |
| 151 | +In addition to the [common input props](./Inputs.md#common-input-props), `<DataTableInput>` accepts: |
| 152 | + |
| 153 | +| Prop | Required | Type | Default | Description | |
| 154 | +| ---------------- | -------- | ----------------------------------------- | --------------------------------------------------- | ----------- | |
| 155 | +| `actions` | Optional | `ReactElement` | `false` | auto (`false` when no filters) | Actions toolbar displayed in the dialog | |
| 156 | +| `children` | Required | `ReactNode` | - | `<DataTable.Col>` elements defining displayed columns | |
| 157 | +| `choices` | Optional | `RaRecord[]` | - | Choices for standalone usage (not needed inside reference inputs) | |
| 158 | +| `dataTableProps` | Optional | `DataTableProps` | - | Props forwarded to the inner [`<DataTable>`](./DataTable.md) | |
| 159 | +| `dialogProps` | Optional | `DialogProps` | - | Props forwarded to the selection dialog | |
| 160 | +| `filters` | Optional | `ReactElement` | `ReactElement[]` | `false` | - | Filters displayed in the dialog toolbar | |
| 161 | +| `multiple` | Optional | `boolean` | `false` | Enables multiple selection (value becomes an array of ids) | |
| 162 | +| `optionText` | Optional | `OptionText` | resource `recordRepresentation` or `name` | Label used for selected chips in standalone mode | |
| 163 | +| `optionValue` | Optional | `string` | `id` | Field used as choice value in standalone mode | |
| 164 | +| `pagination` | Optional | `ReactNode` | [`<Pagination />`](./Pagination.md) | Pagination element rendered below the table | |
| 165 | +| `title` | Optional | `string` | `ra-form-layout.inputs.datatable_input.dialog_title` | Dialog title (translation key or plain string) | |
| 166 | +| `translateChoice`| Optional | `boolean` | `true` | Whether to translate `optionText` values | |
| 167 | + |
| 168 | +## `filters` and `actions` |
| 169 | + |
| 170 | +Use `filters` and `actions` to provide list-style controls in the dialog toolbar: |
| 171 | + |
| 172 | +```tsx |
| 173 | +import { |
| 174 | + DataTable, |
| 175 | + FilterButton, |
| 176 | + ReferenceInput, |
| 177 | + TextInput, |
| 178 | + TopToolbar, |
| 179 | +} from 'react-admin'; |
| 180 | +import { DataTableInput } from '@react-admin/ra-form-layout'; |
| 181 | + |
| 182 | +const supplierFilters = [ |
| 183 | + <TextInput key="q" source="q" label="Search" alwaysOn />, |
| 184 | + <TextInput key="country" source="country" />, |
| 185 | + <TextInput key="category" source="category" />, |
| 186 | +]; |
| 187 | + |
| 188 | +const SupplierActions = () => ( |
| 189 | + <TopToolbar> |
| 190 | + <FilterButton /> |
| 191 | + </TopToolbar> |
| 192 | +); |
| 193 | + |
| 194 | +<ReferenceInput source="preferred_supplier_id" reference="suppliers"> |
| 195 | + <DataTableInput filters={supplierFilters} actions={<SupplierActions />}> |
| 196 | + <DataTable.Col source="company_name" /> |
| 197 | + <DataTable.Col source="category" /> |
| 198 | + <DataTable.Col source="country" /> |
| 199 | + <DataTable.NumberCol source="lead_time_days" /> |
| 200 | + </DataTableInput> |
| 201 | +</ReferenceInput>; |
| 202 | +``` |
| 203 | + |
| 204 | +## `dataTableProps` |
| 205 | + |
| 206 | +Use `dataTableProps` to customize table behavior (hidden columns, row selectability, etc.): |
| 207 | + |
| 208 | +```tsx |
| 209 | +<ReferenceInput source="preferred_supplier_id" reference="suppliers"> |
| 210 | + <DataTableInput |
| 211 | + dataTableProps={{ |
| 212 | + hiddenColumns: ['city'], |
| 213 | + isRowSelectable: record => record.reliability_score >= 90, |
| 214 | + }} |
| 215 | + > |
| 216 | + <DataTable.Col source="company_name" /> |
| 217 | + <DataTable.Col source="category" /> |
| 218 | + <DataTable.Col source="city" /> |
| 219 | + <DataTable.NumberCol source="reliability_score" /> |
| 220 | + </DataTableInput> |
| 221 | +</ReferenceInput> |
| 222 | +``` |
| 223 | + |
| 224 | +## `dialogProps` |
| 225 | + |
| 226 | +Use `dialogProps` to customize the underlying Material UI dialog: |
| 227 | + |
| 228 | +```tsx |
| 229 | +<ReferenceInput source="preferred_supplier_id" reference="suppliers"> |
| 230 | + <DataTableInput |
| 231 | + title="Select a supplier" |
| 232 | + dialogProps={{ maxWidth: 'lg', fullWidth: true }} |
| 233 | + > |
| 234 | + <DataTable.Col source="company_name" /> |
| 235 | + <DataTable.Col source="country" /> |
| 236 | + <DataTable.Col source="city" /> |
| 237 | + </DataTableInput> |
| 238 | +</ReferenceInput> |
| 239 | +``` |
0 commit comments