jqGrid custom format fails on addClass
Asked Answered
G

1

0

I populate a new grid from json with custom formatter the formatter is defined :

testFormatter(value,el,opts)
{
     if (value==0)
     {
          $(el).addClass("Fail");
     }
     …
}

I am expecting the cells to use the css class but If I check the cells they don't add that class.

Gaughan answered 18/5, 2011 at 17:5 Comment(0)
P
6

You made the typical error of the usage of the custom formatter. It is important to understand that the jqGrid has the best performance if the grid contain will be created as the string. In the case the gridview:true gives you the performance. Any custom formatter should work in the gridview:true mode, so the custom formater has no parameter which are DOM element and so you can not use operations like $(el).addClass("Fail");

In some old answers (see here and here) you can find how the problem can be solved, but I would you suggest to use new feature of jqGrid 4.0.0: cellattr option. For undefrstanding: the purpose of the custom formatter is not add some HTML attributes like class for example. It should be used for example to convert some universal date format like yyyy-mm-dd to localized form like dd.mm.yyyy (German style). If you don't want to change format of the column, but want only add some attributes like title (used for tooltips), class (like in your case), style and so on the new cellattr option is what you need.

In you case you can define

cellattr: function(rowId, cellValue, rawObject, cm, rdata) {
    if (cellValue==0) {
        return ' class="Fail"';
    }
}

See a small demo here:

enter image description here

In the demo I added calsses ui-state-error and ui-state-error-text to all cells of 'Client' column where in the 'Closed' the checkbox is set.

Puett answered 18/5, 2011 at 17:47 Comment(2)
thanks for the answer, It seems I can't find documentation of cellattr in jqgrid site, just that they added it.Gaughan
@shevski: there are many intresting new options which are not yet described in the documenation. The column templates for example are introduced in jqGrid 3.8.2, but still not described in the documentation (see here). If you search in the trirand forum for cellattr you will find some information.Puett

© 2022 - 2024 — McMap. All rights reserved.