Add an option to copy cell content through the context menu

Customizing the context menu

by Flexmonster Pivot Table

HTML

<script src="https://cdn.flexmonster.com/flexmonster.js"></script>

<p>
  Select the needed cells on the grid and copy them to the clipboard by choosing
  the Copy option in the context menu.
</p>

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

CSS

@import url(https://cdn.flexmonster.com/assets/jsfiddle-styles.css);

JavaScript

let pivot = new Flexmonster({
  container: "pivotContainer",
  componentFolder: "https://cdn.flexmonster.com/",
  toolbar: true,
  report: {
    dataSource: {
      filename: "data/data.csv"
    }
  }
});

pivot.customizeContextMenu(function(items, data, viewType) {
  items.push({
    label: "Copy",
    handler: () => {
      let content = "";
      if (Array.isArray(data)) {
        let prev;
        for (let cell of data) {
          if (prev) {
            if (cell.columnIndex > prev.columnIndex) {
              content += "\t" + cell.label;
            }
            if (cell.rowIndex > prev.rowIndex) {
              content += "\n" + cell.label;
            }
            prev = cell;
          } else {
            content += cell.label;
            prev = cell;
          }
        }
      } else {
        content += data.label;
      }
      copy(content);
    }
  });
  return items;
});

function copy(content) {
  const textarea = document.createElement("textarea");
  textarea.innerHTML = content;
  document.body.appendChild(textarea);
  textarea.select();
  document.execCommand("copy");
  document.body.removeChild(textarea);
}