We are facing an issue in telerik grid when we bind it using
the .BindTo() and set the .ServerOperation(false).
If we sort our grid or select the next page, it appears that the data source for the grid is still associated to the original data source and we lose the indexing in the grid.
As our grid also requires keyboard navigation through the arrow keys (selecting the down arrow should select the next row in the grid and selecting the up arrow should go to the previous row), we wrote some custom JavaScript to achieve that. (We know we can achieve the navigation also through selecting down or up arrow and spacebar, but due to business needs we want it to be a one key operation).However, we cannot accomplish this after a sort or page as the data source still points to original data source before sort and the selection jumps to the next index in the original data source instead of the adjacent row.
So our question is how to set the data source to point to the sorted data source?
We did try re-reading the data source and refreshing it but that points it all back to the original data source and the grid loses all its sorting or paging as well.
dataSource.read();
dataSource.refresh();
Here is the custom code we have in our JavaScript which is executed when a user clicks the arrow key to select the next row in the grid
function nextRow() {
var gridService = $(gridId).data("kendoGrid");
if (gridService !== undefined) {
var rowCount = gridService.dataSource.data().length;
if (rowCount !== undefined) {
var rows = gridService.items();
var currSelRowIndex = rows.index(gridService.select());
var nextRowIndex = rowCount - 1;
if (currSelRowIndex < rowCount - 1) {
nextRowIndex = currSelRowIndex + 1;
selectServiceLineRow(nextRowIndex, true);
}
else {
nextRowIndex = 0;
var pageService = gridService.dataSource.page();
var pageSizeService = gridService.dataSource.pageSize();
if ((pageService) * pageSizeService < gridService.dataSource.total()) {
gridService.dataSource.page(pageService + 1);
selectServiceLineRow(nextRowIndex, true);
}
}
}
}
}
function selectServiceLineRow(serviceLineRowIndex, withFocus) {
var gridService = $(gridId).data("kendoGrid");
var rowAt = gridService.dataSource.at(serviceLineRowIndex);
var rowuid = rowAt.uid;
var row = gridService.tbody.find("tr[data-uid='" + rowuid + "']");
gridService.select(row);
var cell = row.find('td:eq(0)');
gridService.current(cell);
if (withFocus) {
cell.focus();
}
serviceLinesGridCalcPrevNextButton(serviceLineRowIndex);
}
The grid’s datasource is bined in cshtml like this.
.DataSource(d => d
.Ajax()
.Events(e => e.Error("onWebApiError").Change("ResetGridScrollbar(\"#ServiceLinesGrid\")"))
//Remove .ServerOperation when paging is added
.ServerOperation(false)
.Model(m => m.Id(e => e.LineNumber)))
.BindTo(Model.ServiceLines)
If we sort our grid or select the next page, it appears that the data source for the grid is still associated to the original data source and we lose the indexing in the grid.
As our grid also requires keyboard navigation through the arrow keys (selecting the down arrow should select the next row in the grid and selecting the up arrow should go to the previous row), we wrote some custom JavaScript to achieve that. (We know we can achieve the navigation also through selecting down or up arrow and spacebar, but due to business needs we want it to be a one key operation).However, we cannot accomplish this after a sort or page as the data source still points to original data source before sort and the selection jumps to the next index in the original data source instead of the adjacent row.
So our question is how to set the data source to point to the sorted data source?
We did try re-reading the data source and refreshing it but that points it all back to the original data source and the grid loses all its sorting or paging as well.
dataSource.read();
dataSource.refresh();
Here is the custom code we have in our JavaScript which is executed when a user clicks the arrow key to select the next row in the grid
function nextRow() {
var gridService = $(gridId).data("kendoGrid");
if (gridService !== undefined) {
var rowCount = gridService.dataSource.data().length;
if (rowCount !== undefined) {
var rows = gridService.items();
var currSelRowIndex = rows.index(gridService.select());
var nextRowIndex = rowCount - 1;
if (currSelRowIndex < rowCount - 1) {
nextRowIndex = currSelRowIndex + 1;
selectServiceLineRow(nextRowIndex, true);
}
else {
nextRowIndex = 0;
var pageService = gridService.dataSource.page();
var pageSizeService = gridService.dataSource.pageSize();
if ((pageService) * pageSizeService < gridService.dataSource.total()) {
gridService.dataSource.page(pageService + 1);
selectServiceLineRow(nextRowIndex, true);
}
}
}
}
}
function selectServiceLineRow(serviceLineRowIndex, withFocus) {
var gridService = $(gridId).data("kendoGrid");
var rowAt = gridService.dataSource.at(serviceLineRowIndex);
var rowuid = rowAt.uid;
var row = gridService.tbody.find("tr[data-uid='" + rowuid + "']");
gridService.select(row);
var cell = row.find('td:eq(0)');
gridService.current(cell);
if (withFocus) {
cell.focus();
}
serviceLinesGridCalcPrevNextButton(serviceLineRowIndex);
}
The grid’s datasource is bined in cshtml like this.
.DataSource(d => d
.Ajax()
.Events(e => e.Error("onWebApiError").Change("ResetGridScrollbar(\"#ServiceLinesGrid\")"))
//Remove .ServerOperation when paging is added
.ServerOperation(false)
.Model(m => m.Id(e => e.LineNumber)))
.BindTo(Model.ServiceLines)