使用Javascript从Kendo网格中的列名获取列索引 [英] Get column index from column name in Kendo grid in Javascript

查看:21
本文介绍了使用Javascript从Kendo网格中的列名获取列索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们知道Kendo网格中的列名,有没有办法找出网格中列的索引?

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

例如

EmployeeID| Name
123       | John

我想知道名称"字段的索引,即网格中的 1.任何建议.

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

谢谢.

桑吉夫

推荐答案

请尝试使用以下代码片段.

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.

这篇关于使用Javascript从Kendo网格中的列名获取列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆