Introduction
This blog tells you when to customize the sorting functionality in Kendo Grid and how to perform it.
Sorting in Kendo Grid
We can enable the sorting in Kendo Grid simply by setting the sortable property as true, as shown below.
KendoSorting.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>Kendo UI Snippet</title>
-
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.common.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.rtl.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.silver.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.mobile.all.min.css"/>
-
- <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.3.1028/js/kendo.all.min.js"></script>
- </head>
- <body>
-
- <div id="grid"></div>
- <script>
-
-
- var dataSource = new kendo.data.DataSource({
- data: [
- { id: 1, EmployeeName: "Saravanan", Salary:1100 },
- { id: 2, EmployeeName: "Atheek", Salary:1500 },
- { id: 3, EmployeeName: "Allywn", Salary:1000 }
- ]
- });
-
- $("#grid").kendoGrid({
- dataSource: dataSource,
- sortable: true,
- columns: [{
- Title:"Name",
- field: "EmployeeName",
-
- }
- },
- {field:"Salary", format: '{0:c}'}]
- });
-
- </script>
- </body>
- </html>
From the data shown above, the inbuilt sorting feature will work as expected.
Result
Consider The data given below is used to populate Kendo Grid.
- data: [
- { id: 1, item: "two" },
- { id: 2, item: "one" },
- { id: 3, item: "three" }
- ]
The item will be bound in Kendo Grid, as it was a string type. The sorting will be based on its type.
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>Kendo UI Snippet</title>
-
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.common.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.rtl.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.silver.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.mobile.all.min.css"/>
-
- <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.3.1028/js/kendo.all.min.js"></script>
- </head>
- <body>
-
- <div id="grid"></div>
- <script>
-
- var dataSource = new kendo.data.DataSource({
- data: [
- { Id: 1, item: "two" },
- { Id: 2, item: "one" },
- { Id: 3, item: "three" }
- ]
- });
-
- $("#grid").kendoGrid({
- dataSource: dataSource,
- sortable: true,
- columns: [{
- field: "item",
-
- }]
- });
-
- </script>
- </body>
- </html>
Result
In some cases, as per the requirement, we need to customize this sorting, where it should not happen based on type, so we need to write our own customized function to make the sorting, as we expect, which can be easily done by a comparison function
KendoSorting.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8"/>
- <title>Kendo UI Snippet</title>
-
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.common.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.rtl.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.silver.min.css"/>
- <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1028/styles/kendo.mobile.all.min.css"/>
-
- <script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>
- <script src="http://kendo.cdn.telerik.com/2016.3.1028/js/kendo.all.min.js"></script>
- </head>
- <body>
-
- <div id="grid"></div>
- <script>
- var numbers = {
- "one" : 1,
- "two" : 2,
- "three": 3
- };
-
- var dataSource = new kendo.data.DataSource({
- data: [
- { id: 1, item: "two" },
- { id: 2, item: "one" },
- { id: 3, item: "three" }
- ]
- });
-
- $("#grid").kendoGrid({
- dataSource: dataSource,
- sortable: true,
- columns: [{
- field: "item",
- sortable: {
- compare: function(a, b) {
-
- return numbers[a.item] - numbers[b.item];
- }
- }
-
- }]
- });
-
- </script>
- </body>
- </html>
Result
I hope you have enjoyed this blog. Your valuable feedback, questions or comments about this blog are always welcome.