Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event called when sorting data in kendo grid

I have the sample codes as follows:

  $(document).ready(function () {
    $("#grid").kendoGrid({
        dataSource: getData(),
        height: 550,
        sortable: true,
        pageable: true,
        columns: [{
            field: "ContactName",
            title: "Contact Name",
            width: 200
        }, {
            field: "ContactTitle",
            title: "Contact Title"
        }, {
            field: "CompanyName",
            title: "Company Name"
        }]
    });
       function whenSorting(){
           //// DO SOMETIME......
       }
     });

Now what I want is when I do sorting of any field the function "whenSorting" will be called. How to do that?

like image 330
user3988645 Avatar asked Feb 12 '23 01:02

user3988645


2 Answers

You have local sorting enabled "sortable: true," , for this you can capture it with databound event

    $(document).ready(function () {
                    $("#grid").kendoGrid({
                        dataSource: getData(),
                        height: 550,
                        sortable: true,
                        pageable: true,
                        columns: [{
                            field: "ContactName",
                            title: "Contact Name",
                            width: 200
                        }, {
                            field: "ContactTitle",
                            title: "Contact Title"
                        }, {
                            field: "CompanyName",
                            title: "Company Name"
                        }],
                        dataBound: function(e) {
                          whenSorting();
                        }
                    });

                   function whenSorting(){
                       //// DO SOMETIME......
                   }
                 });

IF you are using server sorting you can handle it in the server read .

Hope this helps

like image 59
cwishva Avatar answered Mar 28 '23 20:03

cwishva


You may bind Change function and check whether sorting exist or not on every grid change

$('#grid').data('kendoGrid').dataSource.bind('change',function(){

    // Get the grid object
     var grid = $("#grid").data("kendoGrid");
    // Get the datasource bound to the grid
    var ds = grid.dataSource;
    // Get current sorting
    var sort = ds.sort();
    // Display sorting fields and direction
    if (sort) {
    for (var i = 0; i < sort.length; i++) {
        alert ("Field:" + sort[i].field + " direction:" + sort[i].dir);
    }
    } else {
    alert("no sorting");
   }           

});

i hope this will help you

like image 42
Abbas Galiyakotwala Avatar answered Mar 28 '23 21:03

Abbas Galiyakotwala