Description

This pivot grid example demonstrates how to change the alignment of the values displayed in the pivot grid cells. The default behavior is to align the values to the right side of the grid cells. To change the default cells alignment, use the formatSettings align property during the definition of the value fields of the pivot grid. In addition to cells content alignment, the formatSettings object allows you to specify how to format the numbers displayed in the pivot grid cells. For example, you may change the number of digits after the decimal point, the thousands separator, whether negative numbers will be displayed in brackets or with a minus sign, as well as custom prefix and sufix text.

jQuery PivotGrid: Pivot grid cell values alignment

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.

jqxPivotGrid API used in this demo

Source code

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).

script.js

$(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
    });
});

body.html

<div id="divPivotGrid" style="height: 400px; width: 800px; background-color: white;">
</div>

Learn more

More jQuery PivotGrid demos