首页 > 解决方案 > React admin导入按钮referenceManyField?

问题描述

我想为我的反应管理应用程序添加一个导入按钮。

此导入将创建与另一条记录相关的记录。例如,我有一份属于一家公司的合同清单。

我看到了这个仓库:https ://github.com/benwinding/react-admin-import-csv

我想知道referenceManyField是否有类似的东西。

标签: react-admin

解决方案


如果有人感兴趣,我为此找到了一个 hacky 解决方案。

我没有使用ReferenceManyField,而是在 Show 组件中使用了List 。

为了能够做到这一点,我必须覆盖列表的道具来指定我想要引用的资源。

我还必须将显示项目 id 作为列表组件的道具传递,并通过 id 过滤列表。

let companyId = '';

const ListActions = (props) => {
  const { className } = props;
  return (
    <TopToolbar className={className}>
       <ImportButton {...props} />
    </TopToolbar>
  );
};

export const ContractRow = ({ contractCompanyId }) => {
  companyId = contractCompanyId;
  const fakeProps: ListProps = {
    basePath: '/contracts',
    hasCreate: false,
    hasEdit: false,
    hasList: true,
    hasShow: false,
    location: { pathname: '/', search: '', hash: '', state: undefined },
    match: { path: '/', url: '/', isExact: true, params: {} },
    options: {},
    permissions: null,
    resource: 'contracts',
  };
 
  return (
    <List
      {...fakeProps}
      filter={{ _company_id: contractCompanyId }}
      actions={<ListActions />}
    >
      <Datagrid className={classes.insideTable}>
        <TextField source="name" />
      </Datagrid>
    </List>
  );
};

推荐阅读