Select programmatically Kendo grid row

久未见 提交于 2019-12-10 00:53:35

问题


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:

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.

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.

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

Thanks


回答1:


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:

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);


来源:https://stackoverflow.com/questions/33138163/select-programmatically-kendo-grid-row

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