![](/img/trans.png)
[英]I am trying to map IDs from inside a list of objects into a new list using React js
[英]Can I use ReferenceArrayInput to edit a list of objects (not just a list of IDs)?
在 React-Admin 中,我有一個要管理的項目,它看起來有點像這樣:
{
"title": "Title of my record",
"source_id": "525560",
"source_timestamp": 1547732039,
"tags": [
{
"id": 9,
"name": "Tag0",
"description": "Descriptio of tag 0"
},
{
"id": 10,
"name": "Tag1",
"description": "Descriptio of tag 1"
}
]
}
標簽作為主項目內的對象列表給出。
我希望能夠管理它,但我在編輯視圖中的代碼是錯誤的:
export const IncidentEdit = props => {
var _tag_ids = [1,2,3,4];
return <Edit title={<IncidentTitle/>} {...props}>
<SimpleForm>
<TextInput fullWidth source="title"/>
<TextField source="description"/>
<ReferenceArrayInput label="tags" reference="tag" source="tags">
<SelectArrayInput>
<ChipField source="name"/>
</SelectArrayInput>
</ReferenceArrayInput>
</SimpleForm>
</Edit>
};
這不起作用,因為 ReferenceArrayInput 期望record.tags
是標簽外鍵的列表。 它實際上是一個標簽列表(因此我們實際上不必查找任何內容)。 它也失敗了,因為它返回的是一個外鍵列表——而 API 只是期望返回一個標簽對象列表。
我們可能會爭辯說 API 設計得很糟糕,但是 React Admin 能適應嗎?
有沒有辦法讓我在將這些記錄輸入到 ReferenceArrayInput 之前將它們轉換為外鍵 ID 列表,然后在將它們發送回 API 之前執行相反的操作? 或者,我可以更改 API,使其僅返回外鍵而不返回實際的標記對象。
在這種情況下,您似乎不必獲取其他數據。 您的 API 響應沒問題,也許只是方法不正確。
為什么不將ArrayInput
與SimpleIterator
ArrayInput
使用? 它允許您編輯嵌套記錄中的所有字段,甚至可以在需要時使用ReferenceInputs
。
<ArrayInput source="tags">
<SimpleFormIterator>
<TextInput source="id" disabled />
<TextInput source="name" />
<TextInput source="description" />
</SimpleFormIterator>
</ArrayInput>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.