JSFiddle - React, Tailwind, and code Playground

by Ravi A

HTML

<link rel="stylesheet" href="https://s3.amazonaws.com/flexmonster/2.3/demo.css">
<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<button onclick="openCalculatedValueEditor()">Open Calculated Value Editor</button>
<div id="pivot-container"></div>

JavaScript

var pivot = new Flexmonster({
  container: "#pivot-container",
  componentFolder: "https://cdn.flexmonster.com/",
  report: {
    dataSource: {
      dataSourceType: "json",
      data: getData()
    },
    slice: {
      rows: [
        {uniqueName: "Country"}, 
        {uniqueName: "Category"}
      ],
      columns: [
        {uniqueName: "Color"}, 
        {uniqueName: "[Measures]"}
      ],
      measures: [
        {uniqueName: "Price"}, 
        {uniqueName: "Quantity"}
      ],
    }
  },
  width: "100%",
  height: 370
});

function openCalculatedValueEditor() {
   pivot.openCalculatedValueEditor('', function(response){
       // 1. Get the current slice
       let slice = pivot.getReport().slice;
      alert(response)
       // 2. Find and activate the newly created calculated value
       (slice.measures).find(measure=>measure.uniqueName==response.uniqueName).active = true;
       // 3. Run query with the modified slice for the changes to take place
       pivot.runQuery(slice);
   });
}

function getData() {
  return [{
    "Category": "Accessories",
    "Size": "262 oz",
    "Color": "red",
    "Destination": "Australia",
    "Business Type": "Specialty Bike Shop",
    "Country": "Australia",
    "Price": 174,
    "Quantity": 225,
    "Discount": 23
  }, {
    "Category": "Accessories",
    "Size": "214 oz",
    "Color": "yellow",
    "Destination": "Canada",
    "Business Type": "Specialty Bike Shop",
    "Country": "Canada",
    "Price": 502,
    "Quantity": 90,
    "Discount": 17
  }, {
    "Category": "Components",
    "Size": "235 oz",
    "Color": "green",
    "Destination": "Australia",
    "Business Type": "Warehouse",
    "Country": "Australia",
    "Price": 551,
    "Quantity": 1950,
    "Discount": 51
  }, {
    "Category": "Components",
    "Size": "307 oz",
    "Color": "white",
    "Destination": "United Kingdom",
    "Business Type": "Warehouse",
    "Country": "Canada",
    "Price": 842,
    "Quantity": 8212,
    "Discount": 55
  }, {
...