7
votes

I currently have a Kendo-UI grid. It has a few column where user can sort on, works great. I also have a details link on each row, so if the user clicks on this they are taken to a details page. I need to pass the current sort into the details page as a value. How can I get the current sort? is there an event I can bind to? Thanks

2
If the link is what triggers showing the details, why do not get current sort on there? - OnaBai
but how do I know which column sort has been clicked? if its descending or ascending? ie If I have 4 columns. I sort on Column 3 desc. how do I know that? - user2206329
Please, check my answer - OnaBai

2 Answers

12
votes

You can get the sorting configuration whenever you want using sort method.

Example: Being grid the id of your Grid. Do:

// 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");
}
1
votes

I ran into this need today and learned that the event is now present as of 2016 R3 release (2016.3.914).

Example usage:

<div id="grid"></div>
<script>
  $("#grid").kendoGrid({
    columns: [
      { field: "name" },
      { field: "age" }
 ],
dataSource: {
  data: [
        { id: 1, name: "Jane Doe", age: 30 },
        { id: 2, name: "John Doe", age: 33 }
     ],
     schema: {
       model: { id: "id" }
     }
   },
   sortable: true,
    sort: function(e) {
      console.log(e.sort.field);
      console.log(e.sort.dir);
    }
  });
</script>

See: http://docs.telerik.com/kendo-ui/api/javascript/ui/grid#events-sort