datasources.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <!doctype html>
  2. <html xmlns="http://www.w3.org/1999/html">
  3. <head>
  4. <meta charset='utf-8'>
  5. <title>Array, object and function data sources - 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="Array, object and function">
  29. <meta property="og:description"
  30. content="This page shows how to use Handsontable with various data sources: array, object and function.">
  31. <meta property="og:url" content="http://handsontable.com/demo/reference.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/reference.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">
  53. <h2>Array, object and function data sources</h2>
  54. <p>This page shows how to use Handsontable with various data sources:</p>
  55. <ul>
  56. <li><a href="#array">array data source</a></li>
  57. <li><a href="#array-hidden">array data source (with hidden columns)</a></li>
  58. <li><a href="#object">object data source</a></li>
  59. <li><a href="#nested">object data source (nested, with column mapping)</a></li>
  60. <li><a href="#dataschema">object data source (custom data schema)</a></li>
  61. <li><a href="#propertyschema">function data source and schema (to reach where arrays and objects can't
  62. reach)</a></li>
  63. </ul>
  64. <p>Please take note that Handsontable will change the original data source. More about this here: <a
  65. href="understanding_reference.html">Understanding binding as reference</a>.</p>
  66. </div>
  67. </div>
  68. </div>
  69. <div class="rowLayout">
  70. <div class="descLayout">
  71. <div class="pad" data-jsfiddle="example1">
  72. <a name="array"></a>
  73. <h2>Array data source</h2>
  74. <p>Most of the people use <strong>array of arrays</strong> data source with Handsontable.</p>
  75. <div id="example1"></div>
  76. <p>
  77. <button name="dump" data-dump="#example1" data-instance="hot1" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
  78. data to console
  79. </button>
  80. </p>
  81. </div>
  82. </div>
  83. <div class="codeLayout">
  84. <div class="pad">
  85. <div class="jsFiddle">
  86. <button class="jsFiddleLink" data-runfiddle="example1" data-instance="hot1">Edit in jsFiddle</button>
  87. </div>
  88. <script data-jsfiddle="example1">
  89. var
  90. data = [
  91. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  92. ['2008', 10, 11, 12, 13],
  93. ['2009', 20, 11, 14, 13],
  94. ['2009', 30, 15, 12, 13]
  95. ],
  96. container1 = document.getElementById('example1'),
  97. hot1;
  98. hot1 = new Handsontable(container1, {
  99. data: data,
  100. startRows: 5,
  101. startCols: 5,
  102. colHeaders: true,
  103. minSpareRows: 1
  104. });
  105. </script>
  106. </div>
  107. </div>
  108. </div>
  109. <div class="rowLayout">
  110. <div class="descLayout">
  111. <div class="pad" data-jsfiddle="example2">
  112. <a name="array-hidden"></a>
  113. <h2>Array data source (with hidden columns)</h2>
  114. <p>Let's say, you want the same data source, but without the <b>Kia</b> column.</p>
  115. <div id="example2"></div>
  116. <p>
  117. <button name="dump" data-instance="hot2" data-dump="#example2" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
  118. data to console
  119. </button>
  120. </p>
  121. </div>
  122. </div>
  123. <div class="codeLayout">
  124. <div class="pad">
  125. <div class="jsFiddle">
  126. <button class="jsFiddleLink" data-runfiddle="example2" >Edit in jsFiddle</button>
  127. </div>
  128. <script data-jsfiddle="example2">
  129. var
  130. hiddenData = [
  131. ['', 'Kia', 'Nissan', 'Toyota', 'Honda'],
  132. ['2008', 10, 11, 12, 13],
  133. ['2009', 20, 11, 14, 13],
  134. ['2009', 30, 15, 12, 13]
  135. ],
  136. container = document.getElementById('example2'),
  137. hot2;
  138. hot2 = new Handsontable(container, {
  139. data: hiddenData,
  140. startRows: 5,
  141. startCols: 5,
  142. colHeaders: true,
  143. columns: [
  144. {data: 0},
  145. {data: 2},
  146. {data: 3},
  147. {data: 4}
  148. ],
  149. minSpareRows: 1
  150. });
  151. </script>
  152. </div>
  153. </div>
  154. </div>
  155. <div class="rowLayout">
  156. <div class="descLayout">
  157. <div class="pad" data-jsfiddle="example3">
  158. <a name="object"></a>
  159. <h2>Object data source</h2>
  160. <p>With version 0.7.0, comes possibility to use <strong>array of objects</strong> data source.</p>
  161. <div id="example3"></div>
  162. <p>
  163. <button name="dump" data-dump="#example3" data-instance="hot3">Dump to console</button>
  164. </p>
  165. </div>
  166. </div>
  167. <div class="codeLayout">
  168. <div class="pad">
  169. <div class="jsFiddle">
  170. <button class="jsFiddleLink" data-runfiddle="example3">Edit in jsFiddle</button>
  171. </div>
  172. <script data-jsfiddle="example3">
  173. var
  174. objectData = [
  175. {id: 1, name: 'Ted Right', address: ''},
  176. {id: 2, name: 'Frank Honest', address: ''},
  177. {id: 3, name: 'Joan Well', address: ''}
  178. ],
  179. container3 = document.getElementById('example3'),
  180. hot3;
  181. hot3 = new Handsontable(container3, {
  182. data: objectData,
  183. startRows: 5,
  184. startCols: 3,
  185. colHeaders: true,
  186. minSpareRows: 1
  187. });
  188. </script>
  189. </div>
  190. </div>
  191. </div>
  192. <div class="rowLayout">
  193. <div class="descLayout">
  194. <div class="pad" data-jsfiddle="example4">
  195. <a name="nested"></a>
  196. <h2>Object data source (nested, with column mapping)</h2>
  197. <p>Some people have nested objects. They can also be used at the data source with a little bit of column
  198. mapping.</p>
  199. <p>The mapping is done using the <b>columns</b> option.</p>
  200. <div id="example4"></div>
  201. <p>
  202. <button name="dump" data-dump="#example4" data-instance="hot4" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
  203. data to console
  204. </button>
  205. </p>
  206. </div>
  207. </div>
  208. <div class="codeLayout">
  209. <div class="pad">
  210. <div class="jsFiddle">
  211. <button class="jsFiddleLink" data-runfiddle="example4" data-instance="hot4">Edit in jsFiddle</button>
  212. </div>
  213. <script data-jsfiddle="example4">
  214. var
  215. nestedObjects = [
  216. {id: 1, name: {first: "Ted", last: "Right"}, address: ""},
  217. {id: 2, address: ""}, // HOT will create missing properties on demand
  218. {id: 3, name: {first: "Joan", last: "Well"}, address: ""}
  219. ],
  220. container4 = document.getElementById('example4'),
  221. hot4;
  222. hot4 = new Handsontable(container4, {
  223. data: nestedObjects,
  224. startRows: 5,
  225. startCols: 4,
  226. colHeaders: true,
  227. columns: [
  228. {data: 'id'},
  229. {data: 'name.first'},
  230. {data: 'name.last'},
  231. {data: 'address'}
  232. ],
  233. minSpareRows: 1
  234. });
  235. </script>
  236. </div>
  237. </div>
  238. </div>
  239. <div class="rowLayout">
  240. <div class="descLayout">
  241. <div class="pad" data-jsfiddle="example5">
  242. <a name="dataschema"></a>
  243. <h2>Object data source (custom data schema)</h2>
  244. <p>When you use object data binding, Handsontable needs to know the data structure to create when you add a new
  245. row.</p>
  246. <p>If your data source contains at least one row, Handsontable will figure out the data structure based on the
  247. first row.</p>
  248. <p>In case you want to start with an empty data source, you will need to provide the <strong>dataSchema</strong>
  249. option that contains the data structure for any new row added to the grid.</p>
  250. <p>The below example shows custom data schema with an empty data source:</p>
  251. <div id="example5"></div>
  252. <p>
  253. <button name="dump" data-dump="#example5" data-instance="hot5" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
  254. data to console
  255. </button>
  256. </p>
  257. </div>
  258. </div>
  259. <div class="codeLayout">
  260. <div class="pad">
  261. <div class="jsFiddle">
  262. <button class="jsFiddleLink" data-runfiddle="example5" data-instance="hot5">Edit in jsFiddle</button>
  263. </div>
  264. <script data-jsfiddle="example5">
  265. var
  266. container = document.getElementById('example5'),
  267. hot5;
  268. hot5 = new Handsontable(container, {
  269. data: [],
  270. dataSchema: {id: null, name: {first: null, last: null}, address: null},
  271. startRows: 5,
  272. startCols: 4,
  273. colHeaders: ['ID', 'First Name', 'Last Name', 'Address'],
  274. columns: [
  275. {data: 'id'},
  276. {data: 'name.first'},
  277. {data: 'name.last'},
  278. {data: 'address'}
  279. ],
  280. minSpareRows: 1
  281. });
  282. </script>
  283. </div>
  284. </div>
  285. </div>
  286. <div class="rowLayout">
  287. <div class="descLayout">
  288. <div class="pad" data-jsfiddle="example6">
  289. <a name="propertyschema"></a>
  290. <h2>Function data source and schema (to reach where arrays and objects can't reach)</h2>
  291. <p>
  292. If your <strong>dataSchema</strong> is actually a constructor of an
  293. object that doesn't directly expose its members, like a Backbone.js
  294. model, you can specify functions for the <strong>data</strong> member
  295. of each <strong>columns</strong> item.
  296. </p>
  297. <p>The below example shows a small example of using such objects:</p>
  298. <div id="example6"></div>
  299. <p>
  300. <button name="dump" data-dump="#example6" data-instance="hot6" title="Prints current data source to Firebug/Chrome Dev Tools">Dump
  301. data to console
  302. </button>
  303. </p>
  304. </div>
  305. </div>
  306. <div class="codeLayout">
  307. <div class="pad">
  308. <div class="jsFiddle">
  309. <button class="jsFiddleLink" data-runfiddle="example6" data-instance="hot6">Edit in jsFiddle</button>
  310. </div>
  311. <script data-jsfiddle="example6">
  312. var
  313. container6 = document.getElementById('example6'),
  314. hot6;
  315. hot6 = new Handsontable(container6, {
  316. data: [
  317. model({id: 1, name: 'Ted Right', address: ''}),
  318. model({id: 2, name: 'Frank Honest', address: ''}),
  319. model({id: 3, name: 'Joan Well', address: ''})
  320. ],
  321. dataSchema: model,
  322. startRows: 5,
  323. startCols: 3,
  324. colHeaders: ['ID', 'Name', 'Address'],
  325. columns: [
  326. {data: property('id')},
  327. {data: property('name')},
  328. {data: property('address')}
  329. ],
  330. minSpareRows: 1
  331. });
  332. function model(opts) {
  333. var
  334. _pub = {},
  335. _priv = {
  336. "id": undefined,
  337. "name": undefined,
  338. "address": undefined
  339. };
  340. for (var i in opts) {
  341. if (opts.hasOwnProperty(i)) {
  342. _priv[i] = opts[i];
  343. }
  344. }
  345. _pub.attr = function (attr, val) {
  346. if (typeof val === 'undefined') {
  347. window.console && console.log("\t\tGET the", attr, "value of", _pub);
  348. return _priv[attr];
  349. }
  350. window.console && console.log("SET the", attr, "value of", _pub);
  351. _priv[attr] = val;
  352. return _pub;
  353. };
  354. return _pub;
  355. }
  356. function property(attr) {
  357. return function (row, value) {
  358. return row.attr(attr, value);
  359. }
  360. }
  361. </script>
  362. </div>
  363. </div>
  364. </div>
  365. <div class="footer-text">
  366. </div>
  367. </div>
  368. </div>
  369. </div>
  370. </div>
  371. <div id="outside-links-wrapper"></div>
  372. </body>
  373. </html>