JavaScript PivotGrid - cell values alignment and number settings.
A live JavaScript/jQuery example of jqxPivotGrid, the jQWidgets PivotGrid component, with its complete source code below. It is one of 12 jqxPivotGrid demos for jQuery.
source property – Gets or sets pivot source adapter used to supply data to the pivot grid.multipleSelectionEnabled property – Gets or sets whether the multiple selection in the pivot grid is enabled or disabled.treeStyleRows property – Gets or sets the rows of the pivot grid are displayed as a tree structure or using classic OLAP style.autoResize property – Gets or sets if the size of pivot grid adjusts automatically to display the entire content.To run it in your own page, load jqx.base.css, a theme and the library files below from npm (npm install jqwidgets-scripts) or the jsDelivr CDN, then add the markup and the script. jQuery is not required: jqxcore.js provides $.
The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxbuttons.js, jqxscrollbar.js, jqxmenu.js, jqxpivot.js, jqxpivotgrid.js.
It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers).
$(document).ready(function () {
// prepare sample data
var data = new Array();
var countries =
[
"Germany", "France", "United States", "Italy", "Spain", "Finland", "Canada", "Japan", "Brazil", "United Kingdom", "China", "India", "South Korea", "Romania", "Greece"
];
var dataPoints =
[
"2.25", "1.5", "3.0", "3.3", "4.5", "3.6", "3.8", "2.5", "5.0", "1.75", "3.25", "4.0"
];
for (var i = 0; i < countries.length * 2; i++) {
var row = {};
var value = parseFloat(dataPoints[Math.round((Math.random() * 100)) % dataPoints.length]);
row["country"] = countries[i % countries.length];
row["value"] = value;
data[i] = row;
}
// create a data source and data adapter
var source =
{
localdata: data,
datatype: "array",
datafields:
[
{ name: 'country', type: 'string' },
{ name: 'value', type: 'number' }
]
};
var dataAdapter = new $.jqx.dataAdapter(source);
dataAdapter.dataBind();
// create a pivot data source from the dataAdapter
var pivotDataSource = new $.jqx.pivot(
dataAdapter,
{
pivotValuesOnRows: false,
rows: [{ dataField: 'country', width: 190 }],
columns: [],
values: [
{ dataField: 'value', width: 200, 'function': 'min', text: 'cells left alignment', formatSettings: { align: 'left', prefix: '', decimalPlaces: 2 } },
{ dataField: 'value', width: 200, 'function': 'max', text: 'cells center alignment', formatSettings: { align: 'center', prefix: '', decimalPlaces: 2 } },
{ dataField: 'value', width: 200, 'function': 'average', text: 'cells right alignment', formatSettings: { align: 'right', prefix: '', decimalPlaces: 2 } }
]
});
// create a pivot grid
$('#divPivotGrid').jqxPivotGrid(
{
source: pivotDataSource,
treeStyleRows: true,
autoResize: false,
multipleSelectionEnabled: true
});
});
<div id="divPivotGrid" style="height: 400px; width: 800px; background-color: white;">
</div>