React-CSV导出不完整数据的CSV链接

发布于 2025-02-07 14:10:19 字数 605 浏览 1 评论 0原文

我正在使用React-CSV的CSV链接来在具有可选行的表中导出CSV数据。数据导出的方式存在问题。例如,当我选择3行时,它将从输出的结果CSV文件中省略最后一行。如果我选择4行,它将仅第一次导出3行,依此类推。如果我第二次点击“导出”按钮,则完整的行将导出。

这是我将数据传递到CSV链接组件的方式:

<CSVLink
 data={getExportTransactionRows(t.transactions)}
 headers={getExportTransactionHeaders(t)}
 filename={t.companyName}
>

这是getExportTransactionRows函数,返回行:

  const getExportTransactionRows = (transactions) => {
    let trans = transactions.filter((t) => t.selected === true);
    if (trans?.length === 0) trans = transactions;
    return trans;
  };

I'm using CSV Link from react-csv to export CSV data in a table with selectable rows. There is a problem in the way the data is getting exported. When I select 3 rows, for example, it's omitting the last row from the resulting CSV file that is outputted. If I select 4 rows, it will only export 3 rows the first time, and so on. If I hit the export button a second time, the complete set of rows will get exported.

Here is how I'm passing data to the CSV Link component:

<CSVLink
 data={getExportTransactionRows(t.transactions)}
 headers={getExportTransactionHeaders(t)}
 filename={t.companyName}
>

Here is the getExportTransactionRows function that returns the rows:

  const getExportTransactionRows = (transactions) => {
    let trans = transactions.filter((t) => t.selected === true);
    if (trans?.length === 0) trans = transactions;
    return trans;
  };

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深已缘浅 2025-02-14 14:10:19

我认为您只是继续导出上一行的原因是,当您选择行时,您的getExportTransactionRows(T.transactions)s不会立即更改值。您可以声明一个保存导出数据的变量。然后在您选择的事件中,然后在选择时更改该变量。将其传递到CSVLINK

I think the reason why you just keep exporting the previous row is that your getExportTransactionRows(t.transactions)s don't change value right away when you selected row. You can declare a variable that saves your export data. Then in your select event and change that variable when you selected. Passing it into CSVLink

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文