Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get the column number by column name in jqgrid

I tried to access the rowObject in custom formatter function by column name but it didn't give any values. I have tried this with both JSON and XML data type .

Is there any way to get the column number by column name in jqgrid.

function Draw_Link ( cellvalue , options , rowObject )
 { 
  return "<a href='someurl.php?col_name="+rowobject.col_name+"'>"+cellvalue+"</a>";
 }
like image 405
kiruthika Avatar asked Dec 17 '22 17:12

kiruthika


1 Answers

The column index for the column is the same as the index of the column in the colModel array before the jqGrid initialization (it is the same as in the input parameter colModel). If you use rownumbers:true, multiselect:true or subGrid:true additional columns will be addid to the grid as the first rows, so the column index which one has in the colModel array as the jqGrid parameter can be other as one have after the grid initialization. You can use for example this simple function to get the index

var getColumnSrcIndexByName = function(grid,columnName) {
    var cm = grid.jqGrid('getGridParam','colModel'),
        i=0, index=0, l=cm.length, cmName;
    while (i<l) {
        cmName = cm[i].name;
        i++;
        if (cmName===columnName) {
            return index;
        } else if (cmName!=='rn' && cmName!=='cb' && cmName!=='subgrid') {
            index++;
        }
    }
    return -1;
};
var index = getColumnSrcIndexByName($("#list"),'MyColumn');

UPDATED: Free jqGrid fork simplifies getting column index from column name because it holds internally the parameter iColByName, which is the map by the column name. One can just get iColByName via

var iColByName = $("#list").jqGrid("getGridParam", "iColByName");

and iColByName["MyColumn"] will be the required column index (iCol). I remind that one can use getGridParam without any parameter to get the reference to all parameters of jqGrid:

var p = $("#list").jqGrid("getGridParam");

The value

var iCol = p.iColByName["MyColumn"];

will be the column index and p.colModel[iCol].name will be "MyColumn".

like image 51
Oleg Avatar answered May 24 '23 05:05

Oleg