extended-salmon
extended-salmon2w ago

useExport with select

I want to use export when I select the elements from the checkbox.
8 Replies
kapa.ai
kapa.ai2w ago
Hi @SetiZ. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To export only the selected elements from a table using checkboxes, you need to combine row selection logic with the useExport hook. While the knowledge sources do not provide a full, ready-made example for exporting only selected rows, they do clarify that useExport is designed to export data fetched from your data provider, and you can use the mapData property to transform the data before export. Here’s how you can approach this: 1. Track Selected Rows: Use your table component’s row selection feature (e.g., DataGrid for Material UI, Table for Ant Design) to keep track of selected row IDs. 2. Filter Data for Export: When triggering export, use the mapData property of useExport to filter the data so that only selected rows are included in the export. Example Approach (Ant Design)
import { useExport } from "@refinedev/core";
import { List, useTable, ExportButton } from "@refinedev/antd";
import { Table } from "antd";
import React, { useState } from "react";

interface IPost {
id: number;
title: string;
}

const PostList: React.FC = () => {
const { tableProps } = useTable<IPost>();
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);

const { triggerExport, isLoading: exportLoading } = useExport<IPost>({
mapData: (item) => {
// Only export selected rows
if (selectedRowKeys.includes(item.id)) {
return item;
}
return null;
},
});
return (
<List
headerButtons={
<ExportButton onClick={triggerExport} loading={exportLoading} />
}
>
<Table
{...tableProps}
rowKey="id"
rowSelection={{
selectedRowKeys,
onChange: setSelectedRowKeys,
}}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
</Table>
</List>
);
};
import { useExport } from "@refinedev/core";
import { List, useTable, ExportButton } from "@refinedev/antd";
import { Table } from "antd";
import React, { useState } from "react";

interface IPost {
id: number;
title: string;
}

const PostList: React.FC = () => {
const { tableProps } = useTable<IPost>();
const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);

const { triggerExport, isLoading: exportLoading } = useExport<IPost>({
mapData: (item) => {
// Only export selected rows
if (selectedRowKeys.includes(item.id)) {
return item;
}
return null;
},
});
return (
<List
headerButtons={
<ExportButton onClick={triggerExport} loading={exportLoading} />
}
>
<Table
{...tableProps}
rowKey="id"
rowSelection={{
selectedRowKeys,
onChange: setSelectedRowKeys,
}}
>
<Table.Column dataIndex="id" title="ID" />
<Table.Column dataIndex="title" title="Title" />
</Table>
</List>
);
};
Note: In the mapData function, you filter out items that are not selected. You may need to further filter out nulls before exporting, depending on your implementation. Key Points - useExport is flexible and allows you to transform or filter data before export using mapData. - You must manage the selection state yourself and ensure only selected items are passed to the export logic. - There is no built-in refine feature that directly exports only selected rows; you need to implement the selection and filtering logic as shown above. For more on useExport and mapData, see the documentation: useExport properties and Ant Design ExportButton usage. If you need to perform bulk actions on selected rows, you may also find this related example helpful: Bulk actions with useUpdateMany (as referenced in a GitHub issue), though it is not specifically for export functionality<https://github.com/refinedev/refine/issues/6661.> If you need a more specific example for another UI framework, let me know! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2w ago
<@267689263246606336> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
extended-salmon
extended-salmonOP7d ago
@kapa.ai I seem to have an error here TypeError: Cannot convert undefined or null to object
kapa.ai
kapa.ai7d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- The error TypeError: Cannot convert undefined or null to object typically occurs when you try to use a method like Object.keys, Object.values, or similar on a value that is undefined or null. In the context of using useExport with selected rows, this can happen if your mapData function returns null or undefined for some items, and the export logic tries to process those. In the example I provided earlier, if you return null for unselected items in mapData, you must ensure to filter out these null values before exporting. The knowledge sources do not provide a built-in way in useExport to filter out null results from mapData, so you need to make sure your mapData only returns valid objects for export. How to fix: Instead of returning null for unselected items, filter your data before passing it to useExport, or ensure your mapData only processes selected items. For example, you can do:
const { triggerExport, isLoading: exportLoading } = useExport<IPost>({
mapData: (item) => item, // No filtering here
// ...other options
});

// When triggering export, only pass selected data
const handleExport = () => {
// Optionally, you can filter your data here before export
triggerExport();
};
const { triggerExport, isLoading: exportLoading } = useExport<IPost>({
mapData: (item) => item, // No filtering here
// ...other options
});

// When triggering export, only pass selected data
const handleExport = () => {
// Optionally, you can filter your data here before export
triggerExport();
};
Or, if you must filter in mapData, make sure to remove null values before the export process, as returning null can cause the error you see. Summary:
The error is likely caused by mapData returning null or undefined for some items. Ensure that mapData only returns valid objects, or filter your data before passing it to useExport to avoid this issue. If you need more details on how mapData works, see the documentation: useExport properties. If you can share the exact code for your mapData or export logic, I can help you pinpoint the issue more precisely. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7d ago
<@267689263246606336> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
extended-salmon
extended-salmonOP7d ago
@kapa.ai can I chose how my file can be named?
kapa.ai
kapa.ai7d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Yes, you can choose the filename of your exported file when using the useExport hook in refine. The useExport hook accepts a filename property, which allows you to specify a custom filename for the export file:
useExport({
filename: "my-custom-filename.csv",
});
useExport({
filename: "my-custom-filename.csv",
});
This will set the name of the downloaded file to "my-custom-filename.csv" instead of the default name. For more details, see the API reference in the documentation: useExport API Reference. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai7d ago
<@267689263246606336> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?