Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I remove a CSS class from a jqGrid cell?

Tags:

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");

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

like image 415
Chris Ammerman Avatar asked Jun 14 '11 15:06

Chris Ammerman


People also ask

How do you remove a class in CSS?

To add the CSS classes to an element we use addClass() method, and to remove the CSS classes we use removeClass() method.

How do I remove all CSS from a class?

To remove all CSS classes of an element, we use removeClass() method. The removeClass() method is used to remove one or more class names from the selected element.


2 Answers

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");

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;
}

See the demo here.

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

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.

like image 102
Oleg Avatar answered Oct 09 '22 13:10

Oleg


One can easily add new class to a cell by removing the old class as:

$("#gridname").removeClass('oldclass')
              .setCell(rowId,'column_name','','newclass');

Where rowId is id of the row containing corresponding cell and can be obtained as:

var ids = $("#gridname").jqGrid('getDataIDs');
like image 43
Deepak Jha Avatar answered Oct 09 '22 14:10

Deepak Jha