Alignment

Left
Center
Right

jQuery Grid: Text Alignment

The set the cells and columns alignment.

A live JavaScript/jQuery example of jqxGrid, the jQWidgets Grid component, with its complete source code below. It is one of 139 jqxGrid demos for jQuery.

jqxGrid 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 $. Getting started with jqxGrid shows the setup step by step.

The page loads jqx.base.css, a theme and these library files: jqxcore.js, jqxdata.js, jqxbuttons.js, jqxscrollbar.js, jqxmenu.js, jqxradiobutton.js, jqxgrid.js, jqxgrid.selection.js.

It also loads helper scripts of this demo site, which are not part of the library: demos.js (getWidth() and other layout helpers), generatedata.js (generatedata(), random sample rows).

script.js

$(document).ready(function () {
    // prepare the data
    var data = generatedata(200);

    var source =
    {
        localdata: data,
        datafields:
        [
            { name: 'id', type: 'number'},
            { name: 'firstname', type: 'string' },
            { name: 'lastname', type: 'string' },
            { name: 'productname', type: 'string' },
            { name: 'available', type: 'bool' },
            { name: 'quantity', type: 'number' },
            { name: 'price', type: 'number' },
            { name: 'total', type: 'number' }
        ],
        datatype: "array"
    };

    var columns = [
          { text: 'First Name', datafield: 'firstname', width: 200, cellsalign: 'center', align: 'center' },
          { text: 'Last Name', datafield: 'lastname', width: 200, cellsalign: 'center', align: 'center' },
          { text: 'Product', datafield: 'productname', width: 180, cellsalign: 'center', align: 'center' },
          { text: 'Quantity', datafield: 'quantity', width: 80, cellsalign: 'center', align: 'center' },
          { text: 'Unit Price', datafield: 'price', width: 90, cellsalign: 'center', align: 'center', cellsformat: 'c2' },
          { text: 'Total', datafield: 'total', cellsalign: 'center', align: 'center', cellsformat: 'c2' }
        ];

    var dataAdapter = new $.jqx.dataAdapter(source);

    // initialize jqxGrid
    $("#grid").jqxGrid(
    {
        width: getWidth('Grid'),
        source: dataAdapter,                
        selectionmode: 'multiplecellsextended',
        columns: columns
    });

    $("#leftbutton").jqxRadioButton({  width: 55 });
    $("#centerbutton").jqxRadioButton({ checked: true,  width: 70 });
    $("#rightbutton").jqxRadioButton({  width: 70 });

    var setAlignment = function (align) {
        $("#grid").jqxGrid('beginupdate');
        for (var index = 0; index < columns.length; index++) {
            $("#grid").jqxGrid('setcolumnproperty', columns[index].datafield, 'align', align);
            $("#grid").jqxGrid('setcolumnproperty', columns[index].datafield, 'cellsalign', align);
        }
        $("#grid").jqxGrid('endupdate');
    }

    $("#leftbutton").on('checked', function () {
        setAlignment('left');
    });
    $("#centerbutton").on('checked', function () {
        setAlignment('center');
    });
    $("#rightbutton").on('checked', function () {
        setAlignment('right');
    });

});

body.html

<div id="grid">
</div>
<div>
    <h3>Alignment</h3>
    <div style="float: left;" id="leftbutton">Left</div>
    <div style="float: left;" id="centerbutton">Center</div>
    <div style="float: left;" id="rightbutton">Right</div>
</div>

Learn more

More jQuery Grid demos