handsontable_cells.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Handsontable - Excel-like tables with extensive funtionality
  4. *
  5. * Demo JS code for handsontable_cells.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var HotCells = function() {
  11. //
  12. // Setup module components
  13. //
  14. // HOT cells examples
  15. var _componentHotCells = function() {
  16. if (typeof Handsontable == 'undefined') {
  17. console.warn('Warning - handsontable.min.js is not loaded.');
  18. return;
  19. }
  20. // Data validation
  21. // ------------------------------
  22. // Add sample data
  23. var people = [
  24. {id: 1, name: {first: 'Joe', last: 'Fabiano'}, ip: '0.0.0.1', email: 'Joe.Fabiano@ex.com'},
  25. {id: 2, name: {first: 'Fred', last: 'Wecler'}, ip: '0.0.0.1', email: 'Fred.Wecler@ex.com'},
  26. {id: 3, name: {first: 'Steve', last: 'Wilson'}, ip: '0.0.0.1', email: 'Steve.Wilson@ex.com'},
  27. {id: 4, name: {first: 'Maria', last: 'Fernandez'}, ip: '0.0.0.1', email: 'M.Fernandez@ex.com'},
  28. {id: 5, name: {first: 'Pierre', last: 'Barbault'}, ip: '0.0.0.1', email: 'Pierre.Barbault@ex.com'},
  29. {id: 6, name: {first: 'Nancy', last: 'Moore'}, ip: '0.0.0.1', email: 'Nancy.Moore@ex.com'},
  30. {id: 7, name: {first: 'Barbara', last: 'MacDonald'}, ip: '0.0.0.1', email: 'B.MacDonald@ex.com'},
  31. {id: 8, name: {first: 'Wilma', last: 'Williams'}, ip: '0.0.0.1', email: 'Wilma.Williams@ex.com'},
  32. {id: 9, name: {first: 'Sasha', last: 'Silver'}, ip: '0.0.0.1', email: 'Sasha.Silver@ex.com'},
  33. {id: 10, name: {first: 'Don', last: 'Pérignon'}, ip: '0.0.0.1', email: 'Don.Pérignon@ex.com'},
  34. {id: 11, name: {first: 'Aaron', last: 'Kinley'}, ip: '0.0.0.1', email: 'Aaron.Kinley@ex.com'}
  35. ];
  36. // Define element
  37. var hot_validation = document.getElementById('hot_validation'),
  38. // Define output element
  39. hot_validation_console = document.getElementById('hot_validation_console');
  40. // Email validator
  41. var ipValidatorRegexp = /^(?:\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b|null)$/;
  42. var emailValidator = function (value, callback) {
  43. setTimeout(function() {
  44. if (/.+@.+/.test(value)) {
  45. callback(true);
  46. }
  47. else {
  48. callback(false);
  49. }
  50. }, 1000);
  51. };
  52. // Initialize with options
  53. var hot_validation_init = new Handsontable(hot_validation, {
  54. data: people,
  55. rowHeaders: true,
  56. stretchH: 'all',
  57. beforeChange: function (changes, source) {
  58. for (var i = changes.length - 1; i >= 0; i--) {
  59. // Gently don't accept the word "foo" (remove the change at index i)
  60. if (changes[i][3] === 'foo') {
  61. changes.splice(i, 1);
  62. }
  63. // If any of pasted cells contains the word "nuke", reject the whole paste
  64. else if (changes[i][3] === 'nuke') {
  65. return false;
  66. }
  67. // Capitalise first letter in column 1 and 2
  68. else if ((changes[i][1] === 'name.first' || changes[i][1] === 'name.last') && changes[i][3].charAt(0)) {
  69. changes[i][3] = changes[i][3].charAt(0).toUpperCase() + changes[i][3].slice(1);
  70. }
  71. }
  72. },
  73. afterChange: function (changes, source) {
  74. if (source !== 'loadData') {
  75. hot_validation_console.innerText = JSON.stringify(changes);
  76. Prism.highlightElement(hot_validation_console);
  77. }
  78. },
  79. colHeaders: ['ID', 'First name', 'Last name', 'IP', 'E-mail'],
  80. columns: [
  81. {data: 'id', type: 'numeric', className: 'htLeft'},
  82. {data: 'name.first'},
  83. {data: 'name.last'},
  84. {data: 'ip', validator: ipValidatorRegexp, allowInvalid: true},
  85. {data: 'email', validator: emailValidator, allowInvalid: false}
  86. ]
  87. });
  88. // Drag down
  89. // ------------------------------
  90. // Add sample data
  91. var hot_drag_data = [
  92. ['', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
  93. ['2008', 45833, 12293, 12894, 78859, 47729, 43054],
  94. ['2009', 34234, 48902, 49950, 58823, 57882, 89954],
  95. ['2010', 85943, 90449, 38882, 34928, 45397, 23487],
  96. ['2011', 44950, 90092, 89932, 89945, 89003, 58943],
  97. ['2012', 23486, 83394, 47729, 23945, 99001, 48995],
  98. ['2013', 90392, 58282, 48852, 17789, 32984, 23498],
  99. ['2014', 47382, 88457, 90029, 58875, 45398, 48995],
  100. ['2015', '', '', '', '', '', ''],
  101. ['2016', '', '', '', '', '', ''],
  102. ['2017', '', '', '', '', '', '']
  103. ];
  104. // Define element
  105. var hot_drag = document.getElementById('hot_drag');
  106. // Initialize with options
  107. var hot_drag_init = new Handsontable(hot_drag, {
  108. data: hot_drag_data,
  109. rowHeaders: true,
  110. colHeaders: true,
  111. stretchH: 'all',
  112. fillHandle: true // possible values: true, false, "horizontal", "vertical"
  113. });
  114. // Merge cells
  115. // ------------------------------
  116. // Generate sample data
  117. var hot_merge_data = Handsontable.helper.createSpreadsheetData(12, 10);
  118. // Define element
  119. var hot_merge = document.getElementById('hot_merge');
  120. // Initialize with options
  121. var hot_merge_init = new Handsontable(hot_merge, {
  122. data: hot_merge_data,
  123. rowHeaders: true,
  124. colHeaders: true,
  125. contextMenu: true,
  126. stretchH: 'all',
  127. mergeCells: [
  128. {row: 1, col: 1, rowspan: 3, colspan: 3},
  129. {row: 3, col: 4, rowspan: 2, colspan: 2},
  130. {row: 5, col: 6, rowspan: 3, colspan: 3}
  131. ]
  132. });
  133. // Alignment
  134. // ------------------------------
  135. // Generate sample data
  136. var hot_alignment_data = Handsontable.helper.createSpreadsheetData(12, 10);
  137. // Define element
  138. var hot_alignment = document.getElementById('hot_alignment');
  139. // Initialize with options
  140. var hot_alignment_init = new Handsontable(hot_alignment, {
  141. data: hot_alignment_data,
  142. rowHeaders: true,
  143. colHeaders: true,
  144. contextMenu: true,
  145. stretchH: 'all',
  146. mergeCells: [
  147. {row: 1, col: 1, rowspan: 3, colspan: 3},
  148. {row: 3, col: 4, rowspan: 2, colspan: 2}
  149. ],
  150. className: "htCenter",
  151. cell: [
  152. {row: 0, col: 0, className: "htRight"},
  153. {row: 1, col: 1, className: "htLeft htMiddle"},
  154. {row: 3, col: 4, className: "htLeft htBottom"}
  155. ]
  156. });
  157. // Readonly
  158. // ------------------------------
  159. // Add dsample data. Used in readonly and disabled examples
  160. var car_data = [
  161. {car: 'Toyota', year: 2008, chassis: 'white', bumper: 'white'},
  162. {car: 'Chevrolet', year: 2010, chassis: 'grey', bumper: 'black'},
  163. {car: 'Lexus', year: 2006, chassis: 'red', bumper: 'black'},
  164. {car: 'Nissan', year: 2013, chassis: 'purple', bumper: 'purple'},
  165. {car: 'Volvo', year: 2012, chassis: 'red', bumper: 'red'},
  166. {car: 'Nissan', year: 2011, chassis: 'black', bumper: 'grey'},
  167. {car: 'Nissan', year: 2012, chassis: 'black', bumper: 'black'},
  168. {car: 'Nissan', year: 2013, chassis: 'blue', bumper: 'blue'},
  169. {car: 'Chrysler', year: 2014, chassis: 'yellow', bumper: 'black'},
  170. {car: 'Volvo', year: 2015, chassis: 'white', bumper: 'grey'}
  171. ];
  172. // Define element
  173. var hot_readonly = document.getElementById('hot_readonly');
  174. // Initialize with options
  175. hot_readonly_init = new Handsontable(hot_readonly, {
  176. data: car_data,
  177. stretchH: 'all',
  178. colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color']
  179. });
  180. // Update settings on init
  181. hot_readonly_init.updateSettings({
  182. cells: function (row, col, prop) {
  183. var cellProperties = {};
  184. if (hot_readonly_init.getSourceData()[row][prop] === 'Nissan') {
  185. cellProperties.readOnly = true;
  186. }
  187. return cellProperties;
  188. }
  189. });
  190. // Disable cell editing
  191. // ------------------------------
  192. // Define element
  193. var hot_non_editable = document.getElementById('hot_non_editable');
  194. // Initialize with options
  195. hot_non_editable_init = new Handsontable(hot_non_editable, {
  196. data: car_data,
  197. stretchH: 'all',
  198. colHeaders: ['Car', 'Year', 'Chassis color', 'Bumper color']
  199. });
  200. // Update settings on init
  201. hot_non_editable_init.updateSettings({
  202. cells: function (row, col, prop) {
  203. var cellProperties = {};
  204. if (hot_non_editable_init.getSourceData()[row][prop] === 'Nissan') {
  205. cellProperties.editor = false;
  206. cellProperties.className = "disabled";
  207. }
  208. else {
  209. cellProperties.editor = 'text';
  210. }
  211. return cellProperties;
  212. }
  213. });
  214. };
  215. //
  216. // Return objects assigned to module
  217. //
  218. return {
  219. init: function() {
  220. _componentHotCells();
  221. }
  222. }
  223. }();
  224. // Initialize module
  225. // ------------------------------
  226. document.addEventListener('DOMContentLoaded', function() {
  227. HotCells.init();
  228. });