I can change the text color by doing this in jqgrid custom formatter:
function YNFormatter(cellvalue, options, rowObject)
{
var color = (cellvalue == "Y") ? "green" : "red";
var cellHtml = "<span style='color:" + color + "' originalValue='" +
cellvalue + "'>" + cellvalue + "</span>";
return cellHtml;
}
but I want to now change the background color of the whole cell (instead of the text color).
Is this possible?
Here is what I did:
Here
Actually you won't even need custom formatter, if you just intend to do it for setting colors. You can even set color value from here like,
Happy Coding.
If you want use
<span>
element inside of the custom cell formatter you can return from the custom formatterwhere the style of
span.cellWithoutBackground
you can define for example like followingHow it works you can see live here:
UPDATED: The answer is old. The best practice would be to use
cellattr
callback incolModel
instead of the usage custom formatters. Changing of background color of the cell is in general just assigningstyle
orclass
attribute to the cells of the column (<td>
elements). Thecellattr
callback defined in the column ofcolModel
allows exactly to do this. One can still use predefined formatters likeformatter: "checkbox"
,formatter: "currency"
,formatter: "date"
and so on, but still change the background color in the column. In the same way therowattr
callback, which can be defined as the jqGrid option (outside of specific column ofcolModel
), allows to assign style/class of the whole row (<tr>
elements).More information about
cellattr
can be found here and here, for example. Another answer explainsrowattr
.