jqGrid 自定义格式在 addClass 上失败 [英] jqGrid custom format fails on addClass

查看:15
本文介绍了jqGrid 自定义格式在 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.

推荐答案

你犯了自定义格式化程序.如果网格包含将被创建为字符串,那么了解 jqGrid 具有最佳性能是很重要的.如果 gridview:true 为您提供性能.任何 自定义格式化程序 都应该在 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");

在一些旧答案中(请参阅 这里here) 你可以找到解决问题的方法,但我建议你使用 jqGrid 4.0.0 的新功能:cellattr 选项.对于 undefrstanding:自定义格式化程序的目的不是添加一些 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.

在你的情况下你可以定义

In you case you can define

cellattr: function(rowId, cellValue, rawObject, cm, rdata) {
    if (cellValue==0) {
        return ' class="Fail"';
    }
}

查看一个小演示这里:

在演示中,我将 ui-state-errorui-state-error-text 类添加到 'Client' 的所有单元格中'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屋!

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