【问题标题】:how to get selected row value in the KendoUI如何在 KendoUI 中获取选定的行值
【发布时间】:2012-10-09 11:49:08
【问题描述】:

我有一个 kendoUI 网格。

                @(Html.Kendo().Grid<EntityVM>()
                    .Name("EntitesGrid")
                                .HtmlAttributes(new { style = "height:750px;width:100%;scrollbar-face-color: #eff7fc;" })
                    .Columns(columns =>
                    {
                        columns.Bound(e => e.Id).Hidden().IncludeInMenu(false);
                        columns.Bound(e => e.EntityVersionId).Hidden().IncludeInMenu(false);
                        columns.Bound(e => e.Name).Width("70%").Title("Entity Name");
                        columns.Bound(e => e.EIN).Width("30%");
                    })
        .ToolBar(toolBar => toolBar.Template("<a class='k-button k-button-icontext k-grid-add' id='addEntity'><span class='k-icon k-add'></span>Entity</a>" +
             "<a class='k-button k-button-icontext' id='editEntity'><span class='k-icon k-edit'></span>Edit</a>"))
                    .DataSource(dataSource => dataSource
                    .Ajax().ServerOperation(false)
                    .Model(model => model.Id(e => e.Id))
                    .Read(read => read.Action("GetEntities", "Entity", new { projectId = Request.QueryString[DataKeyNameConstants.ProjectId] })))
                    .Sortable()
                    .Scrollable()
                    .Filterable()
                    .Resizable(resize => resize.Columns(true))
                    .Reorderable(reorder => reorder.Columns(true))
                    .ColumnMenu()
                    .Selectable(s => s.Mode(GridSelectionMode.Multiple))
                    .Events(events => events.Change("entSelChange"))
            )

现在,我需要从选定的行中获取 EntityVersionId 的值。但不知道怎么做。

这是我的 javascript 函数

$("#editEntity").click(function () {

    var entityGrid = $("#EntitesGrid").data("kendoGrid");

    // what should I do from here
});

更新:添加代码以循环所有行。

function loadPreviousEntityVersion() {

    alert("sdfsdfsdf");
    var entityGrid = $("#EntitesGrid").data("kendoGrid");
    var data = entityGrid.dataSource.data();

    for(var i = 0; i<data.length; i++) {
        var currentDataItem = data[i];
        alert(dataItem.EntityVersionId);

    }
}

【问题讨论】:

标签: grid kendo-ui


【解决方案1】:

一种方法是使用 Grid 的 select()dataItem() 方法。

在单选情况下,select() 将返回可以传递给dataItem() 的单行

var entityGrid = $("#EntitesGrid").data("kendoGrid");
var selectedItem = entityGrid.dataItem(entityGrid.select());
// selectedItem has EntityVersionId and the rest of your model

对于多行选择select() 将返回一个行数组。然后,您可以遍历数组,并且可以将各个行传递到网格的 dataItem()

var entityGrid = $("#EntitesGrid").data("kendoGrid");
var rows = entityGrid.select();
rows.each(function(index, row) {
  var selectedItem = entityGrid.dataItem(row);
  // selectedItem has EntityVersionId and the rest of your model
});

【讨论】:

  • 我想指出的最后一件事是 selectedItem 具有最终由 DataSource 获取的整个模型,包括用户控件不使用的属性。例如。即使它不是隐藏列,您的 EntityVersionId 也会存在。
  • 非常感谢,我还有一个问题。现在,如果您需要遍历所有行,而不仅仅是您选择的行。是否有类似 entityGrid.rows() 的东西来获取所有行。我在 api 中找不到它。
  • 如果您需要所有数据而不是文字 html 行,我什至不会打扰网格的 api。 dataItem() 所做的只是简化对下面数据源的直接访问,因此请直接使用它。 entityGrid.dataSource.data() 应该给你一切。 docs.kendoui.com/api/framework/datasource#data
  • 你好,我尝试使用你的代码,我需要使用选中的行作为另一个网格的数据源,可以直接使用吗?
【解决方案2】:

有更好的方法。我在使用 kendo angularJS 指令且网格没有 ID 的页面中使用它...

change: function (e) {
   var selectedDataItem = e != null ? e.sender.dataItem(e.sender.select()) : null;
}

【讨论】:

  • 这个问题是,如果你只是点击网格项目,它会触发事件,而它假设只有在真正发生变化时才做 ti。基本上按照“编辑”的功能
【解决方案3】:

我认为是否需要检查是否选择了任何行? 下面的代码会检查它:

var entityGrid = $("#EntitesGrid").data("kendoGrid");
            var selectedItem = entityGrid.dataItem(entityGrid.select());
            if (selectedItem != undefined)
                alert("The Row Is SELECTED");
            else
                alert("NO Row Is SELECTED")

【讨论】:

    【解决方案4】:

    如果您想选择特定元素,请使用以下代码

    var gridRowData = $("<your grid name>").data("kendoGrid");
    var selectedItem = gridRowData.dataItem(gridRowData.select());
    var quote = selectedItem["<column name>"];
    

    【讨论】:

      猜你喜欢
      • 2014-10-24
      • 2017-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 1970-01-01
      • 2018-08-12
      • 2017-08-18
      相关资源
      最近更新 更多