简体   繁体   English

React-admin 在列表中创建对话框

[英]React-admin create dialog inside List

Is it ever possible to create a the folowing scenario with react-admin?是否有可能使用 react-admin 创建以下场景?

  • Render items with List+Datagrid使用 List+Datagrid 渲染项目
  • Render also a button in each row在每一行也渲染一个按钮
  • User clicks button用户点击按钮
  • Show a dialog with another List+Datagrid from another resource显示来自另一个资源的另一个 List+Datagrid 的对话框

In my implementation, when button is first clicked, it correctly opens dialog and renders answers .在我的实现中,当第一次单击按钮时,它会正确打开对话框并呈现answers However if I change sorting or go to next page, it also affects the data grid that renders questions .但是,如果我更改排序或转到下一页,它也会影响呈现questions的数据网格。

The reason is I guess when I apply sorting etc in the answers list, it changes query string in the browser, and it effects questions too.原因是我猜当我在answers列表中应用排序等时,它会更改浏览器中的查询字符串,并且也会影响questions

Relevant line: https://github.com/marmelab/react-admin/blob/eb3c1acbf4ecc81793b8e790e11a6f84f9bdbc1b/packages/ra-core/src/controller/useListController.ts#L133相关线路: https : //github.com/marmelab/react-admin/blob/eb3c1acbf4ecc81793b8e790e11a6f84f9bdbc1b/packages/ra-core/src/controller/useListController.ts#L133

Is it possible to have two data grid in same page?是否可以在同一页面中有两个数据网格? (With different resources and sorting context etc). (具有不同的资源和排序上下文等)。

My impl:我的实现:


// render a dialog that contains answer for each questions
const ShowAnwersButton  = (props) => {
  const [open, setOpen] = React.useState(false);
  return (
    <>
      <Dialog open={open} onClose={() => setOpen(false)}>
        <List
          resource="answers"
          basePath="/answers"
          filter={{ question_id: props.record.id }}
        >
          <MyDataGrid>
            <TextField source="id" />
            <DateField source="created_at" />
            <TextField source="name" />
          </MyDataGrid>
        </List>
      </Dialog>
      <Button
        variant={open ? "contained" : "outlined"}
        color="primary"
        onClick={(e) => setOpen(true)}
      >
        Answers
      </Button>
    </>
  );
};

// questions
export const QuestionsList = (props) => (
  <List {...props}>
    <Datagrid>
      <TextField source="id"/>
      <DateTime source="created_at" disabled />
      <BooleanField source="disabled" />
      <ShowAnwersButton source="dummy" />
    </EditableDatagrid>
  </List>
);

You can have two Datagrids in the same page, but not two Lists.在同一页面中可以有两个 Datagrid,但不能有两个 List。 In react-admin, the sorting, filtering and pagination of a List use the URL as state, so there can be only one List per route.在 react-admin 中,List 的排序、过滤和分页使用 URL 作为状态,因此每个路由只能有一个 List。

For your use case, you'll have to call the dataProvider.getList() manually, put the results in a <ListContext> , and then you can use a second <Datagrid> inside it.对于您的用例,您必须手动调用 dataProvider.getList(),将结果放入<ListContext> ,然后您可以在其中使用第二个<Datagrid> It requires knowledge of public but undocumented react-admin APIs.它需要公共但未记录的 react-admin API 的知识。 You should be prepared to dig in the react-admin source code for inspiration:您应该准备好挖掘 react-admin 源代码以获得灵感:

  • useListController使用列表控制器
  • useReferenceManyFieldController useReferenceManyFieldController
  • List列表
  • ListContext列表上下文

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

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