beforeKeyDown.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>beforeKeyDown callback - Handsontable</title>
  6. <!--
  7. Loading Handsontable (full distribution that includes all dependencies)
  8. -->
  9. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/handsontable.css">
  10. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="../dist/pikaday/pikaday.css">
  11. <script data-jsfiddle="common" src="../dist/pikaday/pikaday.js"></script>
  12. <script data-jsfiddle="common" src="../dist/moment/moment.js"></script>
  13. <script data-jsfiddle="common" src="../dist/zeroclipboard/ZeroClipboard.js"></script>
  14. <script data-jsfiddle="common" src="../dist/numbro/numbro.js"></script>
  15. <script data-jsfiddle="common" src="../dist/numbro/languages.js"></script>
  16. <script data-jsfiddle="common" src="../dist/handsontable.js"></script>
  17. <!--
  18. Loading demo dependencies. They are used here only to enhance the examples on this page
  19. -->
  20. <link data-jsfiddle="common" rel="stylesheet" media="screen" href="css/samples.css?20140331">
  21. <script src="js/samples.js"></script>
  22. <script src="js/highlight/highlight.pack.js"></script>
  23. <link rel="stylesheet" media="screen" href="js/highlight/styles/github.css">
  24. <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
  25. <!--
  26. Facebook open graph. Don't copy this to your project :)
  27. -->
  28. <meta property="og:title" content="beforeKeyDown callbacks">
  29. <meta property="og:description"
  30. content="">
  31. <meta property="og:url" content="http://handsontable.com/demo/beforeKeyDown.html">
  32. <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  33. <meta property="og:image:type" content="image/png">
  34. <meta property="og:image:width" content="409">
  35. <meta property="og:image:height" content="164">
  36. <link rel="canonical" href="http://handsontable.com/demo/beforeKeyDown.html">
  37. <!--
  38. Google Analytics for GitHub Page. Don't copy this to your project :)
  39. -->
  40. <script src="js/ga.js"></script>
  41. </head>
  42. <body>
  43. <div class="wrapper">
  44. <div class="wrapper-row">
  45. <div id="global-menu-clone">
  46. <h1><a href="../index.html">Handsontable</a></h1>
  47. </div>
  48. <div id="container">
  49. <div class="columnLayout">
  50. <div class="rowLayout">
  51. <div class="descLayout">
  52. <div class="pad" data-jsfiddle="example1">
  53. <a name="lazy"></a>
  54. <h2>beforeKeyDown callback</h2>
  55. <p>The following demo uses <code>beforeKeyDown</code> callback to modify some key bindings:</p>
  56. <ul>
  57. <li>Pressing DELETE or BACKSPACE on a cell deletes the cell and shifts all cells beneath it in the column up
  58. resulting in the cursor (which doesn't move) having the value previously beneath it, now in the current
  59. cell.
  60. </li>
  61. <li>Pressing ENTER in a cell (not changing the value) results in pushing all the cells in the column beneath
  62. this cell down one row (including current cell) resulting in a blank cell under the cursor (which hasn't
  63. moved)
  64. </li>
  65. </ul>
  66. <div id="example1"></div>
  67. <p>
  68. <button name="dump" data-dump="#example1" data-instance="hot" title="Prints current data source to Firebug/Chrome Dev Tools">
  69. Dump data to console
  70. </button>
  71. </p>
  72. </div>
  73. </div>
  74. <div class="codeLayout">
  75. <div class="pad">
  76. <div class="jsFiddle">
  77. <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
  78. </div>
  79. <script data-jsfiddle="example1">
  80. var data = [
  81. ['Nissan', 2009, 'black', 'black'],
  82. ['Nissan', 2006, 'blue', 'blue'],
  83. ['Chrysler', 2004, 'yellow', 'black'],
  84. ['Volvo', 2012, 'yellow', 'gray']
  85. ],
  86. container = document.getElementById("example1"),
  87. lastChange = null,
  88. hot;
  89. hot = new Handsontable(container, {
  90. data: data,
  91. colHeaders: true,
  92. rowHeaders: true,
  93. minSpareRows: 1,
  94. beforeChange: function (changes, source) {
  95. lastChange = changes;
  96. }
  97. });
  98. hot.updateSettings({
  99. beforeKeyDown: function (e) {
  100. var selection = hot.getSelected();
  101. // BACKSPACE or DELETE
  102. if (e.keyCode === 8 || e.keyCode === 46) {
  103. Handsontable.dom.stopImmediatePropagation(e);
  104. // remove data at cell, shift up
  105. hot.spliceCol(selection[1], selection[0], 1);
  106. e.preventDefault();
  107. }
  108. // ENTER
  109. else if (e.keyCode === 13) {
  110. // if last change affected a single cell and did not change it's values
  111. if (lastChange && lastChange.length === 1 && lastChange[0][2] == lastChange[0][3]) {
  112. Handsontable.dom.stopImmediatePropagation(e);
  113. hot.spliceCol(selection[1], selection[0], 0, ''); // add new cell
  114. hot.selectCell(selection[0], selection[1]); // select new cell
  115. }
  116. }
  117. lastChange = null;
  118. }
  119. }
  120. );
  121. </script>
  122. </div>
  123. </div>
  124. </div>
  125. <div class="footer-text">
  126. </div>
  127. </div>
  128. </div>
  129. </div>
  130. </div>
  131. <div id="outside-links-wrapper"></div>
  132. </body>
  133. </html>