How to implement MVC Kendo grid with select all check box and maintain the states across paging & sorting

2 posts, 0 answers
  1. Laksh
    Laksh avatar
    25 posts
    Member since:
    Mar 2011

    Posted 30 Jun 2015 Link to this post

    Requirements:

    1>I am using MVC kendo grid and I am trying to add select all/deselect check box to the header. I have javascript which does the logic of selecting & deselecting all the individual check boxes. I also want to select/deselect the header check box based on check boxes in the rows. Since during grid initialization grid rows are unknown, we cannot attach click event to the individual check boxes in the rows. we have to do that after data is bound to the grid.

    2> I also wanted to maintain the state of the check boxes when user navigate through pages or do sorting. ( because of this paging & sorting is done on client side. Server operation is set to false) The javascript is working correctly.

    Issues:

    1> I’m calling this javascript in dataBound event of the grid as suggested here by telerik support. However dataBound event is getting fired on each page change and sort. As per the documentation here it should get fired when widget is bound to dataSource. So I tried dataSource’s requestEnd event, but requestEnd event is also getting fired on each page change & sort. AGAIN, as per the documentation here it should only get fire when remote service request is finished.

    2>I also noticed, when I view the page source in browser the grid has rows only for that particular page. So my javascript only finds check boxes for that page only.

    So how do I implement select all check box column that will select all the check boxes and also maintain the states across paging and sorting? can someone please show some examples

    $(function () {
     
     
        $gridElement = $("#grid");
        var kendoGrid = $gridElement.data("kendoGrid");
        var dataSource = kendoGrid.dataSource;
         
        kendoGrid.bind("dataBound", function (e)
            {
                configureSelectCheckBoxes($gridElement)
            }
        )
     
        $("#btnSearch").click(function () {
            kendoGrid.dataSource.read();
            kendoGrid.refresh();
            kendoGrid.dataSource.page(1);
        })
     
         
        function configureSelectCheckBoxes(grid) {
            // attach click event to select all check box
            grid.find("thead input:checkbox").click(
                function () {
                    grid.find("td input:checkbox").prop("checked", $(this).prop("checked"));
                }
             );
     
            // attach click event to individual check box
            grid.find("tbody input:checkbox").click(
                   function () {
                       var checkedCount = grid.find("td input:checkbox:checked").length;
                       var totalCount = grid.find("td input:checkbox").length;
                       grid.find("th input:checkbox").prop("checked", checkedCount === totalCount);
                   }
            );
        }
    })

     

    @(Html.Kendo().Grid<MVCPOC.Models.MyModel>()
                .Name("grid")
                .Columns(col =>
                {
                    col.Bound(p => p.ModelID)
                        .ClientTemplate("<input class='chkbox' type='checkbox' value='#=ModelID#' />")
                        .HeaderTemplate("<input type='checkbox' id='selectAll' />")
                        .Width(30)
                        .Sortable(false)
                        .Filterable(false);               
                    col.Bound(p => p.StateProvinceCode);               
                    col.Bound(p => p.EmailAddress);
                })
                .AutoBind(false)
                .Pageable()
                .Sortable()
                .Scrollable(x => x.Height(300))
                .HtmlAttributes(new { style = "height:500px" })
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .ServerOperation(false)
                    .PageSize(20)
                    .Read(read => read
                        .Action("GetData", "Grid")))
     
            )

     

     

     

  2. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 03 Jul 2015 Link to this post

    Hi Laksh,

    "Select All" functionality is not supported out of the box. That's why you will have to implement your own logic.

    Regarding your first issue. Try to modify the client template as follows:

    .ClientTemplate("<input class='chkbox' type='checkbox' value='#= IsAdmin ? checked='checked':'' #' onclick='rowCheck(this)' />")

    In this code you can attach the click event handler and retrieve the boolean value from the model.

    Regarding your second question. In your code you are using server paging. That's why the rows of the other pages does not exist until you change the current page. Instead try using client paging.

    Here is a sample project that might help you - Checkboxes batch editing
     
    Regards,
    Hristo Valyavicharski
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top