This is a migrated thread and some comments may be shown as answers.

Getting a column value and passing to javascript

2 Answers 1324 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Allan
Top achievements
Rank 1
Allan asked on 16 May 2016, 09:15 AM

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 Answers, 1 is accepted

Sort by
0
Accepted
Stephen
Top achievements
Rank 2
answered on 16 May 2016, 01:46 PM

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.

 

 

0
Allan
Top achievements
Rank 1
answered on 17 May 2016, 08:03 AM
[quote]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.

[/quote]

Worked perfectly thank you.

Tags
Grid
Asked by
Allan
Top achievements
Rank 1
Answers by
Stephen
Top achievements
Rank 2
Allan
Top achievements
Rank 1
Share this question
or