操纵创建按钮路径

Manipulate create button path

我想根据显示在同一页面(也显示在地址栏)的过滤器参数来操作显示在列表页面上的创建按钮。单击 crete 按钮后,必须将用户重定向到创建页面,其中一个参数已根据过滤器填写。

我已经阅读了文档,它展示了如何在每行选项中执行此操作(https://marmelab.com/react-admin/doc/2.9/CreateEdit.html#prefilling-a-create-record), but I want to do this globaly, so that the global "create" button, correctly redirects the user to a pre-filled form (like using the adress http://localhost/comments/create?post_id=123 显示在文档中)。是否可以基于此更改创建按钮的行为?

谁能帮帮我?

我根据文档找到了解决方法:

const CustomActions = ({
    currentSort,
    className,
    resource,
    filters,
    displayedFilters,
    exporter, // you can hide ExportButton if exporter = (null || false)
    filterValues,
    permanentFilter,
    hasCreate, // you can hide CreateButton if hasCreate = false
    basePath,
    selectedIds,
    onUnselectItems,
    showFilter,
    maxResults,
    total,
    ...rest
}) => {

    var currentHref = window.location.href;
    var myRegexp = /filter={%22id%22:%22(\d+)%22}/;
    var regex = currentHref.match(myRegexp); //just got my filtered ID....
    var url = "";
    if (regex) {
        var id = regex[1];
        url = '"id":"' + id + '"';
    }
    return (
        <TopToolbar className={className} {...sanitizeListRestProps(rest)}>
            {filters && cloneElement(filters, {
                resource,
                showFilter,
                displayedFilters,
                filterValues,
                context: 'button',
            })}

            {/* Add your custom actions */}
            <Button color="primary" size="small" label="" href={`${basePath}/create?source={${url}}`}>
                <AddIcon /> New
            </Button>
        </TopToolbar>
    )

};

<List {...props} actions={<CustomActions />} >