You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/ReferenceManyToManyField.md
+34-8Lines changed: 34 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@ title: "The ReferenceManyToManyField Component"
5
5
6
6
# `<ReferenceManyToManyField>`
7
7
8
-
This [Enterprise Edition](https://react-admin-ee.marmelab.com)<imgclass="icon"src="./img/premium.svg" /> component fetches a list of referenced records by lookup in an associative table, and passes the records down to its child component, which must be an iterator component (e.g. `<SingleFieldList>`).
8
+
This [Enterprise Edition](https://react-admin-ee.marmelab.com)<imgclass="icon"src="./img/premium.svg"alt="React Admin Enterprise Edition icon"/> component fetches a list of referenced records by lookup in an associative table, and passes the records down to its child component, which must be an iterator component (e.g. `<SingleFieldList>`).
9
9
10
10

11
11
@@ -15,7 +15,7 @@ Note: The `<ReferenceManyToManyField>` cannot currently display multiple records
15
15
16
16
Let's imagine that you're writing an app managing concerts for artists. The data model features a many-to-many relationship between the `bands` and `venues` tables through a `performances` associative table.
17
17
18
-
```
18
+
```txt
19
19
┌─────────┐ ┌──────────────┐ ┌───────────────┐
20
20
│ bands │ │ performances │ │ venues │
21
21
│---------│ │--------------│ │---------------│
@@ -28,7 +28,7 @@ Let's imagine that you're writing an app managing concerts for artists. The data
28
28
29
29
In this example, `bands.id` matches `performances.band_id`, and `performances.venue_id` matches `venues.id`.
30
30
31
-
To allow users see the `venues` for a given `band` in `<SingleFieldList>`, wrap that component in `<ReferenceManyToManyField>` where you define the relationship via the `reference`, `through` and `using` props:
31
+
To allow users see the `venues` for a given `band` in `<SingleFieldList>`, wrap that component in `<ReferenceManyToManyField>` where you define the relationship via the `reference`, `through` and `using` props:
|`children`| Required |`element`| - | An iterator element (e.g. `<SingleFieldList>` or `<Datagrid>`). The iterator element usually has one or more child `<Field>` components. |
63
63
|`reference`| Required |`string`| - | Name of the reference resource, e.g. 'venues' |
64
-
| `through` | Required | `string` | - | Name of the resource for the associative table, e.g. 'performances'
64
+
|`through`| Required |`string`| - | Name of the resource for the associative table, e.g. 'performances' |
65
65
|`filter`| Optional |`object`|`{}`| Filter for the associative table (passed to the `getManyReference()` call) |
66
66
|`joinLimit`| Optional |`number`| 100 | Limit for the number of results fetched from the associative table. Should be **greater than `perPage`**|
67
67
|`perPage`| Optional |`number`| 25 | Limit the number of displayed result after `getManyReference` is called. Useful when using a pagination component. Should be **smaller than `joinLimit`**|
68
+
|`queryOptions`| Optional |`UseQueryOptions`| - | Query options for the `getMany` and `getManyReference` calls |
68
69
|`sort`| Optional |`{ field: string, order: 'ASC' or 'DESC' }`|`{ field: 'id', order: 'DESC' }`| Sort for the associative table (passed to the `getManyReference()` call) |
69
70
|`source`| Optional |`string`|`'id'`| Name of the field containing the identity of the main resource. Used determine the value to look for in the associative table. |
70
71
|`using`| Optional |`string`|`'[resource]_id,[reference]_id'`| Tuple (comma separated) of the two field names used as foreign keys, e.g 'band_id,venue_id'. The tuple should start with the field pointing to the resource, and finish with the field pointing to the reference |
71
72
72
73
## `children`
73
74
74
-
`<ReferenceManyToManyField>` expects an _iterator_ component as child, i.e. a component working inside a `ListContext`.
75
+
`<ReferenceManyToManyField>` expects an _iterator_ component as child, i.e. a component working inside a `ListContext`.
75
76
76
77
This means you can use a `<Datagrid>` instead of a `<SingleFieldList>`, which is useful if you want to display more details about related records. For instance, to display the venue `name` and `location`:
You can filter the records of the associative table (e.g. `performances`) using the `filter` prop. This `filter` is passed to the `getManyReference()` call.
106
107
107
108
{% raw %}
109
+
108
110
```tsx
109
111
<ReferenceManyToManyField
110
112
reference="venues"
@@ -115,6 +117,7 @@ You can filter the records of the associative table (e.g. `performances`) using
115
117
{/* ... */}
116
118
</ReferenceManyToManyField>
117
119
```
120
+
118
121
{% endraw %}
119
122
120
123
## `joinLimit`
@@ -165,6 +168,27 @@ import { Pagination } from 'react-admin';
165
168
</ReferenceManyToManyField>
166
169
```
167
170
171
+
## `queryOptions`
172
+
173
+
Use the `queryOptions` prop to customize the queries for `getMany` and `getManyReference`.
174
+
175
+
You can for instance use it to pass [a custom meta](./Actions.md#meta-parameter) to the dataProvider.
176
+
177
+
{% raw %}
178
+
179
+
```tsx
180
+
<ReferenceManyToManyField
181
+
reference="venues"
182
+
through="performances"
183
+
using="band_id,venue_id"
184
+
queryOptions={{ meta: { myParameter: 'value' } }}
185
+
>
186
+
{/* ... */}
187
+
</ReferenceManyToManyField>
188
+
```
189
+
190
+
{% endraw %}
191
+
168
192
## `reference`
169
193
170
194
The name of the target resource to fetch.
@@ -187,6 +211,7 @@ For instance, if you want to display the `venues` of a given `bands`, through `p
187
211
By default, react-admin orders the possible values by `id` desc for the associative table (e.g. `performances`). You can change this order by setting the `sort` prop (an object with `field` and `order` properties) to be applied to the associative resource.
188
212
189
213
{% raw %}
214
+
190
215
```tsx
191
216
<ReferenceManyToManyField
192
217
reference="venues"
@@ -197,6 +222,7 @@ By default, react-admin orders the possible values by `id` desc for the associat
197
222
{/* ... */}
198
223
</ReferenceManyToManyField>
199
224
```
225
+
200
226
{% endraw %}
201
227
202
228
## `source`
@@ -242,8 +268,8 @@ You can specify the columns to use in the associative `using` the using prop.
242
268
243
269
`<ReferenceManyToManyField>` fetches the `dataProvider` twice in a row:
244
270
245
-
-once to get the records of the associative resource (`performances` in this case), using a `getManyReference()` call
246
-
-once to get the records of the reference resource (`venues` in this case), using a `getMany()` call.
271
+
- once to get the records of the associative resource (`performances` in this case), using a `getManyReference()` call
272
+
- once to get the records of the reference resource (`venues` in this case), using a `getMany()` call.
247
273
248
274
For instance, if the user displays the band of id `123`, `<ReferenceManyToManyField>` first issues the following query to the `dataProvider`:
249
275
@@ -286,4 +312,4 @@ And receives the reference venues:
0 commit comments