prepopulate.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>Pre-populate new rows from template - 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="Pre-populate new rows from template - Handsontable">
  29. <meta property="og:description"
  30. content="Below example shows how Legend match function can be used to present the template values for empty rows">
  31. <meta property="og:url" content="http://handsontable.com/demo/prepopulate.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/prepopulate.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. <h2>Pre-populate new rows from template</h2>
  54. <p>Below example shows how cell renderers can be used to present the template values for empty rows.</p>
  55. <p>When any cell in the empty row is edited, the
  56. <b>onChange</b> callback fills the row with the template values.</p>
  57. <div id="example1"></div>
  58. <style>
  59. .placeholder {
  60. color: #777;
  61. font-style: italic;
  62. }
  63. </style>
  64. <p>
  65. <button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">
  66. Dump data to console
  67. </button>
  68. </p>
  69. </div>
  70. </div>
  71. <div class="codeLayout">
  72. <div class="pad">
  73. <div class="jsFiddle">
  74. <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
  75. </div>
  76. <script data-jsfiddle="example1">
  77. var
  78. tpl = ['one', 'two', 'three'],
  79. data = [
  80. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  81. ['2008', 10, 11, 12, 13],
  82. ['2009', 20, 11, 14, 13],
  83. ['2009', 30, 15, 12, 13]
  84. ],
  85. container = document.getElementById('example1'),
  86. hot1;
  87. function isEmptyRow(instance, row) {
  88. var rowData = instance.getData()[row];
  89. for (var i = 0, ilen = rowData.length; i < ilen; i++) {
  90. if (rowData[i] !== null) {
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. function defaultValueRenderer(instance, td, row, col, prop, value, cellProperties) {
  97. var args = arguments;
  98. if (args[5] === null && isEmptyRow(instance, row)) {
  99. args[5] = tpl[col];
  100. td.style.color = '#999';
  101. }
  102. else {
  103. td.style.color = '';
  104. }
  105. Handsontable.renderers.TextRenderer.apply(this, args);
  106. }
  107. hot1 = new Handsontable(container, {
  108. startRows: 8,
  109. startCols: 5,
  110. minSpareRows: 1,
  111. contextMenu: true,
  112. cells: function (row, col, prop) {
  113. var cellProperties = {};
  114. cellProperties.renderer = defaultValueRenderer;
  115. return cellProperties;
  116. },
  117. beforeChange: function (changes) {
  118. var instance = hot1,
  119. ilen = changes.length,
  120. clen = instance.colCount,
  121. rowColumnSeen = {},
  122. rowsToFill = {},
  123. i,
  124. c;
  125. for (i = 0; i < ilen; i++) {
  126. // if oldVal is empty
  127. if (changes[i][2] === null && changes[i][3] !== null) {
  128. if (isEmptyRow(instance, changes[i][0])) {
  129. // add this row/col combination to cache so it will not be overwritten by template
  130. rowColumnSeen[changes[i][0] + '/' + changes[i][1]] = true;
  131. rowsToFill[changes[i][0]] = true;
  132. }
  133. }
  134. }
  135. for (var r in rowsToFill) {
  136. if (rowsToFill.hasOwnProperty(r)) {
  137. for (c = 0; c < clen; c++) {
  138. // if it is not provided by user in this change set, take value from template
  139. if (!rowColumnSeen[r + '/' + c]) {
  140. changes.push([r, c, null, tpl[c]]);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. });
  147. hot1.loadData(data);
  148. </script>
  149. </div>
  150. </div>
  151. </div>
  152. <div class="footer-text">
  153. </div>
  154. </div>
  155. </div>
  156. </div>
  157. </div>
  158. <div id="outside-links-wrapper"></div>
  159. </body>
  160. </html>