jqgrid: How to set cell specific css class at generation time

I ind your question interesting so I made the demo for you.

enter image description here

If you want to set some custom attributes on the grid cells (<td> elements) like class, title, colspan, style the cellattr is the best way to do this (see here for details). cellattr are close to custom formatter feature, but allows to define attributes of the cell and not the cell contain.

In the demo I used the following XML input:

<?xml version='1.0' encoding='utf-8'?>
<rows>
<page>1</page><total>1</total><records>2</records>
    <row id='13'>
        <cell>1.00</cell>
        <cell class="ui-state-error">15.00</cell>
        <cell>9.00</cell>
    </row>
    <row id='12'>
        <cell>1.00</cell>
        <cell>2.00</cell>
        <cell class="ui-state-highlight">1.15</cell>
    </row>
</rows>

and the cellattr like the following

cellattr: function () {
    var c = $('cell:eq(1)', arguments[2]).attr('class');
    return c ? " class="" + c + """: "";
}

In the case the ‘class’ attribute of the second (‘:eq(1)’) cell will be used for the formatting.

From the design point of view I would recommend you don’t use the class names directly as the attributes. An alternate attribute like format="error" which will be converted as class="ui-state-error" have some advantages. It could make separation of information like formatting tips from direct HTML instruction.

Leave a Comment