2

我在 react-admin 中使用了以下代码:

<ReferenceInput label="Animal" source="id"reference="animal">
   <SelectInput optionText="type" choices={[ {id: '0', name: 'Cat' }, { id: '1', name: 'Dog'},]}/>
</ReferenceInput>

在该代码中,它从我的数据库接收值“0”到猫和“1”到狗,当我单击下拉菜单时,它显示“0”和“1”,但我只想显示猫和狗我的 SelectInput 中的“0”和“1”。

我尝试使用:choices={[ {id: '0', name: 'Cat' }, { id: '1', name: 'Dog'},]},但在下拉列表中没有效果。

有谁知道我该怎么做?

4

2 回答 2

1

您可以传入 optionText 道具显示所选记录的功能,而不是带有字段名称的字符串:

<ReferenceInput label="Animal" source="id" reference="animal">
   <SelectInput optionText={ choice => choice.type === 0 ? 'Cat' : 'Dog' }/>
</ReferenceInput>
于 2019-07-25T03:00:16.060 回答
0

我发现我的问题是什么。

来自数据库的值只是一个数值 (0, 1),我使用的是 ('0', '1')。因此,SelectInput 无法使用正确的值在 DropDown 中显示。

正确的方式应该是这样的:

<SelectInput 
 optionText = "type" 
 choices = {[{id: 0, name: 'Cat'}, {id: 1, name: 'Dog'},]} 
/>

其中数值不使用引号。

于 2019-08-05T18:09:13.607 回答