0

我正在使用 Jqgrid 。我想根据列值更改行颜色。我可以根据此列值更改行的类。但我需要的是使用从服务器获得的颜色值更改字体颜色。如何做到这一点?

4

3 回答 3

1

或者你也可以试试这个,在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');
}
}

代码未经测试,但应该可以工作。

于 2012-08-10T15:22:41.563 回答
1

您可以通过使用列自定义格式化程序来做到这一点。

格式化程序将是您使用以下格式编写的 javascript 函数:

function myformatter ( cellvalue, options, rowObject )
{
     // format the cellvalue to new format
     return new_formated_cellvalue;
}

网格将路由这些值的位置:

  • cellvalue - 是要格式化的值。
  • options - 是一个包含有关单元格/行的信息的对象。
  • rowObject - 是由您的网格数据类型选项确定的格式的行数据。

因此,在您的自定义格式化程序中,您可以获取单元格值并对其应用类或内联字体样式,如下所示:

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},
      ...
   ]
于 2012-08-10T15:07:58.053 回答
0

在 colModel 中使用 cellattr 属性

cellattr: function (rowId, value, rowObject, colModel, arrData){
return 'style=background-color:black;'; }
于 2013-06-04T22:38:20.497 回答