1 Answer, 1 is accepted
You can programmatically sort Kendo UI Grid using its DataSource's sort() method. For example:
//get reference to the Grid widget
var
grid = $(
"#grid"
).data(
"kendoGrid"
);
//sort Grid's dataSource
grid.dataSource.sort({field:
"SomeField"
, dir:
"asc"
});
Regards,
Iliana Nikolovathe Telerik team
That didn't work for me. I was getting undefined references to the datasource of the grid. Instead, setting it in the datasource directly worked.
$("#Grid").data("kendoGrid").dataSource.sort({ field: "name", dir: "desc" });
is not firing sort function in attached to grid
sort: function (e) { console.log("my sort event fired") }
The Grid sort event will be triggered when the user interacts with the UI. If the sorting is changed programmatically the event will not be raised.
Regards,
Viktor Tachev
Progress Telerik
Should this work as well?
//get reference to the Grid widget
var grid = $("#grid").data("kendoGrid");
//sort Grid's dataSource
grid.dataSource.sort({field: "SomeField", dir: "asc"}, {field: "SomeOtherField", dir: "desc"});
...because it doesn't.
The it is sorted by the first field, but not the second.
Hi Bob,
When you would like to sort the data by multiple fields you need to pass an array to the DataSource sort method. The code would look like this:
var grid = $("#grid").data("kendoGrid");
grid.dataSource.sort([{field: "SomeField", dir: "asc"}, {field: "SomeOtherField", dir: "desc"}]);
Regards,
Viktor Tachev
Progress Telerik
I would like to order my items by day count descending. Either client or server side,I would like to see them in this order: 9,8,7,6,5,4,3,2,1. Instead, on page one I get 4,3,2,1 and on page two 9,8,7,6,5,4.
What am I doing wrong?
Hi Stephen,
I modified one of the Grid examples using the suggestion provided below:
var grid = $("#grid").data("kendoGrid");
grid.dataSource.sort([{field: "SomeField", dir: "asc"}, {field: "SomeOtherField", dir: "desc"}]);
You could find it in the dojo here. In the example, the Freight column is sorted after widget initialization. Could you modify the example so the behavior is reproducible and send it back for me to review? This way I could investigate the scenario and provide a possible resolution for the issue observed.
Regards,
Aleksandar
Progress Telerik