Hi,
I am trying to write table navigation using keys for asp.net gridview. This is what I have so far. It is working fine but it is slow. Please look at it and let me know how to improve the performance if possible.
Basically it is html table with input elements. The structure of table is like this
<table>
<tr>
<td>
<div style="height:22px;">
<input type = "text">
</div>
</td>
</tr>
<tr>
<td>
<div style="height:22px;">
<input type = "text">
</div>
</td>
</tr>
...
</table>
jQuery(function($) {
$('table#<%= myTable.ClientID %>')
.bind('keydown', funcKeyDown)
});
function funcKeyDown(event)
{
//get cell element.
var cell = event.target;
//get current cellIndex
var $cell = $(cell);
var currCell = $cell.parents("td");
var cellIndex = currCell[0].cellIndex;
//get current rowIndex
var currRow = $cell.parents("tr");
var rowIndex = currRow[0].rowIndex;
var nextRowIndex, targetElem = null;
var nextCell;
switch(event.keyCode) {
case 13: //enter key
if(shiftKeyPressed == 1){
//move left
if(!(cellIndex == 0)){
targetElem = currRow.children("td").eq(cellIndex-1).find("input[type=text]");
if(targetElem){targetElem.select();}}}
else {
//move right
if(!(cellIndex == (numElements -1))){
targetElem = currRow.children("td").eq(cellIndex+1).find("input[type=text]");
if(targetElem){
targetElem.select();
}
}
}
return false;
case 16: //shift key
shiftKeyPressed = 1;
return false;
case 33:
case 34: //page-up, page-down
if(event.keyCode == 33){
nextRowIndex = rowIndex - pageSize;
if(nextRowIndex < 0) nextRowIndex = 0;
}
if(event.keyCode == 34){
nextRowIndex = rowIndex + pageSize;
if(nextRowIndex > numRows) nextRowIndex = numRows;
}
targetElem = currRow.parent().children("tr").eq(nextRowIndex).children("td").eq(cellIndex)
.find("input[type=text]");
if(targetElem != null){targetElem.select();}
return false;
case 37: //left
if(!(cellIndex == 0)){
targetElem = currRow.children("td").eq(cellIndex-1).find("input[type=text]");
if(targetElem != null){targetElem.select();}}
return false;
case 38: //up
if(rowIndex != 0){
targetElem = currRow.parent().children("tr").eq(rowIndex-1).children("td").eq(cellIndex)
.find("input[type=text]");
if(targetElem != null){
targetElem.select();}}
return false;
case 39: //right arrow
if(!(cellIndex == (numElements -1))){
targetElem = currRow.children("td").eq(cellIndex+1).find("input[type=text]");
if(targetElem != null){targetElem.select();}}
return false;
case 40: //down
if(rowIndex >= 0){
targetElem = currRow.parent().children("tr").eq(rowIndex+1).children("td").eq(cellIndex)
.find("input[type=text]");
if(targetElem != null){targetElem.select();}}
return false;
}
}