React admin 在SelectInput中将管理员更改值转换为自定义值

React admin 在SelectInput中将管理员更改值转换为自定义值,react-admin,selectinput,React Admin,Selectinput,我在react admin中使用了以下代码: <ReferenceInput label="Animal" source="id"reference="animal"> <SelectInput optionText="type" choices={[ {id: '0', name: 'Cat' }, { id: '1', name: 'Dog'},]}/> </ReferenceInput> 在该代码中,它从我的数据库接收值“0”到一只猫,“1”

我在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'},]}
,但在下拉列表中没有效果


有人知道我怎么做吗?

您可以在optionText属性中传递显示所选记录的功能,而不是显示带有字段名的字符串:

<ReferenceInput label="Animal" source="id" reference="animal">
   <SelectInput optionText={ choice => choice.type === 0 ? 'Cat' : 'Dog' }/>
</ReferenceInput>

choice.type==0?“猫:'狗'}/>

我发现了我的问题所在

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

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

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

其中数值不使用引号