react-bootstrap-table2 icon indicating copy to clipboard operation
react-bootstrap-table2 copied to clipboard

Can you export the csv after sorting the table and have the csv display the sorted table?

Open rhmmuh005 opened this issue 7 years ago • 5 comments

I want the data exported in the csv to be displayed the same way it is being displayed on the table.

For e.g. if I have sorted the table and the order changes, it should appear the same in the csv. Currently, when exporting, it takes the format as it was before sorting.

Is there a way to go around this?

Thanks.

rhmmuh005 avatar Sep 27 '19 05:09 rhmmuh005

@rhmmuh005 I will figure a way to achieve your requirement :)

AllenFang avatar Sep 29 '19 08:09 AllenFang

I am having the same issue. I would be curious to see the result.

fulin426 avatar Oct 02 '19 16:10 fulin426

hey guys, try to add exportAll: false you may fix this issue.

AllenFang avatar Oct 26 '19 07:10 AllenFang

exportAll: false-- I have used this but still not getting sorted CSV is there any way?

manisha-seclogic avatar May 24 '21 05:05 manisha-seclogic

Hi @rhmmuh005,

I found a workaround which is pretty much straightforward.

Add this under like this:

<ToolkitProvider
          keyField="id"
          data={data}
          columns={columns}
          search
          exportCSV={{
            fileName: `In-Active_Partner(s)_List_${new Date().getMonth() +
              1}/${new Date().getDate()}/${new Date().getFullYear()}.csv`,
            noAutoBOM: false,
          }}
        >
          {(props) => (
            <div>
                  <MyExportCSV {...props.csvProps} />
                <div className="table-responsive-lg">
                  <BootstrapTable
                    ref={table}
                   
                    {...props.baseProps}
                  />
                </div>
            </div>
          )}
</ToolkitProvider>

And MyExportCSV function looks like this :

const MyExportCSV = (props) => {
    const handleClick = () => {
      // passing my custom data
      if (table.current.sortContext.state.sortOrder) {
        props.onExport(table.current.paginationContext.props.data);
      } else {
        props.onExport();
      }
    };
    return (
      <ButtonIcon
        size="sm"
        icon="download"
        className="ml-1 mt-lg-0 mb-2"
        iconClassName="fs--2"
        onClick={handleClick}
      >
        .CSV
      </ButtonIcon>
    );
  };

Here, I have invoked onExport which with arguments takes that data for csv export. So, if statement will check between if it is sorted or not using table ref.

Let me know if it works for you! Peace out.

ghost avatar Sep 20 '22 18:09 ghost