【问题标题】:Admin-on-Rest ReferenceField not rendering properlyAdmin-on-Rest ReferenceField 未正确呈现
【发布时间】:2017-08-27 12:38:11
【问题描述】:

我没有让 ReferenceField 正常工作,因为它只显示“firstName”字段的进度条“旋转”。

我可以成功地使用来自https://jsonplaceholder.typicode.com/ 的数据的示例来使用 simpleRest 客户端,但不能使用我自己的 API 端点。我看到了 GET_LIST 请求 - 响应,然后是带有所有者数据的相关 GET_MANY 响应,但标记为“用户”的参考字段没有呈现。

API 端点结构的差异之一是我的 API 中返回的引用是完全限定的,遵循此结构“resource:io.mydomain.#”

但由于第二个 API 调用被触发并返回数据,这似乎无关紧要。在这一点上,我需要一些指示下一步去哪里......

//App.js
const App = () => (
  <Admin dashboard={Dashboard} theme={getMuiTheme(darkBaseTheme)} title="Admin" authClient={authClient} restClient={restClient}>
      <Resource name="Asset" list={AssetList} label="Assets" />
      <Resource name="Owner" list={UserList}  label="Users"  />
      <Resource name="AssetTitle" list={AssetTitleList}  label="Ownership" />
 </Admin>

//users.js
 export const UserList = (props) => (
     <List title="All Users" {...props}>
         <Datagrid>
             <TextField source="id" />
             <TextField source="firstName" />
             <TextField source="lastName" />
             <TextField source="mobile" />
             <EmailField source="email" />
               <EditButton />
         </Datagrid>
     </List>
 );

 //titles.js
 export const AssetTitleList = (props) => (
     <List title="All Titles..." {...props}>
         <Datagrid>
           <TextField source="id" />
           <ReferenceField label="User" source="owner" reference="Owner" >
                          <TextField source="firstName" />
           </ReferenceField>
           <TextField source="information" />
         </Datagrid>
     </List>
 );

//AssetTitle API returns
[{"$class":"io.mydomain.AssetTitle",
"id":"12345",
"owner":"resource:io.mydomain.Owner#11111",
"asset":"resource:io.mydomain.Asset#991",
"information":""}]

//Owner API returns
[{"$class":"io.mydomain.Owner",
"firstName":"Joe",
"lastName":"Doe",
"email":"joe@doe.com",
"mobile":"",
"id":"11111"}]

Screenshot of Referencefield rendering

【问题讨论】:

  • 你能在控制台看到什么吗?标题总是有所有者吗?如果没有,请尝试在您的 ReferenceField 上添加 allowEmpty
  • 是的,标题永远有一个所有者。

标签: admin-on-rest


【解决方案1】:

您可能需要在您的restClient 中清理从 API 返回的数据。

我看到title 资源的owner 字段是resource:io.mydomain.Owner#11111。但是,owner 资源的id 字段是11111

您必须实现自己的 restClient 并“清理”所有已知的 id,例如 owner 字段,以便它们只包含 id。请参阅documentation 了解如何实现您自己的 REST 客户端。

【讨论】:

  • 好的,谢谢,所以在 convertHTTPResponseToREST 方法中,如果是 GET_MANY_REFERENCE 类型,我可以在返回之前修剪响应吗?
  • 按照建议清理带有完全限定参考字段的响应,在 convertHTTPResponseToREST 方法中 - case GET_MANY_REFERENCE,问题就解决了。
  • 你可能也必须为其他动词这样做
猜你喜欢
  • 1970-01-01
  • 2017-09-21
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多