Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 981
kat1330 Avatar asked Oct 15 '15 01:10

kat1330


People also ask

How do I select a row in kendo grid programmatically?

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.

How do I select a row in jqGrid programmatically?

UPDATE: Free jqGrid supports multiPageSelection:true option strarting with the version 4.10. 0. The option allows to set selection of multiple rows in the grid very easy (and it works very quickly, because it set selection state directly during creating the body of the grid).

How do I get the last row in kendo grid?

To get the dataItem for the last selected row: Subscribe for the click event of the checkboxes by using a jQuery selector. In the click event handler, get the row by using the closest jQuery method. Get the row data by using the dataItem method of the Grid.


1 Answers

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);
like image 165
Vladimir Iliev Avatar answered Sep 25 '22 13:09

Vladimir Iliev