JSFiddle - React, Tailwind, and code Playground
by Flexmonster Pivot Table
May 20, 2022
HTML
<link rel="stylesheet" href="https://s3.amazonaws.com/flexmonster/2.3/demo.css">
<script src="https://cdn.flexmonster.com/flexmonster.js"></script>
<div>
<h2>Flexmonster CRUD</h2>
<p>
You can add buttons in cells via the <a href="https://www.flexmonster.com/api/customizecell/" target=_blank>customizeCell</a> function. For identifying which record you want to delete or update, pass the <code>product_id</code> value to delete/update handlers. The button for creating a new row can be placed outside the grid.
</p>
</div>
<button class="top-button" onclick="openCreateForm()">
Create New
</button>
<div id="pivot-container"></div>
CSS
h2 {
padding-left: 10px;
}
p {
font-size: 15px;
line-height: 1.5;
padding-right: 10px;
padding-left: 10px;
}
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;
}
.crud-buttons {
margin-left: 5px !important;
border: 1px solid orangered !important;
}
.top-button {
margin-left: 10px;
}
JavaScript
var pivot = new Flexmonster({
container: "#pivot-container",
componentFolder: "https://cdn.flexmonster.com/",
customizeCell: customizeCell,
report: {
dataSource: {
data: getData(),
},
slice: {
rows: [{
uniqueName: "name"
},
{
uniqueName: "type"
}
],
columns: [{
uniqueName: "[Measures]"
}],
measures: [{
uniqueName: "product_id",
aggregation: "sum"
},
{
uniqueName: "weight_kg",
aggregation: "sum"
},
{
uniqueName: "price",
aggregation: "sum",
format: "-n9xsls70voa00"
}
],
flatOrder: [
"product_id",
"name",
"type",
"weight_kg",
"price"
]
},
options: {
grid: {
type: "flat",
showGrandTotals: "off",
}
},
formats: [{
name: "",
textAlign: "left"
},
{
name: "-n9xsls70voa00",
thousandsSeparator: "",
currencySymbol: "$"
}
]
},
width: "100%",
height: 550
});
function customizeCell(cell, data) {
if (data.measure.uniqueName === "product_id" && data.rowIndex !== 0) {
cell.style["z-index"] = 2; //z-index is required to make buttons clickable
cell.text = `${data.value} <button onclick="deleteRow(${data.value})" class="crud-buttons">Delete</button> <button onclick="updateRow(${data.value})" class="crud-buttons">Update</button>`
}
}
function deleteRow(id) {
alert("Delete product, product_id: " + id);
// Here you can update your data set accordingly
// Then provide the updated data set to Flexmonster using the updateData() API call: https://www.flexmonster.com/api/updatedata/
}
function updateRow(id) {
alert("Update product, product_id: " + id);
// Here you can update your data set accordingly
// Then provide the updated data set to Flexmonster using the updateData() API...