10

Is there a way we can find out the index of column in grid, if we know the column name in Kendo grid?

e.g.

EmployeeID| Name
123       | John

I want to know the index of 'Name' field i.e. 1 in the grid. Any suggestions.

Thanks.

Sanjeev

Jayesh Goyani
  • 11,008
  • 11
  • 30
  • 50
sanjeev40084
  • 9,227
  • 18
  • 67
  • 99
  • What do you need the index for? Are you trying to edit/delete? You need to provide a little more information so we can help. – haakon.io Dec 18 '15 at 01:02
  • @haakon319 i need to hide/show column in my grid. hideColumn/showColumn works in Kendo grid but the performance is really slow so i wanted to try out, this solution here, which requires column index. http://stackoverflow.com/questions/30167893/kendo-grid-column-show-hide-making-issue-with-80-columns – sanjeev40084 Dec 18 '15 at 14:41

3 Answers3

14

Please try with the below code snippet.

<!DOCTYPE html>
<html>
<head>
    <title>Jayesh Goyani</title>
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.902/styles/kendo.common-bootstrap.min.css" />
    <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2015.2.902/styles/kendo.bootstrap.min.css" />
    <script src="https://kendo.cdn.telerik.com/2015.2.902/js/jquery.min.js"></script>
    <script src="https://kendo.cdn.telerik.com/2015.2.902/js/kendo.all.min.js"></script>
</head>
<body>
    <div id="example"></div>
    <input type="text" id="txtColumnName" />
    <button onclick="GetColumnIndexFromName();">GetIndex</button>
    <script>
        $(document).ready(function () {
            $("#example").kendoGrid({
                dataSource: {
                    type: "odata",
                    transport: {
                        read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Customers"
                    },
                    pageSize: 20
                },
                height: 550,
                groupable: true,
                sortable: true,
                pageable: {
                    refresh: true,
                    pageSizes: true,
                    buttonCount: 5
                },
                columns: [{
                    template: "<div class='customer-name'>#: ContactName #</div>",
                    field: "ContactName",
                    title: "Contact Name",
                    width: 240
                }, {
                    field: "ContactTitle",
                    title: "Contact Title"
                }, {
                    field: "CompanyName",
                    title: "Company Name"
                }, {
                    field: "Country",
                    width: 150
                }]
            });
        });

        function GetColumnIndexFromName() {
            var index = -1;
            var strName = $("#txtColumnName").val();
            var grid = $("#example").data("kendoGrid");
            var columns = grid.options.columns;
            if (columns.length > 0) {
                for (var i = 0; i < columns.length; i++) {
                    if (columns[i].field == strName) { // columns[i].title -- You can also use title property here but for this you have to assign title for all columns
                        index = i;
                    }
                }
            }

            if (index == -1) {
                alert("column name not exists");
            }
            else {
                alert("column index is:- " + index);
            }
        }
    </script>
</body>
</html>

Let me know if any concern.

Jayesh Goyani
  • 11,008
  • 11
  • 30
  • 50
  • Just FYI: I used your answer to solve my own question here - https://stackoverflow.com/q/66066772/153923 –  Feb 05 '21 at 16:32
4

This code will give the column object:

var grid = $('#grid').getKendoGrid();
grid.columns.find(function(v, i) { return grid.columns[i].field == 'myColumnName'; })

Of course you can customize the filter further if you want.

  • This technically doesn't get the **index** of the column, but it does get the actual column, which is what I needed rather than the index. – TLS Dec 12 '17 at 16:38
  • 1
    `grid.columns.findIndex(function(v, i) { return grid.columns[i].field == 'myColumnName'; }) ` gets you the index if that is all you need – BGTurner May 31 '18 at 13:51
1

Because kendo grid can have grouped columns, I made a slight improvement. Should be a recursive function, but her it is:

/**
 * Get a column index for a given name
 * @param {any} columnName
 */
function GetColumnIndexFromName(columnName) {
    var index = -1;
    var grid = getKendoGrid();
    var columns = grid.options.columns;
    if (columns.length > 0) {
        for (var i = 0; i < columns.length; i++) {
            if (columns[i].field == columnName) { // columns[i].title -- You can also use title property here but for this you have to assign title for all columns
                index = i;
            }
            // check if the column is grouped
            if (columns[i].columns !== undefined && columns[i].columns.length > 0) {
                for (var j = 0; j < columns[i].columns.length; j++) {
                    if (columns[i].columns[i].field == columnName) { // columns[i].title -- You can also use title property here but for this you have to assign title for all columns
                        index = i + j; // because the column index in a row is not grouped, the counting is continued inside the grouping
                    }
                }
            }
        }
    }
    if (index !== -1) {
        return index;
    }
    return index;
}

usage : GetColumnIndexFromName('');

Wijnand
  • 11
  • 1