Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
135
rated 0 times [  137] [ 2]  / answers: 1 / hits: 22212  / 9 Years ago, thu, december 17, 2015, 12:00:00

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


More From » kendo-ui

 Answers
97

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.


[#64026] Wednesday, December 16, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
trinityr

Total Points: 49
Total Questions: 107
Total Answers: 96

Location: Mayotte
Member since Fri, Oct 1, 2021
3 Years ago
;