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>
<button onclick="moveToRows()">Move Measures To Rows</button>
<div id="pivot-container"></div>
JavaScript
var pivot = new Flexmonster({
container: "#pivot-container",
componentFolder: "https://cdn.flexmonster.com/",
toolbar: true,
report: {
dataSource: {
type: "json",
data: getData()
},
slice: {
rows: [{
uniqueName: "Country"
}, {
uniqueName: "Category"
}],
columns: [{
uniqueName: "Color"
}, {
uniqueName: "[Measures]"
}],
measures: [{
uniqueName: "Price"
}, {
uniqueName: "Quantity"
}],
},
options: {
configuratorActive: false
}
},
width: "100%",
height: 500
});
function moveToRows() {
// get the current list of columns
let columns = pivot.getColumns();
// get index of object with uniqueName: "[Measures]"
let removeIndex = columns.map(function(item) {
return item.uniqueName;
}).indexOf("[Measures]");
if (removeIndex > -1) {
// get the current report object
let report = pivot.getReport();
// remove "[Measures]" from columns
report.slice.columns.splice(removeIndex, 1);
// add "[Measures]" to rows
report.slice.rows.push({
uniqueName: "[Measures]"
});
// apply the changes
pivot.setReport(report);
}
}
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":...