Sorting a grid programmatically

1 Answer 2376 Views
Grid
John
Top achievements
Rank 1
John asked on 22 Mar 2013, 01:28 PM
Hello,

I couldn't see anything in the API docs.  Is there a way to programmatically sort a table after the grid has been rendered?  We'd like to show the default sort order

1 Answer, 1 is accepted

Sort by
0
Iliana Nikolova
Telerik team
answered on 22 Mar 2013, 01:48 PM
Hello John,

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 Nikolova
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
John
Top achievements
Rank 1
commented on 22 Mar 2013, 07:19 PM

Iliana,

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.
John
Top achievements
Rank 1
commented on 17 May 2018, 11:30 AM

$("#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") }

Viktor Tachev
Telerik team
commented on 21 May 2018, 07:50 AM

Hi John,

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
Try our brand new, jQuery-free Angular components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Bob
Top achievements
Rank 1
commented on 14 Aug 2019, 12:44 PM

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

Bob
Top achievements
Rank 1
commented on 14 Aug 2019, 12:46 PM

...because it doesn't.

The it is sorted by the first field, but not the second.

Viktor Tachev
Telerik team
commented on 16 Aug 2019, 08:23 AM

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Stephen
Top achievements
Rank 1
commented on 27 Feb 2020, 01:26 PM

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?

 

Aleksandar
Telerik team
commented on 02 Mar 2020, 07:36 AM

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

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Iliana Nikolova
Telerik team
Share this question
or