Forum


14:02

12/01/2010

I have a cell which I'd like to format based on the value of the contents. You know, the usual thing: make the background red if the text has a certain value. It seems to me that the obvious approach is something like this:
jQuery.extend(jQuery.fn.fmatter , {
myFormatter : function(cellvalue, options, rowdata) {
jQuery("#my_list").setCell(rowdata.rowNum, 'colName', '', 'redcss');
return cellvalue;
}
});
Is this approach doomed to failure? Is there a way of getting the current row number out of the rowdata? I could hardcode the column name, but that would be nice too.
Any ideas?
Thanks
Ari
18:41

Moderators
30/10/2007

Hello,
This will not work written this way. Also in the formatter do not use jQuery commands that are related to grid contents)
One possible solutionis to use afterInsertRow event - it will work with yor code
What you mean with row number?
Regards
Tony
For professional UI suites for Java Script and PHP visit us at our commercial products site - guriddo.net - by the very same guys that created jqGrid.
22:03

28/12/2009

This post has been made many times and with a little forum searching you could find answer.
your js file.js
...
colModel: [
...
{ name: 'Items', index: 'item_count', width: 80, align: "center", searchoptions: { sopt: ['eq', 'ne', 'lt', 'le', 'gt', 'ge'] }, formatter: highlight },
...
function highlight (cellvalue, options, rowObject)
{
var color = (parseInt(cellvalue) < 10000) ? "red" : "green";
var cellHtml = "<span style='color:" + color + "' originalValue='" + cellvalue + "'>" + cellvalue + "</span>";
return cellHtml;
}
http://gyazo.com/f6958571ed5ac5102327e0608d35ab83.png
This screenshot shows it in action, it will only change the color for cells with values under 100000 in red all others in green
02:47

12/01/2010

@newperson
Thanks for your help and example. I did Google this for hours, and found only something similar to your idea on stackoverflow.com. Your example works well for certain things, but it doesn't do what I want which is to apply css to the tr or the td, rather than just inserting html. And it also doesn't work for setting the background colour.
@tony
afterInsertRow might be a better approach and I'll explore that. Thanks. You might like to add newperson's example to your docs since I'm sure that will help a lot of people (just not what I want to do here). Ideally, I wanted to apply a style to the tr so that certain rows are bold or have a different background, depending on the value of one column. So afterInsertRow may do the trick.
Most Users Ever Online: 715
Currently Online:
62 Guest(s)
Top Posters:
OlegK: 1255
markw65: 179
kobruleht: 144
phicarre: 132
YamilBracho: 124
Renso: 118
Member Stats:
Guest Posters: 447
Members: 11373
Moderators: 2
Admins: 1
Forum Stats:
Groups: 1
Forums: 8
Topics: 10592
Posts: 31289
Newest Members:
, razia, Prankie, psky, praveen neelam, greg.valainis@pa-tech.comModerators: tony: 7721, Rumen[Trirand]: 81
Administrators: admin: 66