Kendoui Grid Get the selected row identifier when you click the Edit button

I have a simple grid and I have a lot of problems building PersonID when I click the edit button using jQuery. I need a PersonID because I'm going to add the file upload to the inline edit column, and I want to upload the file and associate it with PersonID. All help is appreciated :)

@(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>
+4
source share
2 answers

I got this working 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);

});
+5
source

Use grid.dataitemthe grid in the selected row.

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

Updated by the OnChange (e) handler.

.OnChange(e=>e.OnChange("grdOnChange")) . , .

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

selectedIndex=selectedDataItem.MyModelID;

}

+2

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


All Articles