简体   繁体   English

将过滤器发送到 react-admin 列表中的导出器

[英]Send filters to exporter within list on react-admin

I am trying to build my own exporter function but I need to call a different dataProvider.我正在尝试构建自己的导出器函数,但我需要调用不同的 dataProvider。 I don't want to use the fetchRelatedRecords as there could be thousands of ids and I would hit the database way too many times.我不想使用 fetchRelatedRecords,因为可能有数千个 ID,而且我会多次访问数据库。 I am trying to send the filters from my list component to the exporter but I have no idea how to do it.我正在尝试将过滤器从我的列表组件发送到导出器,但我不知道该怎么做。 What could be a possible way?什么可能是一种可能的方式?

        <List
            aside={<OrdersAside kpis={kpis} />}
            bulkActionButtons={false}
            filters={<OrdersFilter />}
            sort={{ field: 'pickup_at', order: 'DESC' }}
            filterDefaultValues={{
                pickup_at__gt: pickupAtGt,
            }}
            exporter={exporter}
            {...props}
        >

Many thanks非常感谢

The exporter function doesn't have access to the filter, as it's already applied to query the list of records that the exporter receives as first parameter.导出器函数无权访问过滤器,因为它已经应用于查询导出器作为第一个参数接收的记录列表。

If you need to do custom queries within the exporter, you should know that the exporter function receives the dataProvider as first argument.如果您需要在导出器中进行自定义查询,您应该知道导出器函数接收 dataProvider 作为第一个参数。

And if you can't do exactly what you want with the <ExportButton> , then replace it with your own component!如果你不能用<ExportButton>做你想做的事,那么用你自己的组件替换它! The implementation isn't super complex:实现不是超级复杂:

import * as React from 'react';
import { useCallback, FunctionComponent } from 'react';
import PropTypes from 'prop-types';
import DownloadIcon from '@material-ui/icons/GetApp';
import {
    Button,
    fetchRelatedRecords,
    useDataProvider,
    useNotify,
    useListContext,
    SortPayload,
    Exporter,
    FilterPayload,
} from 'react-admin';

const ExportButton = props => {
    const {
        maxResults = 1000,
        onClick,
        label = 'ra.action.export',
        icon = defaultIcon,
        exporter: customExporter,
        ...rest
    } = props;
    const {
        filterValues,
        resource,
        currentSort,
        exporter: exporterFromContext,
        total,
    } = useListContext(props);
    const exporter = customExporter || exporterFromContext;
    const dataProvider = useDataProvider();
    const notify = useNotify();
    const handleClick = useCallback(
        event => {
            dataProvider
                .getList(resource, {
                    sort: currentSort,
                    filter: filterValues,
                    pagination: { page: 1, perPage: maxResults },
                })
                .then(
                    ({ data }) =>
                        // here, do what you want with the data
                        // ...
                        // the default implementation is:
                        exporter &&
                        exporter(
                            data,
                            fetchRelatedRecords(dataProvider),
                            dataProvider,
                            resource
                        )
                )
                .catch(error => {
                    console.error(error);
                    notify('ra.notification.http_error', 'warning');
                });
            if (typeof onClick === 'function') {
                onClick(event);
            }
        },
        [
            currentSort,
            dataProvider,
            exporter,
            filterValues,
            maxResults,
            notify,
            onClick,
            resource,
            sort,
        ]
    );

    return (
        <Button
            onClick={handleClick}
            label={label}
            disabled={total === 0}
            {...sanitizeRestProps(rest)}
        >
            {icon}
        </Button>
    );
};

const defaultIcon = <DownloadIcon />;

const sanitizeRestProps = ({
    basePath,
    filterValues,
    resource,
    ...rest
}) =>
    rest;

ExportButton.propTypes = {
    basePath: PropTypes.string,
    exporter: PropTypes.func,
    filterValues: PropTypes.object,
    label: PropTypes.string,
    maxResults: PropTypes.number,
    resource: PropTypes.string,
    sort: PropTypes.exact({
        field: PropTypes.string,
        order: PropTypes.string,
    }),
    icon: PropTypes.element,
};

export default ExportButton;

你需要在 CustomListActions 中创建 react-admin <ExportButton filter={{ ...filterValues, ...permanentFilter }} /> 你可以从 props o CustomListActions 访问 filterVaues 和 PermanentFilter 对象

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

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