JSFiddle - React, Tailwind, and code Playground

Export and print

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>
<script src="https://unpkg.com/downloadjs@1.4.7"></script>


  <button onclick="exportHtml()">
    Export to HTML
  </button>


<div id="pivotContainer"></div>

CSS

h2 {
  text-align: center;
}

p, label {
  font-size: 15px;
  line-height: 1.5;
}

p {
  padding-right: 10px;
  padding-left: 10px;
}

button {
  margin-right: 5px;
}

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;
}

.styledList {
    font-size: 15px;
    line-height: 1.5;
    padding-left: 25px;
}

input {
  display: flex;
  margin-bottom: 10px;
  padding: 4px;
  border: 1px solid #d5d5d5;
}

label {
  margin-top: 16px;
  margin-bottom: 5px;
  display: flex;
}

JavaScript

const pivot = new Flexmonster({
  container: "pivotContainer",
  componentFolder: "https://cdn.flexmonster.com/",
  toolbar: true,
  report: {
    dataSource: {
      filename: "https://cdn.flexmonster.com/data/retail-data.json",
    },
    mapping: {
      "Order Date": {
        type: "year/quarter/month/day",
      },
    },
    slice: {
      rows: [
        {
          uniqueName: "Country",
        },
        {
          uniqueName: "City",
        },
        {
          uniqueName: "Contact Last Name",
        },
        {
          uniqueName: "Contact First Name",
        },
        {
          uniqueName: "[Measures]",
        },
      ],
      columns: [
        {
          uniqueName: "Status",
        },
        {
          uniqueName: "Order Date",
        },
      ],
      measures: [
        {
          uniqueName: "Price",
          aggregation: "sum",
        },
        {
          uniqueName: "Quantity",
          aggregation: "sum",
        },
      ],
      expands: {
        columns: [
          {
            tuple: ["status.[cancelled]"],
          },
        ],
      },
    
    },
  },
})

// HTML export
function exportHtml() {
  pivot.exportTo(
    "html",
    {
      destinationType: "plain",
    },
    (html) => {
      const parser = new DOMParser()
      const doc = parser.parseFromString(html.data, "text/html")

      const table = doc.querySelector("table.grid")
      const rows = table.querySelectorAll("tr")

      let theadEndIndex = 1
      rows.forEach((tr, i) => {
        if (tr.querySelector(".columnHeaderCell")) {
          theadEndIndex = i
        }
      })

      const thead = doc.createElement("thead")
      for (let i = 0; i <= theadEndIndex; i++) {
        thead.appendChild(rows[i])
      }

      table.insertBefore(thead, table.firstElementChild)

      let htmlData = doc.documentElement.outerHTML
      download(htmlData, "pivot.html", "text/html")
    },
  )
}