jqGrid自定义格式在addClass上失败 [英] jqGrid custom format fails on addClass
问题描述
我从json填充了一个新的网格 使用自定义格式化程序 格式化程序已定义:
I populate a new grid from json with custom formatter the formatter is defined :
testFormatter(value,el,opts)
{
if (value==0)
{
$(el).addClass("Fail");
}
…
}
我期望单元格使用css类 但是如果我检查单元格,他们不会添加该类.
I am expecting the cells to use the css class but If I check the cells they don't add that class.
推荐答案
您在使用自定义格式化程序都应在gridview:true
模式下工作,因此自定义格式器没有参数(它们是DOM元素),因此您不能使用$(el).addClass("Fail");
You made the typical error of the usage of the custom formatter. It is important to understand that the jqGrid has the best performance if the grid contain will be created as the string. In the case the gridview:true
gives you the performance. Any custom formatter should work in the gridview:true
mode, so the custom formater has no parameter which are DOM element and so you can not use operations like $(el).addClass("Fail");
在一些旧答案中(请参阅在此处),您可以找到解决问题的方法,但是我建议您使用jqGrid 4.0.0的新功能:cellattr
选项.出于不确定的目的:自定义格式化程序的目的不是添加一些HTML属性,例如class.例如,应将其用于将某些通用日期格式(如yyyy-mm-dd)转换为本地化格式(如dd.mm.yyyy(德语样式)).如果您不想更改列的格式,而只想添加一些属性,例如title
(用于工具提示),class
(例如您的情况),style
和因此,您需要的是新的cellattr
选项.
In some old answers (see here and here) you can find how the problem can be solved, but I would you suggest to use new feature of jqGrid 4.0.0: cellattr
option. For undefrstanding: the purpose of the custom formatter is not add some HTML attributes like class for example. It should be used for example to convert some universal date format like yyyy-mm-dd to localized form like dd.mm.yyyy (German style). If you don't want to change format of the column, but want only add some attributes like title
(used for tooltips), class
(like in your case), style
and so on the new cellattr
option is what you need.
在您可以定义的情况下
cellattr: function(rowId, cellValue, rawObject, cm, rdata) {
if (cellValue==0) {
return ' class="Fail"';
}
}
在此处:
在演示中,我向'Client'
列的所有单元格中添加了ui-state-error
和ui-state-error-text
刻度,其中在'Closed'
中设置了复选框.
In the demo I added calsses ui-state-error
and ui-state-error-text
to all cells of 'Client'
column where in the 'Closed'
the checkbox is set.
这篇关于jqGrid自定义格式在addClass上失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!