RadGrid for ASP .NET version |
Q1 2007 and later
|
RadControls for ASP .NET AJAX version |
2008.1.415 and later |
.NET version |
2.0 and later
|
Visual Studio version |
2005 and later
|
Programming language |
Javascript
|
Browser support |
all supported by RadGrid for ASP .NET
all browsers supported by RadControls for ASP .NET AJAX
|
PROJECT DESCRIPTION
The current project shows a RadGrid with a textbox in the command item template. Typing a letter in this textbox sets selected row in the grid to be the first found grid row that has a value in a designated column starting with the letter typed. The entire operation is done client-side using RadGrid client-side API.
Grid's
ClientSettings-->Selecting-->AllowRowSelect is set to true in order to have MasterTableView's Rows array available. Scrolling is enabled and the grid is scrolled down to the selected row, when set. Scrolling is based on the following help topic:
http://www.telerik.com/help/aspnet/grid/?grdScrollToTheSelectedItem.html.
In addition the column that provides the values which are checked for the starting letter is added to MasterTableView's DataKeyNames and
ClientSettings.EnableClientKeyValues is enabled in order these values to be accessible on the client. The essential javascript code follows:
var grid; |
var scrollArea; |
function GridCreated() |
{ |
grid = this; |
scrollArea = document.getElementById(this.ClientID + "_GridData"); |
} |
function KeyPressed(e) |
{ |
var keynum |
var keychar |
|
keynum = e.which || e.keyCode |
keychar = String.fromCharCode(keynum) |
|
var rownum = grid.MasterTableView.Rows.length; |
for (var i = 0; i < rownum; i++) |
{ |
if (grid.MasterTableView.Rows[i].KeyValues["ProductName"].indexOf(keychar) == 0) |
{ |
grid.MasterTableView.SelectRow(grid.MasterTableView.Rows[i].Control, true); |
|
//scroll to the selected row after the grid updates its display |
window.setTimeout(function(){ |
Scroll(grid.MasterTableView.Rows[i]); |
}, 10); |
break; |
} |
} |
|
var textBox = e.srcElement || e.target; |
textBox.blur(); |
return true; |
} |
function Scroll(row) |
{ |
//if the position of the selected row is below the viewable grid area |
if((row.Control.offsetTop - scrollArea.scrollTop) + row.Control.offsetHeight + 20 > scrollArea.offsetHeight) |
{ |
//scroll down to selected row |
scrollArea.scrollTop = scrollArea.scrollTop + ((row.Control.offsetTop - scrollArea.scrollTop) + row.Control.offsetHeight - scrollArea.offsetHeight) + row.Control.offsetHeight; |
} |
//if the position of the the selected row is above the viewable grid area |
else if((row.Control.offsetTop - scrollArea.scrollTop) < 0) |
{ |
//scroll the selected row to the top |
scrollArea.scrollTop = row.Control.offsetTop; |
} |
} |