Persisting the Selected Rows Client-side on Sorting/Paging/Filtering/Grouping
RadGrid loses its current selection on postback - e.g. when the data is sorted, a new group or filter is added, or when the current page changes. The following snippets illustrate how you can persist the selection client-side.
The following logic is used to persist the selected rows:
-
Handle OnRowSelected and OnRowDeselected events for RadGrid. In the handlers you should update the collection of the selected items.
-
Handle OnRowCreated event. In this handler you should check if the current item is present in the selected rows collection and select it if necessary.
Below you can see two approaches for implementing the functionality.
Using Web Storage
For modern browsers that support it you can use WebStorage to persist the selected items.
function rowSelected(sender, args) {
var id = args.getDataKeyValue("ID");
var selected = (JSON.parse(sessionStorage.getItem("selectedItems")) != null) ? JSON.parse(sessionStorage.getItem("selectedItems")) : {};
if (!selected[id]) {
selected[id] = true;
sessionStorage.setItem("selectedItems", JSON.stringify(selected));
}
}
function rowDeselected(sender, args) {
var id = args.getDataKeyValue("ID");
var selected = JSON.parse(sessionStorage.getItem("selectedItems"));
if (selected[id]) {
selected[id] = null;
sessionStorage.setItem("selectedItems", JSON.stringify(selected));
}
}
function rowCreated(sender, args) {
var id = args.getDataKeyValue("ID");
var selected = JSON.parse(sessionStorage.getItem("selectedItems"));
if (selected && selected[id]) {
args.get_gridDataItem().set_selected(true);
}
}
function gridCreated(sender, eventArgs) {
var masterTable = sender.get_masterTableView();
var selectColumn = masterTable.getColumnByUniqueName("ClientSelectColumnName");
var headerCheckBox = $(selectColumn.get_element()).find("[type=checkbox]")[0];
if (headerCheckBox) {
headerCheckBox.checked = masterTable.get_selectedItems().length ==
masterTable.get_dataItems().length;
}
}
Using JavaScript array
For older browsers you can use a static JavaScript array to keep the selected items. Note that AJAX is enabled and the EnablePageHeadUpdate property of RadAjaxManager is set to false.
<head id="Head2" runat="server">
<telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
<script type="text/javascript">
var selected = {};
function rowSelected(sender, args) {
var mailID = args.getDataKeyValue("MailID");
if (!selected[mailID]) {
selected[mailID] = true;
}
}
function rowDeselected(sender, args) {
var mailID = args.getDataKeyValue("MailID");
if (selected[mailID]) {
selected[mailID] = null;
}
}
function rowCreated(sender, args) {
var mailID = args.getDataKeyValue("MailID");
if (selected[mailID]) {
args.get_gridDataItem().set_selected(true);
}
}
function gridCreated(sender, eventArgs) {
var masterTable = sender.get_masterTableView();
var selectColumn = masterTable.getColumnByUniqueName("SelectColumnID");
var headerCheckBox = $(selectColumn.get_element()).find("[type=checkbox]")[0];
if (headerCheckBox) {
headerCheckBox.checked = masterTable.get_selectedItems().length ==
masterTable.get_dataItems().length;
}
}
</script>
</telerik:RadScriptBlock>
</head>