Checkbox column, get selected values and pass to controller

10,041

You can access the backing data on your grid in your event handler

    dataBound: function(e) {
      $(".checkbox").bind("change", function(e) {
        var grid = $("#grid").data("kendoGrid");
        var row = $(e.target).closest("tr");
        row.toggleClass("k-state-selected");
        var data = grid.dataItem(row);
        alert(data.ProductID);
      });
    }

To get a list of selected id's you can either track them during the click event or just collect them all when an event is fired, eg.

    $("#actionButton").click(function(){
      var idsToSend = [];
      var grid = $("#grid").data("kendoGrid")
      var ds = grid.dataSource.view();

      for (var i = 0; i < ds.length; i++) {
        var row = grid.table.find("tr[data-uid='" + ds[i].uid + "']");
        var checkbox = $(row).find(".checkbox");
        if (checkbox.is(":checked")) {
          idsToSend.push(ds[i].ProductID);
        }
      }

      alert(idsToSend);

      //this obviously won't work , but just to illustrate the point.          
      $.post("/whatever", {ids: idsToSend});
    });

edit: demo

Share:
10,041
lazerbrain
Author by

lazerbrain

Updated on July 25, 2022

Comments

  • lazerbrain
    lazerbrain almost 2 years

    I have a column template:

    columns.Template(@<text></text>).ClientTemplate("<input type='checkbox'   class='checkbox'/>").Title("<input type='checkbox'/>").Width(10).Title("Izbor").HtmlAttributes(new { @onclick = "click", style = "align:center;float:none;text-align:center; font-size:12px; vertical-align: middle;" }).HeaderHtmlAttributes(new { style = "overflow: visible; white-space: normal; text-align:center; font-size:12px; font-weight: bold;" });
    

    I select rows with this code:

    function onDataBound(e) {
        $(".checkbox").bind("change", function(e) {
            $(e.target).closest("tr").toggleClass("k-state-selected");
        })
    }
    

    How can I select a certain column (eg. Ids) from the grid and pass them to the controller?