Search code examples
jquerykendo-grid

How do I get the index of a specific column in a grouped column (multi header) Kendo grid


I have kendo grid which has grouped column headers (Two headers) binded with data source. I am using the following code to get the column index of single header row grid which is working fine.

dataBound: function (e) {
            var grid = e.sender;
            var rows = grid.tbody.children();
            var dataItem = grid.dataItem(rows[0]);
            var priColIndex = grid.wrapper.find(".k-grid-header [data-field=FromDemandQty]").index();
            var cell = row.children().eq(priColIndex);

            if (dataItem.FromDemandQty < 0)
                cell.addClass('stkShort');
            else  
                cell.addClass('stkExcess');
}

But this is not working when my kendo grid has two header rows. Below is the Screen shot of my kendo grid. Above mentioned code returns index as 1 when I actually trying to get the index of excess/short column in the image which looks it is reseting the index when a new group starts (Group name : Requestor Data, refer the image)

enter image description here


Solution

  • Finally I got the solution. This will work for both the scenario mentioned on my question

    Insteed of taking the index directly, need to take the index of column's dataset

    Existing code :

    var priColIndex = grid.wrapper.find(".k-grid-header [data-field=FromDemandQty]").index();
    var cell = row.children().eq(priColIndex);
    
    if (dataItem.FromDemandQty < 0)
        cell.addClass('stkShort');
    else  
        cell.addClass('stkExcess');
    

    Should be changed as :

    var fromDemCell = grid.wrapper.find(".k-grid-header [data-field=FromDemandQty]");
    if (fromDemCell.length > 0) {
        var priColIndex = parseInt(fromDemCell[0].dataset.index);
        var cell = row.children().eq(priColIndex );
        if (dataItem.FromDemandQty < 0) {
            cell.addClass('stkShort');
        } else {                               
            cell.addClass('stkExcess');
        }
    }