【问题标题】:how to get Kendo grid row selection如何获得剑道网格行选择
【发布时间】:2023-03-09 01:41:01
【问题描述】:

如何启用 kendo ui 网格行选择。我使用 html 辅助函数创建了一个剑道网格,通过 javascript 访问它并启用了行选择,但下面没有显示运气代码

@(Html.Kendo().Grid((IEnumerable<Gridkendo.Models.Model>)Model)
  .Name("grid")
  .Columns(columns =>
  {
      // Create a column bound to the ProductID property
      columns.Bound(product => product.Name);
      // Create a column bound to the ProductName property
      columns.Bound(product => product.Description);

并在 javascript 中访问它

<script>
    $(function () {
        // Notice that the Name() of the grid is used to get its client-side instance
        var grid = $("#grid").data("kendoGrid");
        var selectedItem = grid.dataItem(grid.select());
        alert(selectedItem.ShipName);
    });
</script>

【问题讨论】:

标签: jquery asp.net-mvc kendo-ui kendo-grid


【解决方案1】:

需要添加Selectable()配置方法。这将启用默认行选择,

@(Html.Kendo().Grid((IEnumerable<Gridkendo.Models.Model>)Model)
  .Name("grid")
  .Columns(columns =>
  {
      // Create a column bound to the ProductID property
      columns.Bound(product => product.Name);
      // Create a column bound to the ProductName property
      columns.Bound(product => product.Description);
  })
  .Selectable()
)

注意,您将在 document ready 事件中获得选定的项目。这意味着网格刚刚被初始化,并且不可能选择任何行。更好的方法是使用“选择”事件,可以在帮助程序中配置:

@(Html.Kendo().Grid((IEnumerable<Gridkendo.Models.Model>)Model)
  .Name("grid")
  .Columns(columns =>
  {
      // Create a column bound to the ProductID property
      columns.Bound(product => product.Name);
      // Create a column bound to the ProductName property
      columns.Bound(product => product.Description);
  })
  .Selectable()
  .Events(ev => ev.Change("doOnRowSelect"))
)

然后您需要定义doOnRowSelect JavaScript 函数:

function doOnRowSelect(e) {
    var selectedItem = this.dataItem(this.select());
    alert(selectedItem.ShipName);
}

编辑:上述方法(至少是 JavaScript 部分)仅在通过 AJAX 加载数据时有效。然而,当从Model 加载数据时,行选择也应该起作用。在这种情况下,选定的行将具有 k-state-selected 类:

function getSelectedItem () {
    var grid = $("#grid").data("kendoGrid");
    var selectedRows = $(".k-state-selected", "#grid");
    if (selectedRows.length > 0) {
       var selectedItem = grid.dataItem(selectedRows[0]);
       alert(selectedItem.ShipName);
    }
}

【讨论】:

  • 出现“没有指定数据源模型 ID 属性”等异常
  • @peter,是的,不幸的是,当通过Model 填充网格时,获取所选项目不起作用。它仅适用于 AJAX 加载 (see here)。然后删除该事件。您现在可以选择表格行了吗?
  • 仍然异常“没有指定数据源模型 ID 属性。”越来越
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 2016-04-11
  • 2020-09-02
相关资源
最近更新 更多