understanding_reference.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>Understanding binding as reference - 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="Understanding data reference binding">
  29. <meta property="og:description"
  30. content="Handsontable binds to your data source (array or object) by reference. Therefore, all the data entered in the
  31. grid will alter the original data source.">
  32. <meta property="og:url" content="http://handsontable.com/demo/understanding_reference.html">
  33. <meta property="og:image" content="http://handsontable.com/demo/image/og-image.png">
  34. <meta property="og:image:type" content="image/png">
  35. <meta property="og:image:width" content="409">
  36. <meta property="og:image:height" content="164">
  37. <link rel="canonical" href="http://handsontable.com/demo/understanding_reference.html">
  38. <!--
  39. Google Analytics for GitHub Page. Don't copy this to your project :)
  40. -->
  41. <script src="js/ga.js"></script>
  42. </head>
  43. <body>
  44. <div class="wrapper">
  45. <div class="wrapper-row">
  46. <div id="global-menu-clone">
  47. <h1><a href="../index.html">Handsontable</a></h1>
  48. </div>
  49. <div id="container">
  50. <div class="columnLayout">
  51. <div class="rowLayout">
  52. <div class="descLayout">
  53. <div class="pad" data-jsfiddle="example1">
  54. <h2>Understanding binding as reference</h2>
  55. <p>Handsontable binds to your data source (array or object) by reference. Therefore, all the data entered in
  56. the
  57. grid will alter the original data source.</p>
  58. <p>In complex applications, you may have a purpose to change data source programatically (outside of
  59. Handsontable). A value change that was done programatically will not be presented on the screen unless you
  60. refresh the grid on screen using the <b>render</b> method.</p>
  61. <div id="example1"></div>
  62. <p>
  63. <button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">
  64. Dump data to console
  65. </button>
  66. </p>
  67. </div>
  68. </div>
  69. <div class="codeLayout">
  70. <div class="pad">
  71. <div class="jsFiddle">
  72. <button class="jsFiddleLink" data-runfiddle="example1">Edit in jsFiddle</button>
  73. </div>
  74. <script data-jsfiddle="example1">
  75. var
  76. data1 = [
  77. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  78. ['2008', 10, 11, 12, 13],
  79. ['2009', 20, 11, 14, 13],
  80. ['2010', 30, 15, 12, 13]
  81. ],
  82. container1 = document.getElementById('example1'),
  83. settings1 = {
  84. data: data1
  85. },
  86. hot1;
  87. hot1 = new Handsontable(container1, settings1);
  88. data1[0][1] = 'Ford'; // change "Kia" to "Ford" programatically
  89. hot1.render();
  90. </script>
  91. </div>
  92. </div>
  93. </div>
  94. <div class="rowLayout">
  95. <div class="descLayout">
  96. <div class="pad">
  97. <h2>But I want to change my data without rendering changes!</h2>
  98. </div>
  99. </div>
  100. </div>
  101. <div class="rowLayout">
  102. <div class="descLayout">
  103. <div class="pad" data-jsfiddle="example2">
  104. <p>In case you want to keep separate working copy of data for Handsontable, it is suggested to clone the data
  105. source before you load it to Handsontable.</p>
  106. <p>This can easily be done with <b>JSON.parse(JSON.stringify(data))</b> or some other deep-cloning function.</p>
  107. <div id="example2"></div>
  108. <p>
  109. <button name="dump" data-dump="#example2" data-instance="hot2" title="Prints current data source to Firebug/Chrome Dev Tools">
  110. Dump data to console
  111. </button>
  112. </p>
  113. </div>
  114. </div>
  115. <div class="codeLayout">
  116. <div class="pad">
  117. <div class="jsFiddle">
  118. <button class="jsFiddleLink" data-runfiddle="example2">Edit in jsFiddle</button>
  119. </div>
  120. <script data-jsfiddle="example2">
  121. var
  122. data2 = [
  123. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  124. ['2008', 10, 11, 12, 13],
  125. ['2009', 20, 11, 14, 13],
  126. ['2010', 30, 15, 12, 13]
  127. ],
  128. container2 = document.getElementById('example2'),
  129. hot2;
  130. hot2 = new Handsontable(container2, {
  131. data: JSON.parse(JSON.stringify(data2))
  132. });
  133. </script>
  134. </div>
  135. </div>
  136. </div>
  137. <div class="rowLayout">
  138. <div class="descLayout">
  139. <div class="pad" data-jsfiddle="example3">
  140. <p>In a similar way, you may find it useful to clone data before saving it.</p>
  141. <p>That would be useful to make programmatic changes that would be saved to server but kept not invisible to
  142. the
  143. user.</p>
  144. <div id="example3"></div>
  145. <p>
  146. <button name="dump" data-dump="#example3" data-instance="hot3" title="Prints current data source to Firebug/Chrome Dev Tools">
  147. Dump data to console
  148. </button>
  149. </p>
  150. </div>
  151. </div>
  152. <div class="codeLayout">
  153. <div class="pad">
  154. <div class="jsFiddle">
  155. <button class="jsFiddleLink" data-runfiddle="example3">Edit in jsFiddle</button>
  156. </div>
  157. <script data-jsfiddle="example3">
  158. var
  159. data3 = [
  160. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  161. ['2008', 10, 11, 12, 13],
  162. ['2009', 20, 11, 14, 13],
  163. ['2010', 30, 15, 12, 13]
  164. ],
  165. container3 = document.getElementById('example3'),
  166. hot3;
  167. hot3 = new Handsontable(container3, {
  168. data: data3,
  169. afterChange: function () {
  170. var tmpData = JSON.parse(JSON.stringify(data3));
  171. // now tmpData has a copy of data3 that can be manipulated
  172. // without breaking the Handsontable data source object
  173. }
  174. });
  175. </script>
  176. </div>
  177. </div>
  178. </div>
  179. <div class="footer-text">
  180. </div>
  181. </div>
  182. </div>
  183. </div>
  184. </div>
  185. <div id="outside-links-wrapper"></div>
  186. </body>
  187. </html>