Getting a column value and passing to javascript

3 posts, 1 answers
  1. Allan
    Allan avatar
    34 posts
    Member since:
    Aug 2015

    Posted 16 May Link to this post

    Hello,

     

    My grid has a column called "vessel_idx" that shows all the IDs relating to each vessel(obviously), each row also has a button that calls my own Javascript function.  I need to pass the value of the vessel_idx into this function, does anyone know how I can accomplish this?

    Here is my grid with the required column and button as described above.

    @(Html.Kendo().Grid<MyProject.ViewModels.SpotGridViewModel>()
         .Name("Grid")
         .Deferred()
         .Columns(columns =>
         {
             columns.Bound(c => c.vessel_idx);
             columns.Template(@<text></text>)
                 .ClientTemplate("<ul class='grid-options'>" +
                                   "<input type='button' id='mybtn' value='Prompt' />"+   
                                 "</ul>").Width(200);
         })
    )

    Here is my javascript code that I need to pass the vessel_idx into

    $("#Grid").on("click", "#mybtn", function (e) {
         
        var id = vessel_idx;
     
                $.ajax({
                    url: "@Url.Action("buttonPrompt", "Home")",
                    type: "GET",
                data: { id: id },
                dataType: 'json',
                cache: false,
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("Error" + errorThrown);
                    console.log(errorThrown);
                },
                success: function (data) {
                    console.log("yay");
                }
                });
        });

    Many thanks

  2. Answer
    Stephen
    Stephen avatar
    86 posts
    Member since:
    Jan 2011

    Posted 16 May in reply to Allan Link to this post

    Inside the "click" function:

    var grid = $("#Grid").getKendoGrid(),
      dataItem = grid.dataItem($(this).closest("tr")),
      id = dataItem.vessel_idx;

     

    This should work...I do this all the time.

     

     

  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Allan
    Allan avatar
    34 posts
    Member since:
    Aug 2015

    Posted 17 May in reply to Stephen Link to this post

    Stephen said:

    Inside the "click" function:

    var grid = $("#Grid").getKendoGrid(),
      dataItem = grid.dataItem($(this).closest("tr")),
      id = dataItem.vessel_idx;

     

     

    This should work...I do this all the time.

    Worked perfectly thank you.

Back to Top