Selecting a row from a Kendo Grid programmatically

寵の児 提交于 2019-12-11 01:34:27

问题


I have got a grid and one of its columns is a template with a checkbox in it:

.Name("grid")
.Columns(columns =>
{
    columns.Bound(c => c.Id).ClientTemplate("<input type=\"checkbox\" id=\"chk_#=Id#\" class=\"gridCK\" onclick=\"zzz(this)\"/>");

When I check the checkbox, I want its row to become selected (and vice-versa). I have tried the following:

function zzz(e) {
    var id = e.id;
    var grid = $("#grid").data("kendoGrid");
    var data = grid.dataSource.data();
    var res = $.grep(data, function (d) {
        if (('chk_' + d.Id) == id) {
            return d.Id;
        }
    });
    var row = grid.table.find('tr[data-id="' + res[0].Id + '"]');
    if (e.checked) {
        row.addClass("k-state-selected");
    } else {
        row.removeClass("k-state-selected");
    }
}

But nothing happens, the row variable isn't the actual row of the grid.

How can I achieve this?


回答1:


Define the function handler as:

function zzz(e) {
    var row = $(e).closest("tr");
    row.addClass("k-state-selected");
}

See it in action:

$(document).ready(function () {
  var crudServiceBaseUrl = "http://demos.telerik.com/kendo-ui/service",
      dataSource = new kendo.data.DataSource({
        transport: {
          read:  {
            url: crudServiceBaseUrl + "/Products",
            dataType: "jsonp"
          },
          parameterMap: function(options, operation) {
            if (operation !== "read" && options.models) {
              return {models: kendo.stringify(options.models)};
            }
          }
        },
        pageSize: 5,
        schema: {
          model: {
            id: "ProductID",
            fields: {
              ProductID: { editable: false, nullable: true },
              ProductName: { validation: { required: true } },
              UnitPrice: { type: "number", validation: { required: true, min: 1} },
              Discontinued: { type: "boolean" },
              UnitsInStock: { type: "number", validation: { min: 0, required: true } }
            }
          }
        }
      });

  var grid = $("#grid").kendoGrid({
    dataSource: dataSource,
    navigatable: true,
    pageable: true,
    columns: [
      "ProductName",
      { field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: 120 },
      { field: "UnitsInStock", title: "Units In Stock", width: 120 },
      { field: "Select", template: "<input type=\"checkbox\" id=\"chk_#=ProductID#\" class=\"gridCK\" onclick=\"zzz(this)\"/>" }
    ],
    editable: false,
    selectable: "multiple"
  }).data("kendoGrid");
});

function zzz(i) {
  var row = $(i).closest("tr");
  row.addClass("k-state-selected");
}
html {
  font-size: 10px; 
  font-family: Arial, Helvetica, sans-serif; 
}
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.common.min.css" />
<link rel="stylesheet" href="http://cdn.kendostatic.com/2014.3.1119/styles/kendo.default.min.css" />
<script src="http://cdn.kendostatic.com/2014.3.1119/js/jquery.min.js"></script>
<script src="http://cdn.kendostatic.com/2014.3.1119/js/kendo.all.min.js"></script>

<div id="grid"></div>


来源:https://stackoverflow.com/questions/27922110/selecting-a-row-from-a-kendo-grid-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!