This is a migrated thread and some comments may be shown as answers.

[Solved] Get index in data source of selected row

1 Answer 387 Views
Grid for HTML
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Chris
Top achievements
Rank 1
Chris asked on 27 Mar 2013, 01:24 AM
How can I determine the index of the selected row in the grid's data source?

For example I've setup the grid list this

var data = [
    { Name: "One", Data: 1 },
    { Name: "Two", Data: 2 },
    { Name: "Three", Data: 3 }
];
var list = new WinJS.Binding.List(data);
  
var grid = new Telerik.UI.RadGrid(grid1, {
    dataSource: list
});

and I've got the current selection 
var dataItem = grid.dataItem(grid.selection[0]);

I want to determine the corresponding index in the WinJS.Binding.List so that I can update the correct record.

Thanks
Chris

1 Answer, 1 is accepted

Sort by
0
Accepted
Tsvetina
Telerik team
answered on 27 Mar 2013, 02:48 PM
Hello Chris,

Usually, when editing data, you would have  a unique value in each data record, usually an ID. If you add one to your data array, locating the edited item would be easy, by calling a filter function on the WinJS.Binding.List. For example, if you have the ID of a currently updated item, you could do the following:
var itemIndex;
 
var editedItem = list.filter(function (item, index) {
    if (item.ID == id) {
        itemIndex = index;
        return item;
    }
})[0];

For your convenience, I have prepared a small sample, where I do external editing of RadGrid. To retrieve the ID of the currently edited item, I output it in a hidden field in the edit form, shown below the grid.


Regards,
Tsvetina
the Telerik team
Have a suggestion or face a problem - you can use the Ideas & Feedback portal to submit ideas, feedback and vote for them.
Tags
Grid for HTML
Asked by
Chris
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Share this question
or