JSFiddle - React, Tailwind, and code Playground

by Flexmonster Pivot Table

HTML

<link rel="stylesheet" href="https://s3.amazonaws.com/flexmonster/2.3/demo.css">
<script src="https://cdn.flexmonster.com/flexmonster.js"></script>

<div>
  <h2>How to export collapsed data on the grid</h2>
  <p>
    To include all data in the export, we suggest using the following approach:
  </p>
  <ol class="styledList">
    <li>Copy the current report configuration (line 37 in the JavaScript box). We are going to restore it later.</li>
    <li>Subscribe to the <a href="https://www.flexmonster.com/api/aftergriddraw/" target="_blank">aftergriddraw</a> event (lines 40-47).</li>
    <li>Expand all data on the grid (line 49). It is needed to temporarily display all collapsed data.</li>
    <li>Once we expand the data on the grid, the <code>aftergriddraw</code> event is triggered. In its handler, we export the report and restore the previously saved report configuration (lines 42-46).</li>
  </ol>
  <p>
    <a href="https://www.flexmonster.com/doc/export-and-print/" target="_blank">Visit our documentation to learn more about export in Flexmonster</a>.
  </p>
</div>

<button onclick="exportFullGrid()">Export Entire Grid</button>

<div id="pivot-container"></div>

CSS

h2 {
  text-align: center;
}

p {
  font-size: 15px;
  line-height: 1.5;
  padding-right: 10px;
  padding-left: 10px;
}

a {
  color: #00a45a;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

code {
    font-size: 105%;
    color: #DF3800;
    background-color: #f5f5f5;
    padding: 1px 4px;
    font-family: monospace !important;
    border-radius: 4px;
}

ol.styledList {
    list-style: none;
    counter-reset: item;
    font-size: 15px;
    line-height: 1.5;
    padding-left: 25px;
}

ol.styledList > li::before {
    content: counter(item);
    counter-increment: item;
    display: block;
    font-weight: bold;
    color: #df3800;
    position: absolute;
    margin: 0;
    margin-left: 9px;
    left: 0;
}

JavaScript

let pivot = new Flexmonster({
  container: "#pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  toolbar: true,
  report: {
    dataSource: {
      filename: "data/data.csv"
    },
    slice: {
      rows: [{
          uniqueName: "Category",
          filter: {
            members: [
              "category.[accessories]"
            ]
          }
        },
        {
          uniqueName: "Color"
        }
      ],
      columns: [{
        uniqueName: "[Measures]"
      }],
      measures: [{
        uniqueName: "Price",
        aggregation: "sum"
      }],
      expands: {
        rows: [{
          tuple: [
            "category.[cars]"
          ]
        }]
      }
    }
  }
});

function exportFullGrid() {
  // Before the export starts, save the report settings:
  const reportCopy = pivot.getReport();
  // Remove all filters for the export:
  removeAllFilters();
  pivot.on("aftergriddraw", () => {
    pivot.off("aftergriddraw");
    pivot.exportTo('excel', null, () => {
      // Export is complete
      // Now we can restore the previous report settings (with the filters):
      pivot.setReport(reportCopy);
    });
  });
  // Expand the data to include all data in the export:
  pivot.expandAllData();
}

function removeAllFilters() {
  // Get all selected fields:
  let selectedRows = pivot.getRows();
  let selectedColumns = pivot.getColumns();
  let selectedMeasures = pivot.getMeasures();
  let selectedReportFilters = pivot.getReportFilters();

  let allSelectedFields = [...selectedReportFilters, ...selectedMeasures, ...selectedColumns, ...selectedRows];

  // Remove all filters for the export:
  for (let i = 0; i < allSelectedFields.length; i++) {
    flexmonster.clearFilter(allSelectedFields[i].uniqueName);
  }
}