如何在kendo UI MVC网格上获取选择单个单元格的行数据?

时间:2016-03-16 16:38:07

标签: javascript asp.net-mvc kendo-grid

我正在使用一个kendo网格来表示一个年度计划,其中我为每个人都有一行,一个列有员工的名字,另一个30/31表示网格上显示的那一天。 我需要的是选择一个单元格,获取员工姓名(或ID)和索引。

这是网格代码:

@(Html.Kendo().Grid<WorkTimeManager.Presentation.Models.YearPlanViewModel>()
      .Name("gridJan")

      .Columns(col =>
      {
          col.Bound(c => c.EmployeeName).Title("Employee").Width(170);
          col.Group(group => group
           .Title("January")
           .Columns(columns =>
           {
               columns.Bound(c => c.Day1).Width(30);}
               columns.Bound(c => c.Day2).Width(30);}
               columns.Bound(c => c.Day3).Width(30);}

......还有一些...... 网格可由单元格(非行)

选择
.Selectable(selectable => selectable
            .Mode(GridSelectionMode.Multiple)
            .Type(GridSelectionType.Cell))

我需要使用JavaScript访问数据项,到目前为止,这就是我得到的所有内容:

function getDataFromGrid(gridName){
        var grid = $('#'+gridName).data('kendoGrid');
        alert(grid);
        var cell = grid.select();

使用

var data = grid.dataItem(cell);
除非可以按行(不是单元格)选择,否则

将无效。

提前谢谢

2 个答案:

答案 0 :(得分:1)

GridSelectionMode为“Multiple”,GridSelectionType为“Cell”,select方法返回所选网格单元格数组

var grid = $("#gridJan").data("kendoGrid");
currentSelection = grid.select();
selectedRows = [];
currentSelection.each(function () {
var currentRowIndex = $(this).closest("tr").index();
if (selectedRows.indexOf(currentRowIndex) == -1) {
    selectedRows.push(currentRowIndex);
}
})

答案 1 :(得分:0)

@Pizzy你可以使用jQuery和click事件来导航网格吗?类似下面的东西..

$('#gridJan tr td').click(function(){  

   //Get the name, etc by navigating upward from td to the tr parent and then back down to find specific child td that contains the needed data
   var id = $(this).parent('tr').children('whatever you are looking for').html();

   ...
});