Kendo grid change event fires only once per row se

2019-07-27 22:40发布

问题:

In a MVC4 project, I've got 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's 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 called:

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 done with it.

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

This works fine. The problem is, the event is fired every time I select a different row but if I select a row that was previously selected, the event isn't fired and I can't get the code of that row.

Any help?

回答1:

Add .Selectable() in your grid so it allow you select previously row.

@(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:

you should add Selectable() before add event like this

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