简体   繁体   中英

Select programmatically Kendo grid row

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM