Kendo grid change event fires only once to select a row

In an MVC4 project, I have a kendo grid that fires an event when a row is selected.

<div id="datagrid"> @(Html.Kendo().Grid<SustIMS.Models.StretchModel>() .Name("datagrid_Stretches") .Columns(columns => { columns.Bound(s => s.StretchCode).Title(ViewBag.lblCode).Width(80); columns.Bound(s => s.StretchMediumDescription).Title(ViewBag.lblDescription); ... }) .Events(e => e.Change("onChange")) <--------- here the event .DataSource(dataSource => dataSource .Ajax() .PageSize(15) .Read(read => read.Action("GetStretches", "MasterData")) ) ) </div> 

So, when I select a row, the onChange function is onChange :

 function onChange() { var code = this.dataItem(this.select()).StretchCode; $.get('@Url.Content("getStretchCode")', { "code": code }); } 

In my controller, the code is retrieved, and some operations are performed with it.

 public void getStretchCode(string code) { currentStretch.RoadComponentCode = code; ... } 

It works great. The problem is that the event is fired every time I select a different row, but if I select a previously selected row, the event does not fire, and I cannot get the code for this row.

Any help?

+1
source share
2 answers

Add .Selectable() to your grid so you can select the row earlier.

 @(Html.Kendo().Grid<SustIMS.Models.StretchModel>() .Name("datagrid_Stretches") .Columns(columns => { columns.Bound(s => s.StretchCode).Title(ViewBag.lblCode).Width(80); columns.Bound(s => s.StretchMediumDescription).Title(ViewBag.lblDescription); ... }) .Selectable(selectable => selectable .Type(GridSelectionType.Row) <--------- Add This ) .Events(e => e.Change("onChange")) .DataSource(dataSource => dataSource .Ajax() .PageSize(15) .Read(read => read.Action("GetStretches", "MasterData")) ) ) 

Script

 function onChange() { var code = this.dataItem(this.select()).StretchCode; $.post('@Url.Content("getStretchCode")', { "code": code }); } 
+2
source

you must add Selectable () before adding such an event

  .Selectable(selectable => selectable .Mode(GridSelectionMode.Multiple) .Type(GridSelectionType.Cell)) .Events(e => e.Change("onChange")) 
+2
source

Source: https://habr.com/ru/post/973032/


All Articles