handsontable_advanced.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Handsontable - Excel-like tables with extensive funtionality
  4. *
  5. * Demo JS code for handsontable_advanced.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var HotAdvanced = function() {
  11. //
  12. // Setup module components
  13. //
  14. // HOT advanced examples
  15. var _componentHotAdvanced = function() {
  16. if (typeof Handsontable == 'undefined') {
  17. console.warn('Warning - handsontable.min.js is not loaded.');
  18. return;
  19. }
  20. // Conditional formatting
  21. // ------------------------------
  22. // Add data
  23. var hot_format_data = [
  24. ['', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
  25. ['2003', -38293, '', 38849, 32353, -47758, 'Read only'],
  26. ['2004', 23433, 88569, 48892, 12322, '', 27840],
  27. ['2005', 64393, -89432, 'Read only', 89390, 42853, -12228],
  28. ['2006', 45382, 57729, -48823, -12774, '', -98493],
  29. ['2007', -86433, 48923, -33378, 'Read only', 90043, 34982],
  30. ['2008', 45833, -12293, 12894, 78859, '', 43054],
  31. ['2009', 'Read only', '', 49950, -58823, -57882, 89954],
  32. ['2010', -85943, 90449, -38882, 34928, '', 23487],
  33. ['2011', 44950, -90092, 'Read only', '', 89003, 'Read only'],
  34. ['2012', 23486, 'Read only', 47729, 23945, -99001, 48995],
  35. ['2013', 90392, '', 48852, 17789, 32984, ''],
  36. ['2014', -47382, 88457, '', 58875, -45398, '']
  37. ];
  38. // Header row renderer
  39. function firstRowRenderer(instance, td, row, col, prop, value, cellProperties) {
  40. Handsontable.renderers.TextRenderer.apply(this, arguments);
  41. // Add styles to the table cell
  42. td.style.fontWeight = '500';
  43. td.style.color = '#1B5E20';
  44. td.style.background = '#E8F5E9';
  45. }
  46. // Negative values renderer
  47. function negativeValueRenderer(instance, td, row, col, prop, value, cellProperties) {
  48. Handsontable.renderers.TextRenderer.apply(this, arguments);
  49. // If row contains negative number, add class "negative"
  50. if (parseInt(value, 10) < 0) {
  51. td.className = 'text-danger';
  52. }
  53. // If empty cell, add grey background
  54. if (!value || value === '') {
  55. td.style.background = '#f5f5f5';
  56. }
  57. }
  58. // Maps function to lookup string
  59. Handsontable.renderers.registerRenderer('negativeValueRenderer', negativeValueRenderer);
  60. // Define element
  61. var hot_format = document.getElementById('hot_format');
  62. // Initialize with options
  63. hot_format_init = new Handsontable(hot_format, {
  64. data: hot_format_data,
  65. stretchH: 'all',
  66. afterSelection: function (row, col, row2, col2) {
  67. var meta = this.getCellMeta(row2, col2);
  68. if (meta.readOnly) {
  69. this.updateSettings({fillHandle: false});
  70. }
  71. else {
  72. this.updateSettings({fillHandle: true});
  73. }
  74. },
  75. cells: function (row, col, prop, td) {
  76. var cellProperties = {};
  77. if (row === 0 || this.instance.getData()[row][col] === 'Read only') {
  78. cellProperties.readOnly = true; // make cell read-only if it is first row or the text reads 'readOnly'
  79. }
  80. if (row === 0 || col === 0) {
  81. cellProperties.renderer = firstRowRenderer; // uses function directly
  82. }
  83. else {
  84. cellProperties.renderer = "negativeValueRenderer"; // uses lookup map
  85. }
  86. return cellProperties;
  87. }
  88. });
  89. // Sorting data
  90. // ------------------------------
  91. // Add sample data for multiple examples
  92. var hot_data = [
  93. [1, "George Washington", "http://en.wikipedia.org/wiki/George_Washington", "30/04/1789", "4/03/1797", "Virginia"],
  94. [2, "John Adams", "http://en.wikipedia.org/wiki/John_Adams", "4/03/1797", "4/03/1801", "Massachusetts"],
  95. [3, "Thomas Jefferson", "http://en.wikipedia.org/wiki/Thomas_Jefferson", "4/03/1801", "4/03/1809", "Virginia"],
  96. [4, "James Madison", "http://en.wikipedia.org/wiki/James_Madison", "4/03/1809", "4/03/1817", "Virginia"],
  97. [5, "James Monroe", "http://en.wikipedia.org/wiki/James_Monroe", "4/03/1817", "4/03/1825", "Virginia"],
  98. [6, "John Quincy Adams", "http://en.wikipedia.org/wiki/John_Quincy_Adams", "4/03/1825", "4/03/1829", "Massachusetts"],
  99. [7, "Andrew Jackson", "http://en.wikipedia.org/wiki/Andrew_Jackson", "4/03/1829", "4/03/1837", "Tennessee"],
  100. [8, "Martin Van Buren", "http://en.wikipedia.org/wiki/Martin_Van_Buren", "4/03/1837", "4/03/1841", "New York"],
  101. [9, "William Henry Harrison", "http://en.wikipedia.org/wiki/William_Henry_Harrison", "4/03/1841", "4/04/1841", "Ohio"],
  102. [10, "John Tyler", "http://en.wikipedia.org/wiki/John_Tyler", "4/04/1841", "4/03/1845", "Virginia"],
  103. [11, "James K. Polk", "http://en.wikipedia.org/wiki/James_K._Polk", "4/03/1845", "4/03/1849", "Tennessee"],
  104. [12, "Zachary Taylor", "http://en.wikipedia.org/wiki/Zachary_Taylor", "4/03/1849", "9/07/1850", "Louisiana"]
  105. ];
  106. // Define element
  107. var hot_sorting = document.getElementById('hot_sorting');
  108. // Initialize with options
  109. var hot_sorting_init = new Handsontable(hot_sorting, {
  110. data: hot_data,
  111. stretchH: 'all',
  112. rowHeaders: true,
  113. colHeaders: true,
  114. columnSorting: true,
  115. manualColumnResize: true,
  116. sortIndicator: true
  117. });
  118. // Pagination
  119. // ------------------------------
  120. // Setup data set
  121. var getData = (function () {
  122. // Data
  123. var hot_pagination_data = [
  124. [1, "George Washington", "http://en.wikipedia.org/wiki/George_Washington", "30/04/1789", "4/03/1797", "Virginia"],
  125. [2, "John Adams", "http://en.wikipedia.org/wiki/John_Adams", "4/03/1797", "4/03/1801", "Massachusetts"],
  126. [3, "Thomas Jefferson", "http://en.wikipedia.org/wiki/Thomas_Jefferson", "4/03/1801", "4/03/1809", "Virginia"],
  127. [4, "James Madison", "http://en.wikipedia.org/wiki/James_Madison", "4/03/1809", "4/03/1817", "Virginia"],
  128. [5, "James Monroe", "http://en.wikipedia.org/wiki/James_Monroe", "4/03/1817", "4/03/1825", "Virginia"],
  129. [6, "John Quincy Adams", "http://en.wikipedia.org/wiki/John_Quincy_Adams", "4/03/1825", "4/03/1829", "Massachusetts"],
  130. [7, "Andrew Jackson", "http://en.wikipedia.org/wiki/Andrew_Jackson", "4/03/1829", "4/03/1837", "Tennessee"],
  131. [8, "Martin Van Buren", "http://en.wikipedia.org/wiki/Martin_Van_Buren", "4/03/1837", "4/03/1841", "New York"],
  132. [9, "William Henry Harrison", "http://en.wikipedia.org/wiki/William_Henry_Harrison", "4/03/1841", "4/04/1841", "Ohio"],
  133. [10, "John Tyler", "http://en.wikipedia.org/wiki/John_Tyler", "4/04/1841", "4/03/1845", "Virginia"],
  134. [11, "James K. Polk", "http://en.wikipedia.org/wiki/James_K._Polk", "4/03/1845", "4/03/1849", "Tennessee"],
  135. [12, "Zachary Taylor", "http://en.wikipedia.org/wiki/Zachary_Taylor", "4/03/1849", "9/07/1850", "Louisiana"],
  136. [13, "Millard Fillmore", "http://en.wikipedia.org/wiki/Millard_Fillmore", "9/07/1850", "4/03/1853", "New York"],
  137. [14, "Franklin Pierce", "http://en.wikipedia.org/wiki/Franklin_Pierce", "4/03/1853", "4/03/1857", "New Hampshire"],
  138. [15, "James Buchanan", "http://en.wikipedia.org/wiki/James_Buchanan", "4/03/1857", "4/03/1861", "Pennsylvania"],
  139. [16, "Abraham Lincoln", "http://en.wikipedia.org/wiki/Abraham_Lincoln", "4/03/1861", "15/04/1865", "Illinois"],
  140. [17, "Andrew Johnson", "http://en.wikipedia.org/wiki/Andrew_Johnson", "15/04/1865", "4/03/1869", "Tennessee"],
  141. [18, "Ulysses S. Grant", "http://en.wikipedia.org/wiki/Ulysses_S._Grant", "4/03/1869", "4/03/1877", "Ohio"],
  142. [19, "Rutherford B. Hayes", "http://en.wikipedia.org/wiki/Rutherford_B._Hayes", "4/03/1877", "4/03/1881", "Ohio"],
  143. [20, "James A. Garfield", "http://en.wikipedia.org/wiki/James_A._Garfield", "4/03/1881", "19/09/1881", "Ohio"],
  144. [21, "Chester A. Arthur", "http://en.wikipedia.org/wiki/Chester_A._Arthur", "19/09/1881", "4/03/1885", "New York"],
  145. [22, "Grover Cleveland", "http://en.wikipedia.org/wiki/Grover_Cleveland", "4/03/1885", "4/03/1889", "New York"],
  146. [23, "Benjamin Harrison", "http://en.wikipedia.org/wiki/Benjamin_Harrison", "4/03/1889", "4/03/1893", "Indiana"],
  147. [24, "Grover Cleveland (2nd term)", "http://en.wikipedia.org/wiki/Grover_Cleveland", "4/03/1893", "4/03/1897", "New York"],
  148. [25, "William McKinley", "http://en.wikipedia.org/wiki/William_McKinley", "4/03/1897", "14/9/1901", "Ohio"],
  149. [26, "Theodore Roosevelt", "http://en.wikipedia.org/wiki/Theodore_Roosevelt", "14/9/1901", "04/03/09", "New York"],
  150. [27, "William Howard Taft", "http://en.wikipedia.org/wiki/William_Howard_Taft", "04/03/09", "04/03/13", "Ohio"],
  151. [28, "Woodrow Wilson", "http://en.wikipedia.org/wiki/Woodrow_Wilson", "04/03/13", "04/03/21", "New Jersey"],
  152. [29, "Warren G. Harding", "http://en.wikipedia.org/wiki/Warren_G._Harding", "04/03/21", "02/08/23", "Ohio"],
  153. [30, "Calvin Coolidge", "http://en.wikipedia.org/wiki/Calvin_Coolidge", "02/08/23", "04/03/29", "Massachusetts"],
  154. [31, "Herbert Hoover", "http://en.wikipedia.org/wiki/Herbert_Hoover", "04/03/29", "04/03/33", "Iowa"],
  155. [32, "Franklin D. Roosevelt", "http://en.wikipedia.org/wiki/Franklin_D._Roosevelt", "04/03/33", "12/04/45", "New York"],
  156. [33, "Harry S. Truman", "http://en.wikipedia.org/wiki/Harry_S._Truman", "12/04/45", "20/01/53", "Missouri"],
  157. [34, "Dwight D. Eisenhower", "http://en.wikipedia.org/wiki/Dwight_D._Eisenhower", "20/01/53", "20/01/61", "Texas"],
  158. [35, "John F. Kennedy", "http://en.wikipedia.org/wiki/John_F._Kennedy", "20/01/61", "22/11/63", "Massachusetts"],
  159. [36, "Lyndon B. Johnson", "http://en.wikipedia.org/wiki/Lyndon_B._Johnson", "22/11/63", "20/01/69", "Texas"],
  160. [37, "Richard Nixon", "http://en.wikipedia.org/wiki/Richard_Nixon", "20/01/69", "09/08/74", "California"],
  161. [38, "Gerald Ford", "http://en.wikipedia.org/wiki/Gerald_Ford", "09/08/74", "20/01/77", "Michigan"],
  162. [39, "Jimmy Carter", "http://en.wikipedia.org/wiki/Jimmy_Carter", "20/01/77", "20/01/81", "Georgia"],
  163. [40, "Ronald Reagan", "http://en.wikipedia.org/wiki/Ronald_Reagan", "20/01/81", "20/01/89", "California"],
  164. [41, "George H. W. Bush", "http://en.wikipedia.org/wiki/George_H._W._Bush", "20/01/89", "20/01/93", "Texas"],
  165. [42, "Bill Clinton", "http://en.wikipedia.org/wiki/Bill_Clinton", "20/01/93", "20/01/01", "Arkansas"],
  166. [43, "George W. Bush", "http://en.wikipedia.org/wiki/George_W._Bush", "20/01/01", "20/01/09", "Texas"],
  167. [44, "Barack Obama", "http://en.wikipedia.org/wiki/Barack_Obama", "20/01/09", "Incumbent", "Illinois"]
  168. ];
  169. // Paging setup
  170. return function () {
  171. var page = parseInt(window.location.hash.replace('#', ''), 10) || 1,
  172. limit = 10,
  173. row = (page - 1) * limit,
  174. count = page * limit,
  175. part = [];
  176. for (;row < count;row++) {
  177. part.push(hot_pagination_data[row]);
  178. }
  179. // Toggling active class in pagination
  180. if(location.hash != "") {
  181. // Remove active class on load from the first item
  182. $('#hot_pagination_nav .page-item').removeClass('active');
  183. // Remove active class from siblings on click
  184. $('#hot_pagination_nav .page-item').on('click', function() {
  185. $(this).siblings('.page-item').removeClass('active');
  186. });
  187. // Add active class
  188. $('#hot_pagination_nav .page-item').has('a[href="' + location.hash + '"]').addClass('active');
  189. }
  190. return part;
  191. }
  192. })();
  193. // Define element
  194. var hot_pagination = document.getElementById('hot_pagination');
  195. // Initialize with options
  196. var hot_pagination_init = new Handsontable(hot_pagination, {
  197. data: getData(),
  198. colHeaders: true,
  199. stretchH: 'all'
  200. });
  201. // Load data on hash change
  202. Handsontable.dom.addEvent(window, 'hashchange', function (event) {
  203. hot_pagination_init.loadData(getData());
  204. });
  205. // Pre-populating new rows
  206. // ------------------------------
  207. // Add data
  208. var hot_populate_data = [
  209. [1, "George Washington", "http://en.wikipedia.org/wiki/George_Washington", "30/04/1789", "4/03/1797", "Virginia"],
  210. [2, "John Adams", "http://en.wikipedia.org/wiki/John_Adams", "4/03/1797", "4/03/1801", "Massachusetts"],
  211. [3, "Thomas Jefferson", "http://en.wikipedia.org/wiki/Thomas_Jefferson", "4/03/1801", "4/03/1809", "Virginia"],
  212. [4, "James Madison", "http://en.wikipedia.org/wiki/James_Madison", "4/03/1809", "4/03/1817", "Virginia"],
  213. [5, "James Monroe", "http://en.wikipedia.org/wiki/James_Monroe", "4/03/1817", "4/03/1825", "Virginia"],
  214. [6, "John Quincy Adams", "http://en.wikipedia.org/wiki/John_Quincy_Adams", "4/03/1825", "4/03/1829", "Massachusetts"],
  215. [7, "Andrew Jackson", "http://en.wikipedia.org/wiki/Andrew_Jackson", "4/03/1829", "4/03/1837", "Tennessee"],
  216. [8, "Martin Van Buren", "http://en.wikipedia.org/wiki/Martin_Van_Buren", "4/03/1837", "4/03/1841", "New York"],
  217. [9, "William Henry Harrison", "http://en.wikipedia.org/wiki/William_Henry_Harrison", "4/03/1841", "4/04/1841", "Ohio"],
  218. [10, "John Tyler", "http://en.wikipedia.org/wiki/John_Tyler", "4/04/1841", "4/03/1845", "Virginia"],
  219. [11, "James K. Polk", "http://en.wikipedia.org/wiki/James_K._Polk", "4/03/1845", "4/03/1849", "Tennessee"],
  220. [12, "Zachary Taylor", "http://en.wikipedia.org/wiki/Zachary_Taylor", "4/03/1849", "9/07/1850", "Louisiana"]
  221. ];
  222. // Cells template
  223. var tpl = ['one', 'two', 'three', 'four', 'five', 'six'];
  224. // Render empty row
  225. function isEmptyRow(instance, row) {
  226. var rowData = instance.getData()[row];
  227. for (var i = 0, ilen = rowData.length; i < ilen; i++) {
  228. if (rowData[i] !== null) {
  229. return false;
  230. }
  231. }
  232. return true;
  233. }
  234. // Render default values
  235. function defaultValueRenderer(instance, td, row, col, prop, value, cellProperties) {
  236. var args = arguments;
  237. if (args[5] === null && isEmptyRow(instance, row)) {
  238. args[5] = tpl[col];
  239. td.style.color = '#ccc';
  240. }
  241. else {
  242. td.style.color = '';
  243. }
  244. Handsontable.renderers.TextRenderer.apply(this, args);
  245. }
  246. // Define element
  247. var hot_populate = document.getElementById('hot_populate')
  248. // Initialize with options
  249. var hot_populate_init = new Handsontable(hot_populate, {
  250. data: hot_populate_data,
  251. startRows: 8,
  252. startCols: 5,
  253. minSpareRows: 1,
  254. colHeaders: true,
  255. stretchH: 'all',
  256. contextMenu: true,
  257. cells: function (row, col, prop) {
  258. var cellProperties = {};
  259. cellProperties.renderer = defaultValueRenderer;
  260. return cellProperties;
  261. },
  262. beforeChange: function (changes) {
  263. var instance = hot_populate_init,
  264. ilen = changes.length,
  265. clen = instance.colCount,
  266. rowColumnSeen = {},
  267. rowsToFill = {},
  268. i,
  269. c;
  270. for (i = 0; i < ilen; i++) {
  271. // If oldVal is empty
  272. if (changes[i][2] === null && changes[i][3] !== null) {
  273. if (isEmptyRow(instance, changes[i][0])) {
  274. // Add this row/col combination to cache so it will not be overwritten by template
  275. rowColumnSeen[changes[i][0] + '/' + changes[i][1]] = true;
  276. rowsToFill[changes[i][0]] = true;
  277. }
  278. }
  279. }
  280. for (var r in rowsToFill) {
  281. if (rowsToFill.hasOwnProperty(r)) {
  282. for (c = 0; c < clen; c++) {
  283. // If it is not provided by user in this change set, take value from template
  284. if (!rowColumnSeen[r + '/' + c]) {
  285. changes.push([r, c, null, tpl[c]]);
  286. }
  287. }
  288. }
  289. }
  290. }
  291. });
  292. // Highlighting current
  293. // ------------------------------
  294. // Define element
  295. hot_highlight = document.getElementById('hot_highlight');
  296. // Initialize with options
  297. hot_highlight_init = Handsontable(hot_highlight, {
  298. data: hot_data,
  299. minRows: 5,
  300. minCols: 6,
  301. stretchH: 'all',
  302. currentRowClassName: 'active',
  303. currentColClassName: 'active',
  304. rowHeaders: true,
  305. colHeaders: true
  306. });
  307. // Select cell
  308. hot_highlight_init.selectCell(2,2);
  309. };
  310. //
  311. // Return objects assigned to module
  312. //
  313. return {
  314. init: function() {
  315. _componentHotAdvanced();
  316. }
  317. }
  318. }();
  319. // Initialize module
  320. // ------------------------------
  321. document.addEventListener('DOMContentLoaded', function() {
  322. HotAdvanced.init();
  323. });