简体   繁体   English

React-Admin:如何处理对象列表中的输入源

[英]React-Admin: How do I handle an input source on a list of objects

In react-admin , I have an input whose source is a list of objects.react-admin中,我有一个input ,其源是对象列表。

<CheckboxGroupInput  source="binded_cameras" choices={choices}/>

The binded_cameras list looks like this: binded_cameras列表如下所示:

"binded_cameras": [
    {
       "id": 1,
       "name": "Cam 1",
       "url": "dummyurl.com"
    },
    {
       "id": 4,
       "name": "Cam 2",
       "url": "dummyurl.com"
     }
]

I am trying to get only the list of id to deal with.我试图只获取要处理的id列表。
I tried binded_cameras.id but of course it did not work.我试过binded_cameras.id但它当然没有用。
How do I manipulate this list of objects, and generate only a list of ids?如何操作这个对象列表,并只生成一个 id 列表?

Since React-admin uses react-final-form, you can it's use parse() and format() functions to transform the input value when saving to and loading from the record, just pass them as props to Input, in this case CheckboxGroupInput:由于 React-admin 使用 react-final-form,您可以使用 parse() 和 format() 函数在保存到记录和从记录加载时转换输入值,只需将它们作为道具传递给 Input,在本例中为 CheckboxGroupInput:

Mnemonic for the two functions:两个函数的助记符:

parse(): input -> record parse(): 输入 -> 记录

format(): record -> input格式():记录->输入

<CheckboxGroupInput 
    source="binded_cameras" 
    choices={choices}
    parse={ids => ids.map(id => ({id}))}
    format={bindedCameras => bindedCameras.map(b => b.id)}
/>

https://marmelab.com/react-admin/Inputs.html#transforming-input-value-tofrom-record https://marmelab.com/react-admin/Inputs.html#transforming-input-value-tofrom-record

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM