如何从 jqGrid 列获取单元格值以对背景色进行条件格式设置
我正在使用 jqGrid 树网格,我想根据单元格中的数据值(它是一个整数)来格式化列的背景颜色:
这是一个我设置列的示例:
{
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
},
这工作正常,但我的问题在于cellAttr 回调。在这个条件格式行中:
if (rawObject[11] > 0) {
return 'style="background-color:#FFCCCC"';
}
我想重用这个逻辑,所以我不想索引到 rawObject 并找出我正在使用的列。我希望有一种方法可以做这样的事情:
if (rawObject.missingBooks > 0) {
return 'style="background-color:#FFCCCC"';
}
但这似乎是未定义的。这样,如果我添加新列,我不必重新索引所有这些条件格式代码。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我明白这个问题。我建议 Tony 对 jqGrid 代码进行一些更改。大多数情况下,修改该位置 在代码中填充第一个
rd
,然后在下一个 for 循环中调用addCell
,并将rd
作为附加参数。函数addCell
可以将信息转发给formatCol
,而formatCol
可以使用附加参数rd 调用
其中的信息格式与您想要的格式完全相同。cellattr
尽管如此,无需对 jqGrid 代码进行任何更改,就可以相对轻松地获得几乎与您所需相同的结果。要做到这一点,只需构造一个地图对象,它可以根据名称为我们提供
rawObject
中列的索引。例如,如果地图尚未填充,我们可以使用
beforeRequest
或beforeProcessing
来填充地图。代码可以看起来像这样,代码将不再使用像
rawObject[11]
这样的索引,其中索引11
可以在代码中进行一些修改后更改。您可以在此处查看相应的演示。
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 calladdCell
withrd
as additional parameter. The functionaddCell
could forward the information toformatCol
and theformatCol
can callcellattr
with additional parameterrd
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
orbeforeProcessing
to fill the map if it is not yet filled. The code can looks likeSo the code will be free from usage of indexes like
rawObject[11]
where the index11
can be changed after some modification in the code.You can see the corresponding demo here.