如何从 jqGrid 单元格中删除 CSS 类? [英] How do I remove a CSS class from a jqGrid cell?

查看:18
本文介绍了如何从 jqGrid 单元格中删除 CSS 类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用下面的 setCell 方法将 CSS 类添加到 jqGrid 单元格.

It is possible to add a CSS class to a jqGrid cell using the setCell method as below.

grid.setCell(rowId, "ColumnName", "", "my-style-class");

考虑到此方法似乎只能添加 CSS 类,如何从 jqGrid 单元中删除 CSS 类?

Considering that this method appears only able to add CSS classes, how can one remove a CSS class from a jqGrid cell?

推荐答案

不能用标准的 jqGrid 方法删除调用类.因此,您必须手动执行此操作:

One can't remove the call class with a standard jqGrid method. So you have to do this manually:

var iCol = getColumnIndexByName(grid,"ColumnName"),
    tr = grid[0].rows.namedItem(rowid), // grid is defined as grid=$("#grid_id")
    td = tr.cells[iCol];
$(td).removeClass("my-style-class");

其中 getColumnIndexByName 是一个简单的函数,它通过列名获取列索引:

where getColumnIndexByName is a simple function which get the column index by the column name:

var getColumnIndexByName = function(grid,columnName) {
    var cm = grid.jqGrid('getGridParam','colModel');
    for (var i=0,l=cm.length; i<l; i++) {
        if (cm[i].name===columnName) {
            return i; // return the index
        }
    }
    return -1;
}

查看演示这里.

更新:免费 jqGridiColByName 内部参数,可用于代替 getColumnIndexByName 函数.iColByName 参数将在内部由空闲的 jqGrid 填充,并将通过列的重新排序来更新.所以使用起来很安全

UPDATED: Free jqGrid have iColByName internal parameter which can be used instead of getColumnIndexByName function. The iColByName parameter will be filled by free jqGrid internally and it will updated by reodering of columns. So it's safe to use

var p = grid.jqGrid("getGridParam"), // get the reference to all parameters
    iCol = p.iColByName["ColumnName"], // get index by column name
    cm = p.colModel[iCol]; // item of "ColumnName" column

方法很简单,效果也很快.应该考虑到该功能在免费 jqGrid 4.8 发布之后 包含在免费 jqGrid 中.因此,必须从 GitHub 下载最新的源代码或至少使用免费的 jqGrid 4.9-beta1 才能拥有该功能.

The way is very simple and it works very quickly. One should take in consideration that the feature is included in free jqGrid after publishing of free jqGrid 4.8. So one have to download the latest sources from GitHub or to use at least free jqGrid 4.9-beta1 to have the feature.

这篇关于如何从 jqGrid 单元格中删除 CSS 类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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