umais
umais

Reputation: 1422

How to select a row in kendo grid by data item ID?

I need to select a specific row in kendoGrid but NOT by data-uid (as data-uid is changed when the grid dataSource is loaded again) but by the row itemID. I saw posts but they only select the row by uid which is not what I needed, I actually need to restart the HTML5 application and when grid is loaded, a specific item should be selected. This is what I've been seeing

Demo: jsfiddle.net/rusev/qvKRk/3/

e.g. the object has OrderID as ID, and every time the grid is loaded, it will be the same, unlike uid, I want to know how will I be able to select a row with OrderID, instead of uid.

Upvotes: 10

Views: 45218

Answers (4)

Well, accordingly to what I have done (and worked for me), and even though the work around isn't the prettiest, set one more Column, with your model id and with ClientTemplate then create any html object (div in my case) inside it give it a html id of your id, so when ever you need it, you just have to go and look with something like:

    grid.dataItem($("td div#id").closest("tr"));

Because remember that the dataItem method is waiting for a selector then you get your selectedItem as regular one.

EDIT: I forgot to say, that you should (or could) use the style property

    display:none

If you don't want to display that col.

Upvotes: 0

Chance
Chance

Reputation: 57

Going along with what umais has mentioned, the better approach, since there is no built in functionality for this as of yet, would be to iterate through all the records to find the one you need. The function that I built will work even if there are pages of data. The only other way that I can think of doing this would be do do a secondary ajax call; But this works well. Note that i haven't tested it with more than 2000 records.

    var dataGrid = $("#GATIPS").data("kendoGrid").dataSource;
    var numOfRows = dataGrid.total();
    var currentPageSize = dataGrid.pageSize();
    dataGrid.pageSize(numOfRows);
    var dataGridData = dataGrid.data();
    for (var i = 0; i < numOfRows; i++) {
        if (dataGridData[i].uid == e)
            return dataGridData[i];
    }
    dataGrid.pageSize(currentPageSize); // reset the view

e is the UID. However this can be substituted for what ever variable you need just replace the check.

Upvotes: 2

Rui Martins
Rui Martins

Reputation: 2184

You cam mix row itemID and data.uid, I guess.

var grid = $("#Grid").data("kendoGrid");
var dataItem = $("#Grid").data("kendoGrid").dataSource.get(itemID);
var row = $("#Grid").data("kendoGrid").tbody.find("tr[data-uid='" + dataItem.uid + "']");

Upvotes: 35

umais
umais

Reputation: 1422

a work around that I managed to have, was to go through all rows and check which row model has that ID equal to the parameter, and then get that row data-uid and select the item through data-uid. It's working fine for me, since there were no suggestion, it's the better answer for now.

Upvotes: 1

Related Questions