The column index for the column is the same as the column index in the colModel array before jqGrid was initialized (it is similar to the colModel input parameter). If you use rownumbers:true , multiselect:true or subGrid:true , additional columns will be added to the grid as the first rows, so the index of the column that has colModel in the array as the jqGrid parameter may be different, since there is one after 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 makes it easier to get the column index from the column name, as it contains the internal parameter iColByName , which is the display of the column name. You can just get iColByName via
var iColByName = $("#list").jqGrid("getGridParam", "iColByName");
and iColByName["MyColumn"] will be the required column index ( iCol ). I remind you that you can use getGridParam without any parameter to get a link to all jqGrid parameters:
var p = $("#list").jqGrid("getGridParam");
Value
var iCol = p.iColByName["MyColumn"];
there will be a column index, and p.colModel[iCol].name will be "MyColumn" .
source share