2
votes

Not sure if I am doing this correctly. But I bind a kendo grid with array data of objects, where each object has a Boolean property (i.e. either true or false).

For the user I display it as a checkbox with the checked state showing the true state and vice versa. If you check something in the page 1; go to the page 3; check a few rows; and go back to page 1, the state of the checkbox defaults to how it was at the time it was loaded.

Here is the code snippet I've used.

$(function () {
    var ds = new kendo.data.DataSource({
        transport: {
            read: {
                url: '/echo/json/',
                type: 'POST',
                data: {
                    json: JSON.stringify(students)                    
                }
            }
        },
        pageSize: 5
    });

    var kgrid = $("#grid").kendoGrid({
        columns: [

        {
            field: 'name',
            title: 'Name'
        }, {
            field: 'age',
            title: 'Age'
        }, {
            field: 'place',
            title: 'Place'
        }, {
            field: 'hasCar',
            title: 'Owns Car',
            template: function (data) {
                return '<input type="checkbox" ' + (data.hasCar ? 'checked' : '') + '/>';
            }
        }],
        height: 240,
        pageable: true
    });

    $('#btnload').click(function () {
        var grid = kgrid.data('kendoGrid');
        grid.setDataSource(ds);
        $('#grid-display').show();
    });

    $('#btnrefresh').click(function(){
       ds.read(); 
       console.log('refresh'); 
    });

    $('#btnlist').click(function(){
        var res = $('#result');
        kgrid.find('tr td:last input:checked').each(function(i, e){
           var name = $(e).closest('tr').children().first().text();
           res.append('<p>' + name + '</p>'); 
           console.log('execute');           
        });
    });
});

How to persist the state on pagination?

Ps: fiddle to work with: http://jsfiddle.net/deostroll/2SGyV/3/

2

2 Answers

3
votes

That is because the changes you are doing stays there in the grid only - not in kendoDataSource. Here MVVM is not done. So you have to change appropriate value in dataSource each time you click the checkbox.

When you select another page in kendoGrid, it fetches data from dataSource, and then display it the grid. Unless and untill that dataSource is changed, you can't see the changes done in the grid.

PS: Had there is any field for Id in dataSource I would have updated jsfiddle myself

UPDATE

Check this Updated jsfiddle

I have updated template for checkbox

template: function (data) {
    return '<input type="checkbox" ' + (data.hasCar ? 'checked' : '') + ' data-name="'+ data.name + '"' +'/>';
}

Now checkbox will have name of the data. You can change it with id. On change event, update your dataSource accordingly on each checkbox click.

$('#grid-display input').live('change', function(){
    alert($(this).attr('data-name'));
    //update the data source here based on the data-name attribute u're getting
});
0
votes

Use databound event in the kendo Grid.

Not sure below code work will work for you or not. But similar code worked for me

function OnDataBoundEventMethod(e) {
    var view = this.dataSource.view();
    for (var i = 0; i < view.length; i++) {
        if ([view[i].hasCar]) {
            this.tbody.find("tr[data-uid='" + view[i].uid + "']")
                .addClass("k-state-selected")
                .find(".row-check-box")
                .attr("checked", "checked");
        }
    }
}