This article is an example of a Kendo Grid row selection issue. The framework used is Kendo UI for jQuery. Let's begin.
In the persist row selection in this knowledge base article, https://docs.telerik.com/kendo-ui/knowledge-base/persist-row-selection-while-paging, it chooses the wrong rows after choosing the page and going back then Shift+Click a row. Below is the code from the knowledge base article.
<!-- https://docs.telerik.com/kendo-ui/knowledge-base/persist-row-selection-while-paging --> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Kendo UI Snippet</title> | |
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2021.3.914/styles/kendo.default-v2.min.css"/> | |
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> | |
<script src="https://kendo.cdn.telerik.com/2021.3.914/js/kendo.all.min.js"></script> | |
</head> | |
<body> | |
<div id="grid"></div> | |
<script> | |
$(function () { | |
var selectedOrders = []; | |
var idField = "OrderID"; | |
$("#grid").kendoGrid({ | |
dataSource: { | |
type: "odata", | |
transport: { | |
read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders" | |
}, | |
schema: { | |
model: { | |
id: "OrderID", | |
fields: { | |
OrderID: { type: "number" }, | |
Freight: { type: "number" }, | |
ShipName: { type: "string" }, | |
OrderDate: { type: "date" }, | |
ShipCity: { type: "string" } | |
} | |
} | |
}, | |
pageSize: 10, | |
serverPaging: true, | |
serverFiltering: true, | |
serverSorting: true | |
}, | |
height: 400, | |
selectable: "multiple", | |
pageable: { | |
buttonCount: 5 | |
}, | |
sortable: true, | |
filterable: true, | |
navigatable: true, | |
columns: [ | |
{ | |
field: "ShipCountry", | |
title: "Ship Country", | |
width: 300 | |
}, | |
{ | |
field: "Freight", | |
width: 300 | |
}, | |
{ | |
field: "OrderDate", | |
title: "Order Date", | |
format: "{0:dd/MM/yyyy}" | |
} | |
], | |
change: function (e, args) { | |
var grid = e.sender; | |
var items = grid.items(); | |
items.each(function (idx, row) { | |
var idValue = grid.dataItem(row).get(idField); | |
if (row.className.indexOf("k-state-selected") >= 0) { | |
selectedOrders[idValue] = true; | |
} else if (selectedOrders[idValue]) { | |
delete selectedOrders[idValue]; | |
} | |
}); | |
}, | |
dataBound: function (e) { | |
var grid = e.sender; | |
var items = grid.items(); | |
var itemsToSelect = []; | |
items.each(function (idx, row) { | |
var dataItem = grid.dataItem(row); | |
if (selectedOrders[dataItem[idField]]) { | |
itemsToSelect.push(row); | |
} | |
}); | |
e.sender.select(itemsToSelect); | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Follow these steps to reproduce the row selection issue.
- On page 1, select Brazil (row 3). The Brazil row should be selected.
- Go to page 2 then back to page 1. Brazil is still selected. Well and good.
- Shift+Click Belgium, the selection now starts from row 1 to row 5 (France, Germany, Brazil, France, Belgium). Selected should be row 3 to 5 (Brazil, France, Belgium).
Kendo Grid Persist Row Selection Problem |
persistSelection
option. Since we have the persistSelection
, we no longer need to handle the change
event. On dataBound
, we find which rows were selected and set the focus to that row.
Here is our solution. You can edit the code below using any text editor but I prefer Visual Studio Code. Make sure you are connected to the Internet because the code pulls the Kendo library from the Telerik CDN. Take note that this is just a demonstration code and you'll need to purchase a license if you are going to use Kendo UI for jQuery for more than just evaluation purposes.
<!-- https://jpllosa.blogspot.com --> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Kendo Grid Persist Row Selection Example</title> | |
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2021.3.914/styles/kendo.default-v2.min.css"/> | |
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script> | |
<script src="http://kendo.cdn.telerik.com/2021.2.616/js/kendo.all.min.js"></script> | |
</head> | |
<body> | |
<div id="grid"></div> | |
<script> | |
$(function () { | |
$("#grid").kendoGrid({ | |
dataSource: { | |
type: "odata", | |
transport: { | |
read: "http://demos.kendoui.com/service/Northwind.svc/Orders" | |
}, | |
schema: { | |
model: { | |
id: "OrderID", | |
fields: { | |
OrderID: { type: "number" }, | |
Freight: { type: "number" }, | |
ShipName: { type: "string" }, | |
OrderDate: { type: "date" }, | |
ShipCity: { type: "string" } | |
} | |
} | |
}, | |
pageSize: 10, | |
serverPaging: true, | |
serverFiltering: true, | |
serverSorting: true | |
}, | |
height: 400, | |
selectable: "multiple", | |
persistSelection: true, | |
pageable: { | |
buttonCount: 5 | |
}, | |
sortable: true, | |
filterable: true, | |
navigatable: true, | |
columns: [ | |
{ | |
field: "ShipCountry", | |
title: "Ship Country", | |
width: 300 | |
}, | |
{ | |
field: "Freight", | |
width: 300 | |
}, | |
{ | |
field: "OrderDate", | |
title: "Order Date", | |
format: "{0:dd/MM/yyyy}" | |
} | |
], | |
dataBound: function (e) { | |
var grid = e.sender; | |
var selectedRows = grid.select(); | |
if (selectedRows.length > 0) { | |
var lastRow = selectedRows[selectedRows.length - 1]; | |
grid.selectable._lastActive = lastRow; | |
grid.current($(lastRow).find("td:first")); | |
} | |
} | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Output:
Kendo Grid Persist Row Selection Solution |
There you have it. A quick and simple example of how to persist a row selection on a Kendo Grid.