简体   繁体   English

在 React Admin 中从同一端点创建多个资源以应用不同的过滤器

[英]Create multiple resources from the same endpoint in React Admin to have different filters applied

I need to create different routes in React admin, all based on the same endpoint but with a different filter.我需要在 React admin 中创建不同的路由,所有路由都基于相同的端点但具有不同的过滤器。 My need is that I need a Menu entry to show all properties with status = approved, all properties with status = review and so on.我的需要是我需要一个菜单​​条目来显示所有状态 = 已批准的属性、所有状态 = 审查的属性等等。 I tried doing this:我试过这样做:

export default function App() {
  return (
    <Admin
      loginPage={CustomLoginPage}
      dataProvider={dataProvider}
      authProvider={authProvider}
    >
      <Resource
        name="properties"
        options={{ label: 'Properties in review' }}
        icon={UserIcon}
        list={PropertyReviewList}
        show={PropertyShow}
        edit={PropertyEdit}
      />
      <Resource
        name="properties"
        options={{ label: 'Properties Approved' }}
        icon={UserIcon}
        list={PropertyApprovedList}
        show={PropertyShow}
        edit={PropertyEdit}
      />
    </Admin>
  );
}

but this is not working as only the last defined property is showing.但这不起作用,因为仅显示最后定义的属性。 What is the best way to achieve what I am trying to achieve?实现我想要实现的目标的最佳方法是什么?

If I understood you correctly, you need a component that renders different data depending on some part of the link that can change.如果我理解正确,您需要一个组件,该组件根据链接的某些部分可以更改来呈现不同的数据。

This can be achieved using query params.这可以使用查询参数来实现。 Here is an interactive code example from react router docs that does what you need: https://v5.reactrouter.com/web/example/query-parameters这是来自反应路由器文档的交互式代码示例,可以满足您的需要: https ://v5.reactrouter.com/web/example/query-parameters

So, you will have a link for your page like "/properties?status=review" where status is the query param.因此,您将有一个页面链接,例如“/properties?status=review”,其中状态是查询参数。 You check it in your component to show data depending on its value您在组件中检查它以根据其值显示数据

status = approved, all properties with status = review状态 = 已批准,所有状态 = 审查的属性

Enter these properties in query.在查询中输入这些属性。 Example:例子:

axios.get("/api/...?status=approved")
axios.get("/api/...?status=review")

Makes these api calls where you need it.在您需要的地方进行这些 api 调用。 You can have a single controller in backend to handle both the requests.您可以在后端有一个控制器来处理这两个请求。

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

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