我怎样才能从jqGrid列的单元格值为背景做条件格式 [英] How can i get the cell value from jqGrid column to do conditional formatting for backcolor

查看:186
本文介绍了我怎样才能从jqGrid列的单元格值为背景做条件格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jqGrid treegrid,我想根据单元格(它的一个整数)中的数据的值来格式化列的背面颜色:



  {
名称:'missingBooks',
cellattr:函数(rowId,tv,rawObject,cm,rdata){

//条件格式化
if(rawObject [11]> 0){
return'style =background - 颜色:#FFCCCC';


width 75,
unformat:originalValueUnFormatter,
格式化程序:missingBooksFormatter,
align:right,
index :'missingBooks',
hidden:false,
sorttype:'int',
sortable:true
},

这工作正常,但我的问题是在cellAttr回调。在这个条件格式化行中:

pre $ code if(rawObject [11]> 0){
return'style =背景色:#FFCCCC';



$ b我想重用这个逻辑,所以我不想索引到rawObject并找出我正在使用的列。我希望有办法做这样的事情:

$ p $ if(rawObject.missingBooks> 0){
return'style =background-color:#FFCCCC';
}

但是这似乎是未定义的。这样,如果我添加一个新的列,我不必重新索引所有这些条件格式化代码。

解决方案

我明白这个问题。我建议Tony在jqGrid代码中进行一些更改。大多数情况下,修改地点<在代码中填充第一个 rd ,然后在下一个for循环调用 addCell rd 作为附加参数。函数 addCell 可以将信息转发给 formatCol formatCol 可以使用附加参数 rd 来调用 cellattr ,这个参数的格式与您想要的完全相同。

然而,可以相对容易地得到几乎相同的结果,而不需要对jqGrid代码进行任何更改。要做到这一点,可以构建地图对象,它可以根据名称为我们提供 rawObject 中列的索引。



例如,我们可以使用 beforeRequest beforeProcessing 来填充地图。该代码可能看起来像

  var colMap = {}; 
$(#tree)。jqGrid({
...
colModel:[
{name:'missingBooks',
cellattr:function(rowId, tv,rawObject,cm,rdata){
//条件格式化
if(Number(rawObject [colMap.missingBooks])> 0){
return'style =background-color: #FFCCCC';
} else {
return'';
}
}
...
],
beforeRequest:function (){
if($ .isEmptyObject(colMap)){
var i,cmi,$ b $ cm = $(this).jqGrid('getGridParam','colModel'),
= cm.length;
(i = 0; i <1; i ++){
cmi = cm [i];
colMap [cmi.name] = i;
}
}
}
});

因此,代码将免于使用诸如 rawObject [11] 其中索引 11 可以在代码中修改一下。



你可以在这里看到


i am using jqGrid treegrid and i want to format the back color of columns based on the value of the data in the cell (its an integer):

Here is an example where I setup the column:

             {
                 name: 'missingBooks',
                 cellattr: function (rowId, tv, rawObject, cm, rdata) {

                 //conditional formatting
                     if (rawObject[11] > 0) {
                         return 'style="background-color:#FFCCCC"';
                     }
                 },
                 width: 75,
                 unformat: originalValueUnFormatter,
                 formatter: missingBooksFormatter,
                 align: "right",
                 index: 'missingBooks',
                 hidden: false,
                 sorttype: 'int',
                 sortable: true
             },

this works fine but my issue is in the cellAttr callback. In this conditional formatting line:

      if (rawObject[11] > 0) {
                         return 'style="background-color:#FFCCCC"';
                     }

i would like to reuse this logic so i dont want to have to index into the rawObject and figure out what column i am using. i was hoping there was a way to do something like this:

       if (rawObject.missingBooks > 0) {
                         return 'style="background-color:#FFCCCC"';
                     }

but this seems to be undefined. This way if i add a new column i dont have to reindex all of this conditional formatting code.

解决方案

I understand the problem. I suggested Tony to make some changes in jqGrid code. Mostly it would be enough to modify the place in the code to fill first rd and then in the next for loop call addCell with rd as additional parameter. The function addCell could forward the information to formatCol and the formatCol can call cellattr with additional parameter rd which will be has the information in exact the same format like you as want.

Nevertheless one can relatively easy to have almost the same results which you need without any changes in the jqGrid code. To do this one can just construct the map object which can give us the index of the column in the rawObject based on the name.

For example we can use beforeRequest or beforeProcessing to fill the map if it is not yet filled. The code can looks like

var colMap = {};
$("#tree").jqGrid({
    ...
    colModel: [
        {name: 'missingBooks',
            cellattr: function (rowId, tv, rawObject, cm, rdata) {
                //conditional formatting
                 if (Number(rawObject[colMap.missingBooks]) > 0) {
                     return ' style="background-color:#FFCCCC"';
                 } else {
                     return '';
                 }
            }
            ...
    ],
    beforeRequest: function () {
        if ($.isEmptyObject(colMap)) {
            var i, cmi,
                cm = $(this).jqGrid('getGridParam', 'colModel'),
                l = cm.length;
            for (i = 0; i < l; i++) {
                cmi = cm[i];
                colMap[cmi.name] = i;
            }
        }
    }
});

So the code will be free from usage of indexes like rawObject[11] where the index 11 can be changed after some modification in the code.

You can see the corresponding demo here.

这篇关于我怎样才能从jqGrid列的单元格值为背景做条件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆