Kendoui Grid Get Selected Row Id when Edit Button is clicked

匿名 (未验证) 提交于 2019-12-03 08:50:26

问题:

I have a simple grid and I'm having a lot of trouble collecting the PersonID when the edit button is clicked using JQuery. I need the PersonID because I'm going to add a file upload to the inline edit column and i want to upload a file and associate it with the PersonID. All help welcome :)

@(Html.Kendo().Grid<GridCustomPopupTemplate.Models.Person>().Name("Grid") .DataSource(dataSource => dataSource     .Ajax()     .Model(model => model.Id(m => m.PersonID))         .Read(read => read.Action("GetPersons", "Home"))         .Update(up => up.Action("UpdatePerson", "Home")) )  .Columns(columns => {     columns.Bound(c => c.PersonID).Width(200);     columns.Bound(c => c.Name);     columns.Command(cmd => cmd.Edit()); })  .Pageable() .Sortable() .Editable(ed => ed.Mode(GridEditMode.InLine)) .Events(ev => ev.Edit("doOnRowSelect"))   )    <script type="text/javascript">  function doOnRowSelect(e) {       alert(The #PersonID# of the row which we are going to edit here....)  }  </script> 

回答1:

I got this working by using the following JavaScript:

$("body").on("click", "[role='row']", function (e) {      var grid = $("#Grid").getKendoGrid();     var item = grid.dataItem($(e.target).closest("tr"));     alert(item.PersonID);  }); 


回答2:

Use the grid.dataitem on the selected grid row.

var grid = $('#grdMyGrid').data('kendoGrid'); var selectedItem = grid.dataItem(grid.select()); 

Updated with a OnChange(e) handler.

In an .OnChange(e=>e.OnChange("grdOnChange")), you can store and access the item. I am sure the grids change will fire edit button in another row is selected.

var selectedIndex=0; function grdOnChange(e) {    var selectedDataItem = e != null ? e.sender.dataItem(e.sender.select()) : null;  selectedIndex=selectedDataItem.MyModelID; 

}



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!