简体   繁体   English

如何获取分组的Kendo网格中所选行的索引和数据

[英]How do I get the index and data of a selected row in a grouped Kendo grid

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. 我在这个jsfiddle链接中添加了我的代码。 This code worked in my system, I dont know why deleteRecord() method isn't invoked in jsfiddle, But that is not actual question. 这个代码在我的系统中工作,我不知道为什么在jsfiddle中没有调用deleteRecord()方法,但这不是实际的问题。

Here while clicking on last row's cancel button alert message will says index as 8 , But the actual index is 4 . 在这里点击最后一行的取消按钮提醒消息时会将索引显示为8 ,但实际索引为4 every button gives me wrong index only. 每个按钮只给我错误的索引。

You're using a very old version of Kendo UI in your fiddle, so selecting didn't work either. 你在小提琴中使用了一个非常古老的Kendo UI版本,因此选择也不起作用。 The reason it didn't find deleteRecord is that you set your fiddle to wrap in window.onLoad , which happens after document.ready . 它没有找到deleteRecord的原因是你将你的小提琴设置为window.onLoad ,它发生在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: 关于行索引:您需要确定相对于网格数据行的索引(如果您只是获取所选行的索引,它也将计算分组行;如果您有任何行,则会对细节行进行相同的操作) ,所以你可以像这样使用grid.items()

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: 所以,它可能只是我的kendo配置,但我必须访问所选记录的行索引的方式是这样的:

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. 因此,一旦我在变量中设置了索引,我就必须通过向指定的行添加和删除CSS类来设置它。 I had to use a element.find method to do this like so: 我不得不使用element.find方法来执行此操作:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM