【发布时间】:2012-08-10 14:30:59
【问题描述】:
我正在使用 Jqgrid 。我想根据列值更改行颜色。我可以根据此列值更改行的类。但我需要的是使用从服务器获得的颜色值更改字体颜色。如何做到这一点?
【问题讨论】:
标签: jqgrid
我正在使用 Jqgrid 。我想根据列值更改行颜色。我可以根据此列值更改行的类。但我需要的是使用从服务器获得的颜色值更改字体颜色。如何做到这一点?
【问题讨论】:
标签: jqgrid
您可以通过使用列custom formatter 来做到这一点。
格式化程序将是您使用以下格式编写的 javascript 函数:
function myformatter ( cellvalue, options, rowObject )
{
// format the cellvalue to new format
return new_formated_cellvalue;
}
网格将路由这些值的位置:
因此,在您的自定义格式化程序中,您可以获取单元格值并对其应用类或内联字体样式,如下所示:
function myformatter ( cellvalue, options, rowObject )
{
if (cellvalue == "red")
return '<font color="red">' + cellvalue + '</font>';//or use classes
else
return '<font color="blue">' + cellvalue + '</font>';//or use classes
}
然后在您的列定义中,您只需指定哪些列将使用此格式化程序,就像这样(添加到任何需要字体颜色的列):
colModel: [
...
{name:'price', index:'price', width:60, align:"center", editable: true, formatter:myformatter},
...
]
【讨论】:
或者你也可以试试这个,在loadComplete中,像这样获取jqgrid的所有行ID
var allRowsOnCurrentPage = $('#grid').jqGrid('getDataIDs');
假设您在 jqgrid 中有 name 和 company 列,并且有两行。对于一行数据是这样的
姓名:xxx 公司:yyy
第二行你有这样的数据
姓名:aaa 公司:bbb
所以你在 for 循环中得到 Name 值
for(int i=1;i<=allRowsOnCurrentPage.length;i++)
{
var Name=$('#grid').jqGrid('getCell',i,'Name');
if(Name="aaa")
{
$('#grid').jqGrid('setCell',i,"Name","",{'background-color':'yellow');
}
}
代码未经测试,但应该可以工作。
【讨论】:
在 colModel 中使用 cellattr 属性
cellattr: function (rowId, value, rowObject, colModel, arrData){
return 'style=background-color:black;'; }
【讨论】: