kat1330
kat1330

Reputation: 5332

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

Upvotes: 19

Views: 35571

Answers (1)

Vladimir Iliev
Vladimir Iliev

Reputation: 1887

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

Upvotes: 30

Related Questions