Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export data to CSV in server pagination / sorting / filtering mode

I am trying to export the ag-grid data to CSV.

The issue is, it exports only the visible data OR the in-memory data received from HTTP call while considering paginationPageSize, maxBlocksInCache, cacheBlockSize etc. in the grid. Not the entire data-set.

I went through below links, but couldn't get much help.

  1. [export] Export to CSV all pages in Client side Pagination
  2. agGrid data export

Is there any way we can achieve this? Or this is altogether not possible?

like image 681
Paritosh Avatar asked Dec 28 '17 11:12

Paritosh


1 Answers

This is how i solved this -

  1. fetch all rows you need from your data source
  2. clone gridapi object
  3. grab the serverside cache from the cloned gridapi
  4. process it so its filled with your feched data
  5. run export to excel method on the cloned gridapi
  6. ...
  7. PROFIT

const gapi = cloneDeep(this.gridApi); // clone gridApi

const blocks = gapi['serverSideRowModel'].rootNode.childrenCache.blocks; // object notation to suppress private warning/err

  // swap rows cache with fetched data
  for (let i = 0, j = 0; i < Math.ceil(results.length/this.paginationPageSize); i++) {
    // we alter relevant block, or if it is not loaded yet we clone 1st one and alter it
    const block = blocks[i] || cloneDeep(blocks[0]);  
    block.rowNodes.forEach(n => n.data = results[j++]);
    blocks[i] = block;
  }
  gapi['serverSideRowModel'].rootNode.childrenCache.blocks = blocks;

  gapi.exportDataAsExcel(params);
like image 57
Sergey Sob Avatar answered Oct 14 '22 17:10

Sergey Sob