【问题标题】:How to make ReferenceManyField to match childs from a url?如何使 ReferenceManyField 匹配来自 url 的孩子?
【发布时间】:2018-08-02 17:32:52
【问题描述】:

我正在使用 react-admin 和 Spring REST 作为数据 API 构建应用程序。我处于具有相对 cme​​ts 的 Product 对象的典型情况。

在显示产品的元素中,我添加了 ReferenceManyField 以显示该产品的 cmets。但问题是:根据参考文档,该组件获取调用 cmets 列表的 cmets 并查找具有父亲 id 的 cmets:

https://marmelab.com/react-admin/Fields.html#referencemanyfield

但由于我的 Spring REST 为一种特定产品产生了类似的东西:

{
  "id" : 1,
  "description" : "Description of Product 1",
  "price" : 50045.0,
  "title" : "Title of product 1",
  "additionaldata" : [ ],
  "pimages" : [ ],
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/products/1"
    },
    "product" : {
      "href" : "http://localhost:8080/products/1"
    },
    "pcomments" : {
      "href" : "http://localhost:8080/products/1/pcomments"
    },
    "vendor" : {
      "href" : "http://localhost:8080/products/1/vendor"
    }
  }
}

逻辑是相反的,我们找到要调用的 URI 以查找该特定产品的 cmets 列表。

如何让 ReferenceManyField 调用 http://localhost:8080/products/1/pcomments 来获取该产品的 cmets?

【问题讨论】:

    标签: spring-rest react-admin


    【解决方案1】:

    尝试执行以下操作:

    <ReferenceManyField source="your_source" target="_links['pcomments']['href']" reference="products"/>
    

    【讨论】:

    • 感谢您的回复。我试过这个,但显然它不起作用,因为 cmets 列表中没有父 ID &lt;ReferenceManyField label="Commenti" reference="pcomments" target="id"&gt; &lt;SingleFieldList&gt; &lt;ChipField source="content" /&gt; &lt;/SingleFieldList&gt; &lt;/ReferenceManyField&gt;
    【解决方案2】:

    您可以将您的搜索 url 包装在 ReferenceManyField 的 filter 属性中,然后在您的 dataProvider 中从 GET_MANY_REFERENCE(switch case) 中获取过滤器。那么你就可以随心所欲了

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多