JSFiddle - React, Tailwind, and code Playground
by Flexmonster Pivot Table
HTML
<link rel="stylesheet" href="https://cdn.flexmonster.com/2.3/demo.css">
<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<button id="btn" onclick="addRecord()">Add a new record</button>
<button id="btn" onclick="editRecords()">Edit records</button>
<button id="btn" onclick="deleteRecord()">Delete one record</button>
<button id="btn" onclick="addField()">Add a new field</button>
<div id="pivot-container"></div>
JavaScript
var pivot = new Flexmonster({
container: "pivot-container",
componentFolder: "https://cdn.flexmonster.com/",
width: "100%",
height: 430,
toolbar: true,
report: {
dataSource: {
data: getData()
},
options: {
editing: true
}
}
});
function addRecord() {
var dataForUpdate = [{
"Category": "Cars",
"Price": 51844,
"RowId": 4
}]
pivot.updateData({data: dataForUpdate}, {partial: true});
}
function deleteRecord() {
var dataForUpdate = [{
"RowId": 3,
"DeleteRow": true
}]
pivot.updateData({data: dataForUpdate}, {partial: true});
}
function editRecords() {
var dataForUpdate = [{
"Category": "Accessories",
"Price": 400,
"RowId": 1
},
{
"Category": "Bikes",
"Price": 3000,
"RowId": 2
}]
flexmonster.updateData({data: dataForUpdate}, {partial: true});
}
function addField(){
// 1. Get new column name:
var columnName = "New Data";
// 2. Get new column name data type:
var columnType = "string";
// 3. Get the current report object:
var report = pivot.getReport();
// 4. Get the data object from the report:
var currentData = report.dataSource.data;
// 5. Add the new column to the meta object of currentData:
(currentData[0])[columnName] = {"type": columnType};
// 6. Update the report:
report.dataSource.data = currentData;
pivot.setReport(report);
}
//The Data:
function getData() {
return [
{
"Category": { "type": "string" },
"Price": { "type": "number" },
"RowId": { "type": "id" },
"DeleteRow": { "type": "delete" }
},
{
"Category": "Accessories",
"Price": 242,
"RowId": 1
},
{
"Category": "Bikes",
"Price": 1487,
"RowId": 2
},
{
"Category": "Clothing",
"Price": 40,
"RowId": 3
}
]
}