Sequenzia
Sequenzia

Reputation: 2381

jqGrid sort index by column name

Simple question. Is there a function to get the current sort position of a column by name in a grid?

Upvotes: 2

Views: 2663

Answers (1)

Oleg
Oleg

Reputation: 221997

The indexes used in the remapColumns method are the same as in the colModel array. It's important to understand, that the indexes can be different as in the colModel parameter of jqGrid used initially. If jqGrid uses options rownumbers: true an additional column with the name 'rn' will be inserted at the first place of colModel array. The indexes of all other elements of colModel array will be incremented. In the same way the option multiselect: true inserts the column 'cb' abd the option subGrid: true inserts the column 'subgrid'. In the same way the option treeGrid: true follows appending colModel array with some additional hidden columns which names can be defined by treeReader. The default names of the columns in case of treeGridModel: 'nested' are: 'level', 'lft', 'rgt', 'isLeaf', 'expanded', 'loaded' and 'icon' or 'level', 'parent', 'isLeaf', 'expanded', 'loaded' and 'icon' in case of treeGridModel: 'adjacency'.

So to find the index of the column by the name you should just get the current colModel, look through the items and find the item where 'name' property are the column name which you need. To get colModel you can use $("#grid")[0].p.colModel or $("#grid").jqGrid('getgridParam', 'colModel'). So the code can be like the following:

var getColumnIndexByName = function (columnName) {
        var cm = $(this).jqGrid('getGridParam', 'colModel'), i, l = cm.length;

        for (i = 0; i < l; i++) {
            if (cm[i].name === columnName) {
                return i; // return the index
            }
        }
        return -1;
    };

and the usage like

var $grid = $("#grid"),
    iCol = getColumnIndexByName.call($grid[0], 'myColName');

To get the name of the current sorted column you can use $grid.jqGrid('getGridParam', 'sortname')

Upvotes: 4

Related Questions