Javascript 以编程方式选择剑道网格行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33138163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:41:04  来源:igfitidea点击:

Select programmatically Kendo grid row

javascriptjquerykendo-ui

提问by kat1330

I found posts with similar titles but I still cannot resolve my issue. Definitely I am doing something wrong.

我找到了类似标题的帖子,但我仍然无法解决我的问题。我肯定做错了什么。

In Kendo grid configuration have some function which take context (grid) and read selected row:

在 Kendo 网格配置中,有一些函数可以获取上下文(网格)并读取选定的行:

change: function (e) {
            refresh(this);
        }

This is how I configured "change" event.

这就是我配置“更改”事件的方式。

In function "refresh(grid)" I am getting selected row on following way:

在函数“刷新(网格)”中,我通过以下方式获得选定的行:

    refresh: function (grid) {        
    var selectedRows = grid.select();
    var selectedRow = grid.dataItem(selectedRows[0]);
    var id = selectedRow.Id;
}

This approach works perfect when I select grid row manually. But when I select row programatically "selectedRow" variable is null.

当我手动选择网格行时,这种方法非常有效。但是当我以编程方式选择行时,“selectedRow”变量为空。

I am selecting programatically on following way:

我正在通过以下方式以编程方式选择:

var grid = $("#grid").data("kendoGrid"); 
var rows = grid.dataSource.data(); 
var row = rows[rows.length - 1]; 
grid.select(row);

As I sad in above, in previous "refresh(grid)" method variable selectedRow will be null.

正如我在上面难过的那样,在之前的“刷新(网格)”方法中,selectedRow 变量将为空。

Does anybody have some opinion about that? Why is it happened?

有人对此有什么看法吗?为什么会发生?

Thanks

谢谢

回答by Vladimir Iliev

According to the Grid documentation the "select"method accepts "string" parameter (selector) or jQuery element. That why if you need to correctly select the row you should modify your current code as follows:

根据 Grid 文档,“select”方法接受“string”参数(选择器)或 jQuery 元素。这就是为什么如果您需要正确选择行,您应该按如下方式修改当前代码:

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

//if you are using the "pageable" option of the grid
//you should get the visible rows using the .view() method
var models = grid.dataSource.data();

var model = models[models.length - 1]; 
var lastRowUid = model.uid;

//find the target row element:
var row = grid.table.find("[data-uid=" + lastRowUid + "]");

grid.select(row);