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

I tried to access the rowObject in a custom formatting function by column name, but it did not give any values. I tried this with both JSON and the XML data type.

Is there a 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>"; } 
+4
source share
1 answer

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" .

+10
source

Source: https://habr.com/ru/post/1338383/


All Articles