Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current row in a Kendo context menu?

I have a Kendo context menu for a Kendo grid. I want to extract value of current row. This is my view:

@(Html.Kendo().Grid<Class>()
    .Name("reqRows")
    .Columns(columns =>
    {
        columns.Bound(x => x.0);
    })
)

@(Html.Kendo().ContextMenu()
    .Name("brc")
    .Target("#reqRows")
    .Items(items =>
    {
        items.Add()
            .Text("1").LinkHtmlAttributes(new { onClick = "confirm()" });
    })
)

<script>
    function confirm(e) {
        alert(e);
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        alert(dataItem.0);
    }
</script>

I get Undefined value for e. How can I extract the current row? And can I extract Model.Id, which is not bound in columns?

like image 383
Akbari Avatar asked Oct 23 '25 06:10

Akbari


1 Answers

You can use the Select event:

@(Html.Kendo().ContextMenu()
    .Name("brc")
    .Target("#reqRows")
    .Items(items =>
    {
      items.Add().Text("1");
    })
    .Events(e => e.Select("selectItem"))
)

Then get the selected item like this:

function selectItem(e) {
    var grid = $(e.target).data("kendoGrid");
    var item = grid.dataItem(grid.select());
    var data = item.SomeColumn;
}

If you open the context menu using the right mouse button, it won't select the row by default and item will be undefined. Add this javascript to ensure the row is selected (note that your grid will need to be .Selectable()):

$("#reqRows").on("mousedown", "tr[role='row']", function (e) {
    if (e.which === 3) {
        $("#reqRows tbody tr").removeClass("k-state-selected");
        $(this).addClass("k-state-selected");
    }
});

If you want ID, you'll need to bind it to the grid. Just keep it hidden:

@(Html.Kendo().Grid<Class>()
    .Name("reqRows")
    .Columns(columns =>
    {
        columns.Bound(x => x.Id).Hidden();
        columns.Bound(x => x.SomeColumn);
    })
)
like image 61
Nic Avatar answered Oct 25 '25 20:10

Nic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!