Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. //Table keyboard navigation
  2. $('.datatable').on('keydown','td',function(e){
  3. var currCell = $(this);
  4. var row = $(this).closest('tr');
  5. if (e.which == 13 ) { // return
  6. if($(row).attr('onclick')){
  7. $(row).trigger("click");
  8. }else if($(row).find("td:first-child i.icon-plus").length > 0){
  9. $(row).find("td:first-child i.icon-plus").click();
  10. }else if($(row).find('a[href]').length > 0){
  11. $(row).find('a[href]').click();
  12. }else if($(row).find('.footnote').length > 0){
  13. $(row).find('.footnote').trigger('click');
  14. }
  15. }
  16.  
  17. var c = "";
  18. var tabables = $('a[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), *[tabindex]');
  19. if (e.which == 39) {
  20. // Right Arrow
  21. c = currCell.next();
  22. if(c.length == 0){
  23. c = currCell.closest('tr').next().find('td:first');
  24. if(c.length == 0){
  25. var index = tabables.index(this);
  26. c = tabables.eq(index + 1);
  27. }
  28. }
  29. } else if (e.which == 37) {
  30. // Left Arrow
  31. c = currCell.prev();
  32. if(c.length == 0){
  33. c = currCell.closest('tr').prev().find('td:last');
  34. if(c.length == 0){
  35. var index = tabables.index(this);
  36. c = tabables.eq(index - 1);
  37. }
  38. }
  39. } else if (e.which == 38) {
  40. // Up Arrow
  41. c = currCell.closest('tr').prev().find('td:eq(' +
  42. currCell.index() + ')');
  43. if(c.length == 0){
  44. var index = tabables.index(this);
  45. c = tabables.eq(index - 1);
  46. }
  47. } else if (e.which == 40) {
  48. // Down Arrow
  49. c = currCell.closest('tr').next().find('td:eq(' +
  50. currCell.index() + ')');
  51. if(c.length == 0){
  52. var index = tabables.index(this);
  53. c = tabables.eq(index + 1);
  54. }
  55. } else if (e.which == 9 && !e.shiftKey) {
  56. // Tab
  57. e.preventDefault();
  58. c = currCell.next();
  59. if(c.length == 0){
  60. c = currCell.closest('tr').next().find('td:first');
  61. if(c.length == 0){
  62. var index = tabables.index(this);
  63. c = tabables.eq(index + 1);
  64. }
  65. }
  66. } else if (e.which == 9 && e.shiftKey) {
  67. // Shift + Tab
  68. e.preventDefault();
  69. c = currCell.prev();
  70. if(c.length == 0){
  71. c = currCell.closest('tr').prev().find('td:last');
  72. if(c.length == 0){
  73. var index = tabables.index(this);
  74. c = tabables.eq(index - 1);
  75. }
  76. }
  77. }
  78.  
  79. // If we didn't hit a boundary, update the current cell
  80. if (c.length > 0) {
  81. currCell = c;
  82. currCell.focus();
  83. }
  84. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement