Created
July 18, 2012 21:49
-
-
Save JayDouglass/3139158 to your computer and use it in GitHub Desktop.
Spreadsheet behavior for gridview textboxes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(global, $){ | |
var keyCodes = { | |
13: 'enter', | |
38: 'up', | |
40: 'down', | |
37: 'left', | |
39: 'right' | |
}; | |
var spreadsheet = { | |
initialize: function(table) { | |
$(table).on('keydown', 'td input', function(e) { | |
var key = keyCodes.hasOwnProperty(e.which) ? keyCodes[e.which] : null; | |
if (key) { | |
spreadsheet[key](spreadsheet.getCurrentTdAndTr(this), e); | |
} | |
}); | |
}, | |
getCurrentTdAndTr: function(inputElement) { | |
var $inputElement = $(inputElement); | |
return { | |
$inputElement: $inputElement, | |
td: $inputElement.closest('td')[0], | |
tdIndex: $($inputElement.closest('td')[0]).index(), | |
tr: $inputElement.closest('tr')[0] | |
}; | |
}, | |
// when enter or down key is pressed in a textbox within a table cell, | |
// find the next table cell below it and set focus | |
down: function(cell) { | |
$(cell.tr).next('tr').children('td').eq(cell.tdIndex).find(':input').focus().select(); | |
}, | |
enter: function(cell, e) { | |
e.preventDefault(); | |
spreadsheet.down(cell); | |
}, | |
up: function(cell) { | |
$(cell.tr).prev('tr').children('td').eq(cell.tdIndex).find(':input').focus().select(); | |
}, | |
left: function(cell) { | |
// $(cell.td).prev('td').find(':input').focus().select(); | |
}, | |
right: function(cell) { | |
// $(cell.td).next('td').find(':input').focus().select(); | |
} | |
}; | |
global.spreadsheet = spreadsheet; | |
})(this, jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment