最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I get the index and data of a selected row in a grouped Kendo grid - Stack Overflow

programmeradmin3浏览0评论

I'm trying to access the row index like this:

var grid = $("#grid").data("kendoGrid");
alert(grid.select().index());

I have added my code in this jsfiddle link. This code worked in my system, I dont know why deleteRecord() method isn't invoked in jsfiddle, But that is not actual question.

Here while clicking on last row's cancel button alert message will says index as 8, But the actual index is 4. every button gives me wrong index only.

I'm trying to access the row index like this:

var grid = $("#grid").data("kendoGrid");
alert(grid.select().index());

I have added my code in this jsfiddle link. This code worked in my system, I dont know why deleteRecord() method isn't invoked in jsfiddle, But that is not actual question.

Here while clicking on last row's cancel button alert message will says index as 8, But the actual index is 4. every button gives me wrong index only.

Share Improve this question edited Dec 30, 2013 at 14:42 Lars Höppner 18.4k2 gold badges47 silver badges73 bronze badges asked Dec 28, 2013 at 13:38 GunaseelanGunaseelan 15.5k11 gold badges84 silver badges130 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

You're using a very old version of Kendo UI in your fiddle, so selecting didn't work either. The reason it didn't find deleteRecord is that you set your fiddle to wrap in window.onLoad, which happens after document.ready.

Regarding the row index: you need to determine the index relative to the grid's data rows (if you simply get the index of the selected row, it will count the grouping rows as well; the same would happen for detail rows if you had any), so you can use grid.items() like this:

var grid = $("#grid").data("kendoGrid");        
var dataRows = grid.items();
var rowIndex = dataRows.index(grid.select());

See demo here.

If what you're really interested in is accessing the data of the selected row, you should use something like this (note that all of this is assuming your grid is set to cell or single row selection):

var tr = grid.select().closest("tr");
var dataItem = grid.dataItem(tr);

So, it may just be my kendo configuration but the way I had to access the row index of the selected records was like so:

var archGrid = $("#archiveRecords").data("kendoGrid");
var impGrid = $("#importedRecords").data("kendoGrid");

var archRow = archGrid.select();
var impRow = impGrid.select();

var archRowIndex = archRow[0].rowIndex;
var impRowIndex = impRow[0].rowIndex;

So once I had the index set up in my variables I had to set it by adding and removing CSS classes to my specified rows. I had to use a element.find method to do this like so:

if (condition1) {
    impRow.removeClass('k-state-selected');
    $('#importedRecords').data('kendoGrid').element.find('tbody tr:eq(' + archRowIndex + ')').addClass('k-state-selected');
}
else if (condition2){
    archRow.removeClass('k-state-selected');
    $('#archiveRecords').data('kendoGrid').element.find('tbody tr:eq(' + impRowIndex + ')').addClass('k-state-selected');
}

Just posting because I spent a long time looking for how to do set a selected row by the row index. Good luck!

Below codes will give you the row index as well as column index in kendo grid I hope this will be useful

var grid = $("#kendogridid").data("kendoGrid");
                  var row = $(this).closest("tr");
      var rowIdx = $("tr", grid).index(row);        
                  var colIdx = $("td", row).index(this);
发布评论

评论列表(0)

  1. 暂无评论