嵌套引用字段 [英] Nested Reference Field

查看:45
本文介绍了嵌套引用字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了检索设备类型,我使用了一个将检索设备型号,然后另一个使用设备型号的字段typeID"来引用设备类型来检索设备类型.

In order to retrieve the equipment type I am using a that will retrieve the equipment model and then another that references the equipment type using the equipment model's field "typeID" to retrieve the equipment type.

但是它显示以下警告:

警告:失败的道具类型:无效的道具 translateChoice of type布尔值提供给 ReferenceField,预期功能.

Warning: Failed prop type: Invalid prop translateChoice of type boolean supplied to ReferenceField, expected function.

图片代表数据模型(一个设备有一个设备模型,一个设备模型有一个设备类型)

The image represents the data model (an equipment has an equipment model, and an equipment model has an equipment type)

推荐答案

我发现更好的解决方案有点像黑客,但似乎更有效.

I found a better solution is kinda of an hack but seems to be more efficient.

以问题为例哪里只需要获取设备类型,它会是这样的:

Taking the question example where in order to get equipmentType is only needed <ReferenceField>, it would be something like this:

    const EquipList = ({...props}) => {
      <List {...props}>
        <Datagrid>

          <ReferenceFieldController label="Equipment Type" reference="equipmentModel" source="modelID" linkType={false}>
            {({referenceRecord, ...props}) => (
              <ReferenceField basePath="/equipmentModel" resource="equipmentModel" reference="equipmentType" source="typeID" record={referenceRecord || {}} linkType="show">
                <TextField source="name" />
              </ReferenceField>
            )}
          </RefenceFieldController>

        </Datagrid>
      </List>
    }

在上面的示例中, 获取设备的设备模型,类似于 .标签是必需的,因为 RA 使用第一个 来显示 中的列标题,如果您使用国际化,则应将翻译函数应用于正确的资源在这个道具上.

In the example above <ReferenceFieldController> fetches the equipmentModel of equipment, as like <ReferenceField>. Label is needed because RA uses the first <ReferenceField> to show the column header in <Datagrid>, if you use internationalization you should apply translate function to the correct resource on this prop.

获取记录并将其作为 referenceRecord 传递给子函数,该子函数将为字段呈现呈现组件.您呈现一个 <ReferenceField> 来获取嵌套关系,然后显示该字段,而不是呈现字段组件.由于 <ReferenceFieldController> 仅将控制器道具传递给其子级,并且字段组件的道具在嵌套关系中不会执行您想要的操作,因此您必须将它们显式传递给 ;.您需要将 record 作为 referenceRecord ||{} 因为最初的 referenceRecord 尚未提取,并且 不能将记录设为 null.

<ReferenceController> fetches the record and passes it as referenceRecord to a child function that will render the component for field presentation. Instead of presenting the field component you render a <ReferenceField> to fetch the nested relation and next you show the field. Since <ReferenceFieldController> only passes controller props to its child and the props of field component don't do what you want in the nested relation, you have to explicit pass them to <ReferenceField>. You need to pass record of <ReferenceField> as referenceRecord || {} because the initially the referenceRecord is not fetched yet and <ReferenceField> doesn't work with record as null.

<ReferenceFieldController> 的 linkType 设置为 false 使其不会呈现将用户重定向到的 组件路线不正确.

Setting the linkType of <ReferenceFieldController> to false makes it not render a <Link> component that would redirect the user to an incorrect route.

这篇关于嵌套引用字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆