Introduction

In this article, I am going to discuss how to integrate the Kendo Sortable control in the Grid so that we can drag and drop the records within a Grid.

Remote Data Source for Kendo Grid

I am going to use the API response given below to construct my remote data source for Kendo Grid which was created in my previous article.

API - http://github-ci-staging.azurewebsites.net/api/Technologies/TechnologiesList

Type - GET.

Testing the APIs, using POSTMAN.

JQuery
Figure 1

Create a new HTML page. In my case, I named it KendoSortableGrid.cshtml.

KendoSortableGrid.cshtml

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <base href="http://demos.telerik.com/kendo-ui/sortable/integration-grid">
  5. <style>
  6. html {
  7. font-size: 14px;
  8. font-family: Arial, Helvetica, sans-serif;
  9. }
  10. </style>
  11. <title></title>
  12. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.common-material.min.css" />
  13. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.material.min.css" />
  14. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.material.mobile.min.css" />
  15. <script src="https://kendo.cdn.telerik.com/2017.3.913/js/jquery.min.js"></script>
  16. <script src="https://kendo.cdn.telerik.com/2017.3.913/js/kendo.all.min.js"></script>
  17. </head>
  18. <body>
  19. <div id="example">
  20. <div id="grid"></div>
  21. <script>
  22. $(document).ready(function() {
  23. var grid = $("#grid").kendoGrid({
  24. dataSource: {
  25. type: "json",
  26. transport: {
  27. read: "http://github-ci-staging.azurewebsites.net/api/Technologies/TechnologiesList"
  28. },
  29. schema: {
  30. model: {
  31. fields: {
  32. value: { type: "number" },
  33. text: { type: "string" },
  34. }
  35. }
  36. },
  37. },
  38. scrollable: true,
  39. columns: [
  40. { field: "value", title: "S No", width: "130px" },
  41. { field: "text", title: "Technology", width: "130px" },
  42. ]
  43. }).data("kendoGrid");
  44. });
  45. </script>
  46. </div>
  47. </body>
  48. </html>

From the code given above, it is obvious that we have constructed a data source for the Kendo Grid using our API, i.e., http://github-ci-staging.azurewebsites.net/api/Technologies/TechnologiesList.

Kendo Grid in Browser

JQuery
Figure 2

Kendo Sortable

Kendo Sortable makes the DOM element sortable by drag and drop with a mouse or a finger in mobile device.

Integrating Kendo Sortable in Kendo Grid

KendoSortableGrid.cshtml

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <base href="http://demos.telerik.com/kendo-ui/sortable/integration-grid">
  5. <style>
  6. html {
  7. font-size: 14px;
  8. font-family: Arial, Helvetica, sans-serif;
  9. }
  10. </style>
  11. <title></title>
  12. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.common-material.min.css" />
  13. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.material.min.css" />
  14. <link rel="stylesheet" href="https://kendo.cdn.telerik.com/2017.3.913/styles/kendo.material.mobile.min.css" />
  15. <script src="https://kendo.cdn.telerik.com/2017.3.913/js/jquery.min.js"></script>
  16. <script src="https://kendo.cdn.telerik.com/2017.3.913/js/kendo.all.min.js"></script>
  17. </head>
  18. <body>
  19. <div id="example">
  20. <div id="grid"></div>
  21. <script>
  22. $(document).ready(function() {
  23. var grid = $("#grid").kendoGrid({
  24. dataSource: {
  25. type: "json",
  26. transport: {
  27. read: "http://github-ci-staging.azurewebsites.net/api/Technologies/TechnologiesList"
  28. },
  29. schema: {
  30. model: {
  31. fields: {
  32. value: { type: "number" },
  33. text: { type: "string" },
  34. }
  35. }
  36. },
  37. },
  38. scrollable: true,
  39. columns: [
  40. { field: "value", title: "S No", width: "130px" },
  41. { field: "text", title: "Technology", width: "130px" },
  42. ]
  43. }).data("kendoGrid");
  44. grid.table.kendoSortable({
  45. filter: ">tbody >tr",
  46. hint: $.noop,
  47. cursor: "move",
  48. autoScroll: true,
  49. placeholder: function(element) {
  50. return element.clone().addClass("k-state-hover").css("opacity", 0.65);
  51. },
  52. container: "#grid tbody",
  53. change: function(e) {
  54. var skip = grid.dataSource.skip(),
  55. oldIndex = e.oldIndex + skip,
  56. newIndex = e.newIndex + skip,
  57. data = grid.dataSource.data(),
  58. dataItem = grid.dataSource.getByUid(e.item.data("uid"));
  59. grid.dataSource.remove(dataItem);
  60. grid.dataSource.insert(newIndex, dataItem);
  61. }
  62. });
  63. });
  64. </script>
  65. <style>
  66. .k-grid tbody tr {
  67. cursor: move;
  68. }
  69. </style>
  70. </div>
  71. </body>
  72. </html>

"Change event" definition of the Kendo Sortable is used to reorder the index of the data item which is in Grid data source.

  • Filter - This determines which items are sortable
  • Container - Determines to which boundaries the hint movement will be constrained
  • autoScroll - To enable scrolling when there is drag and drop within scrollable Grid
Result in Browser

After reordering the records by drag and drop using mouse,

JQuery
Figure 3

I hope, you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.

References

http://docs.telerik.com/kendo-ui/controls/interactivity/sortable/overview

Download the source code here.