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

Get selected row in server binding

3 Answers 205 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Vince
Top achievements
Rank 1
Vince asked on 05 May 2013, 01:54 AM
Hi, I trying to add a custom delete command on the toolbar and I need to get the selected row and data. I'm using Server binding and I tried using some of the answers in the forum but it still didn't work.

function deleteCommand(e) {
    var grid = $('#reimbursementsGrid').data('kendoGrid');
    var rows = grid.select();
    rows.each(
        function () {
            var record = $(this).data();
            alert('Selected : ' + record.Id);
 
        }
    )
}
The alert is always showing undefined. Can someone please point me in the right direction please?

The Id is defined in the model and is a hidden column
@(Html.Kendo().Grid(Model)
        .Name("reimbursementsGrid")
        .DataSource(dataSource => dataSource
            .Server()
            .Model(model =>
            {
                model.Id(r => r.Id);

...
        .Columns(columns =>
        {
            columns.Bound(o => o.Id).Hidden(true);
...

3 Answers, 1 is accepted

Sort by
0
Vince
Top achievements
Rank 1
answered on 06 May 2013, 10:21 PM
Finally figured it out after searching like crazy. Here is my solution if others find it helpful.

function deleteCommand(e) {
    var grid = $('#reimbursementsGrid').data('kendoGrid');
    var selectedRows = grid.select();
    var selected = $.map(selectedRows, function(item) {
                var checked = $(item).find('.check-box').attr('checked');
                return {
                    id: $(item).find(':nth-child(1)').text(),
                    expDate: $(item).find(':nth-child(2)').text(),
                    category: $(item).find(':nth-child(3)').text(),
                    amount: $(item).find(':nth-child(4)').text(),
                    miles: $(item).find(':nth-child(5)').text(),
                    submitted:  checked === "checked" ? true : false
                };
            });
    console.log( selected );
    console.log( selected[0].id );
    console.log( selected[0].category );
    console.log( selected[0].amount );
    console.log( selected[0].submitted );
}
I'm not sure if its the preferred way but its working. I also had a checkbox column which I wanted to find out also if it was checked which is in the above solution in case others need as had to search to find this too.
0
Galen Giebler
Top achievements
Rank 1
answered on 19 May 2013, 09:30 PM
THANKS SO MUCH for posting your solution.  You saved me from starting over and rewriting my solution!
Cheers!
0
Stu
Top achievements
Rank 1
answered on 20 Apr 2015, 09:36 PM
Thanks very much for this solution.  I've been working on this for a couple days and your code worked great for me.
Tags
Grid
Asked by
Vince
Top achievements
Rank 1
Answers by
Vince
Top achievements
Rank 1
Galen Giebler
Top achievements
Rank 1
Stu
Top achievements
Rank 1
Share this question
or