如何访问模型所选项目在剑道UI网格 [英] How can I access the model for the selected item in a Kendo UI Grid

查看:174
本文介绍了如何访问模型所选项目在剑道UI网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的ASP.NET MVC应用程序可选kendoUI网格。我如何获得所选项目的强类型的模型对象?我曾尝试以下code,但它不能正常工作。

  @(Html.Kendo()网格和LT;备份>(Model.Backups)
    。名称(MatchingBackupsGrid)
    .Columns(COL =>
        {
            col.Bound(备份= GT; backup.BackupUId).title伪(UID);
            col.Bound(备份= GT; backup.BackupFirstName).title伪(名);
            col.Bound(备份= GT; backup.BackupLastName).title伪(姓氏);
        })
        .Scrollable()
        .Selectable(SEL =>
        {
            sel.Mode(GridSelectionMode.Single);
            sel.Type(GridSelectionType.Row);
        })
         .DataSource(数据源=>数据源
            。服务器()
            .Read(读=> read.Action(SearchForBackup,编曲,新的{lastName的= Model.SearchTerm}))
            。型号(型号=> model.Id(备份= GT; backup.BackupUId))
         )
    )功能SelectBackupButtonClickHandler(){    VAR电网= $(#MatchingBackupsGrid)的数据(kendoGrid);    变种selectedBackup = grid.dataItem(grid.select());    的console.log(selectedBackup);
}


解决方案

我不知道那是什么,我前面做的时候,我的code现在正在工作。

我试过很多东西 - 我认为由于某种原因 grid.select()返回一个数组,所以我想申请一个索引来获得的第一个元素像这样的数组:

  VAR selectedBackups = grid.select();//奇怪的是,在Visual Studio智能感知误导
//我在这里,因为它在向我展示了模特属性
//索引这个数组
selectedBackups [0] .BackupFirstName;

我也试过像这样的阵列上的转变:

  VAR名= $ .MAP(grid.select()函数(项目)
{
    返回item.BackupFirstName +''+ item.BackupLastName;
});

我在这么多的想法我是想,我忘了检查了我原来的code的没有的居然有选择行的强类型模型之间。

所以,仅仅是为了记录,在问题code是正确的,并且它转载如下。为了获得所选择的项目在单一选择模式KendoUI网格,做到以下几点:

  VAR电网= $(#MatchingBackupsGrid)的数据(kendoGrid);//这是一个单一的对象,而不是一个阵列
//这是你的强类型模型对象
变种selectedBackup = grid.dataItem(grid.select());的console.log(selectedBackup);

I have a selectable kendoUI grid in my ASP.NET MVC app. How do I get the strongly typed model object for the selected item? I have tried the following code but it doesn't work.

@(Html.Kendo().Grid<Backup>(Model.Backups)
    .Name("MatchingBackupsGrid")
    .Columns(col =>
        {
            col.Bound(backup => backup.BackupUId).Title("UID");
            col.Bound(backup => backup.BackupFirstName).Title("First Name");
            col.Bound(backup => backup.BackupLastName).Title("Last Name");
        })
        .Scrollable()
        .Selectable(sel =>
        {
            sel.Mode(GridSelectionMode.Single);
            sel.Type(GridSelectionType.Row);
        })
         .DataSource(dataSource => dataSource
            .Server()
            .Read(read => read.Action("SearchForBackup", "Arr", new { lastName = Model.SearchTerm }))
            .Model(model => model.Id(backup => backup.BackupUId))
         )
    )

function SelectBackupButtonClickHandler() {

    var grid = $("#MatchingBackupsGrid").data("kendoGrid");

    var selectedBackup = grid.dataItem(grid.select());

    console.log(selectedBackup);
}

解决方案

I have no idea what it was that I was doing earlier, my code is working now.

I tried so many things -- I assumed for some reason that grid.select() returns an array, so I tried to apply an indexer to get the first element of the array like so:

var selectedBackups = grid.select();

// strangely, the Visual Studio intellisense mislead 
// me here as it showed me the model properties on 
// indexing this array
selectedBackups[0].BackupFirstName;

I also tried a transformation on the array like so:

var names = $.map(grid.select(), function(item)
{
    return item.BackupFirstName + ' ' + item.BackupLastName;
});

I was in between so many ideas I was trying that I forgot to check that my original code did actually have a strongly typed model of the selected row.

So, just for the record, the code in the question is correct, and it is reproduced below. To get the selected item in a single selection mode KendoUI grid, do the following:

var grid = $("#MatchingBackupsGrid").data("kendoGrid");

// this is a single object and not an array
// this is your strongly typed model object
var selectedBackup = grid.dataItem(grid.select());

console.log(selectedBackup);

这篇关于如何访问模型所选项目在剑道UI网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆