简体   繁体   English

如何在 react-admin 的 ReferenceInput 字段中格式化选项的文本?

[英]How to format the text of the choices in a ReferenceInput field in react-admin?

The ReferenceInput field in combination with SelectInput allows to populate the option texts from a column of a referenced resource by giving the name of the column like so: ReferenceInput 字段与 SelectInput 结合允许通过给出列的名称从引用资源的列填充选项文本,如下所示:

<ReferenceInput label="Location name" source="id" reference="Location">
 <SelectInput optionText={"building"} />
</ReferenceInput>

I'd like to combine the option text from several columns, but I don't know how I access the current record.我想组合几列中的选项文本,但我不知道如何访问当前记录。 I want to achieve something like in this code (that of course does not work, just to show what I mean):我想在这段代码中实现类似的东西(这当然不起作用,只是为了说明我的意思):

<ReferenceInput label="Location name" source="id" reference="Location">
 <SelectInput optionText=`${record.building} ${record.room}` />
</ReferenceInput>

optionText also accepts a function, so you can shape the option text at will: optionText也接受一个函数,因此您可以随意塑造选项文本:

const choices = [
   { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
   { id: 456, first_name: 'Jane', last_name: 'Austen' },
];
const optionRenderer = choice => `${choice.first_name} ${choice.last_name}`;
<SelectInput source="author_id" choices={choices} optionText={optionRenderer} />

optionText also accepts a React Element, that will be cloned and receive the related choice as the record prop. optionText还接受一个 React 元素,它将被克隆并接收相关的选择作为record道具。 You can use Field components there.您可以在那里使用 Field 组件。

const choices = [
   { id: 123, first_name: 'Leo', last_name: 'Tolstoi' },
   { id: 456, first_name: 'Jane', last_name: 'Austen' },
];
const FullNameField = ({ record }) => <span>{record.first_name} {record.last_name}</span>;
<SelectInput source="gender" choices={choices} optionText={<FullNameField />}/>

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

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