反应管理员 | Bulk-Select with ReferenceField

React-Admin | Bulk-Select with ReferenceField

在我们的应用程序中,我们尝试在 ReferenceField 中使用 Datagrid 来 create/modify/delete 相关记录,如 https://marmelab.com/blog/2018/07/09/react-admin-tutorials-form-for-related-records.html

所示

除了在最近的 react-admin 更新中添加的批量操作外,本教程中显示的所有功能都运行良好。单击这些复选框可以得到

Uncaught TypeError: _this.props.onToggleItem is not a function

我认为这是因为 onToggleItem 属性通常由 List 组件提供,但是在这个应用程序中,Datagrid 没有父 List 组件。

在 ReferenceManyField 和 Datagrid 之间添加一个列表组件允许批量 select/delete 在对样式进行一些更改后工作,但这会导致另一个问题:当前显示的页面(即记录 1-10、11-20、等)按资源存储在商店中,因此可能会出现商店说我们在第 2 页并显示第 2 页的情况,该页面是空的,因为只有足够的相关项目来填充一页.

我是不是漏掉了什么?或者目前无法在 ReferenceManyField 中使用 bulk-select?

export const NetworksShow = (props) => (
  <Show title={<NetworksTitle />} actions={<NetworksShowActions />} {...props}>

        <ReferenceManyField addLabel={false} target="ipid" reference="acl-network">
          <List style={{ margin: '0px -24px -16px -24px' }} {...props} actions={<NetworkACLCardActions ipid={props.id}/>} filter={{ ipid: _.has(props, 'id') ? props.id : undefined }}>
            <Datagrid hasBulkActions={true}>
              <ReferenceField label="Network" source="ipid" reference="networks">
                <TextField source="name" />
              </ReferenceField>
              <TextField label="URL" source="url" />
              <BWChip label="Action" source="wb" />
              <EditButton />
              <DeleteButton />
            </Datagrid>
          </List>
        </ReferenceManyField>
  </Show>
);

正如我从 documentation 了解到的那样,Datagrid 只是一个迭代器 "dumb component".
它只是 "shows" 父元素 - 通常是 List (connected component) 或在你的情况下 ReferenceManyField - 元素先前已获取的东西。 因此,我认为 BulkActions 只有在由 List 元素提供时才能发挥作用。

对于问题的第二部分,列表应该在顶层使用,而不是在其他元素中使用,这就是它破坏分页的原因。

作为 https://github.com/marmelab/react-admin/pull/2365 的副作用,现在可以按照问题中描述的方式使用 ReferenceManyField -> List -> Datagrid。

例如,我们现在正在执行以下操作:

      <ReferenceManyField addLabel={false} target="groupid" reference="users">
        <List
          style={{ margin: '0px -24px -16px -24px' }}
          filter={{ groupid: id }}
          {...props}
        >
          <Datagrid hasBulkActions>
            <LinkField label="Name" source="name" />
            <LinkField label="Username" source="email" />
            <FlexibleBooleanField label="Email" source="allowemail" />
            <ACLChip label="User Access" source="aclid" />
          </Datagrid>
        </List>
      </ReferenceManyField>

批量操作适用于上述内容,并且可以避免任何分页问题,​​因为如果当前页面上没有任何内容,react-admin 现在会检查并修改分页。

我实现了 "DumbList",它从父组件获取数据而不是自己加载数据。这解决了问题:

import React from 'react';
import { ListController } from 'ra-core';
import { ListView } from 'ra-ui-materialui/esm/list/List';

export const DumbList = props =>
  <ListController {...props}>
    {controllerProps => {
      let { data } = props
      const ids = Object.keys(data || {})
      const total = ids.length
      return <ListView
        {...props}
        // This is important, otherwise bulkActionsToolbar ends up on very top of the page
        classes={{ card: 'relative' }}
        {...Object.assign(controllerProps, { data, ids, total })} />
    }}
  </ListController>