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="getPage(0)">
PAGE 1
</button>
<button onclick="getPage(1)">
PAGE 2
</button>
<button id="sort" onclick="sort(1)">
SORT A-Z
</button>
<p>
<b>NOTE</b>: In this example, custom sorting works only for <code>'KEY'</code> parameter.
</p>
<div id="pivot-container"></div>
CSS
p {
font-size: 14px
}
JavaScript
const page_size = 10;
let page_index = 0;
/*
* If 0 -> no sorting applied
* If 1 -> A-Z sorting
* If -1 -> Z-A sorting
*/
let current_sorting = 0;
let pivot = new Flexmonster({
container: "pivot-container",
componentFolder: "https://cdn.flexmonster.com/",
toolbar: true,
report: {
options: {
sorting : false,
grid: {
type: "flat"
},
showGrandTotals: false
},
dataSource: {
mapping: {
id: {
type: "id"
}
},
data: getData().slice(page_index, page_size)
}
}
});
function getPage(index) {
page_index = index;
sort(current_sorting);
}
function sort(flag) {
current_sorting = flag;
if (flag === 1) {
document.getElementById("sort").innerText = "SORT Z-A"
document.getElementById("sort").setAttribute("onclick", "sort(-1)");
//Change report to a default data sorted A-Z
let report = pivot.getReport();
let data = getData().sort((a,b) => {
return (a.key).localeCompare(b.key)
})
report.dataSource.data = data.slice(page_index * page_size, (page_index + 1) * page_size);
pivot.setReport(report);
}
if (flag === -1) {
document.getElementById("sort").innerText = "DISABLE SORT"
document.getElementById("sort").setAttribute("onclick", "sort(0)");
//Change report to a default data sorted Z-A
let report = pivot.getReport();
let data = getData().sort((a,b) => {
return (b.key).localeCompare(a.key)
})
report.dataSource.data = data.slice(page_index * page_size, (page_index + 1) * page_size);
pivot.setReport(report);
}
if (flag === 0) {
document.getElementById("sort").innerText = "SORT A-Z"
document.getElementById("sort").setAttribute("onclick", "sort(1)");
//Change report to a default data unsorted
let report = pivot.getReport();
report.dataSource.data = getData().slice(page_index * page_size, (page_index + 1) * page_size);
pivot.setReport(report);
}
}
function getData() {
let...