Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

index.html 12 KiB

2 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>XhiveFramework DataTable</title>
  8. <style>
  9. body {
  10. font-family: 'Tahoma';
  11. font-weight: normal;
  12. font-size: 12px;
  13. }
  14. </style>
  15. <link rel="stylesheet" href="./dist/xhiveframework-datatable.css" />
  16. </head>
  17. <body>
  18. <h1>XhiveFramework DataTable</h1>
  19. <button onclick="datatable.render()">Render Table</button>
  20. <button onclick="datatable.refresh()">Refresh Data</button>
  21. <button onclick="switchToTreeView()" data-action="treeview">TreeView</button>
  22. <label>
  23. <input type="checkbox" id="input-large-data" />
  24. <span>Large Data</span>
  25. </label>
  26. <label>
  27. <input type="checkbox" id="dark-theme" />
  28. <span>Dark Theme</span>
  29. </label>
  30. <section style="width: 60%; margin: 0 auto;">
  31. </section>
  32. <section id="datatable2" style="width: 60%; ">
  33. </section>
  34. <script src="./node_modules/sortablejs/Sortable.js"></script>
  35. <script src="./dist/xhiveframework-datatable.js"></script>
  36. <script>
  37. document.addEventListener('DOMContentLoaded', () => {
  38. let data = [];
  39. let columns = [];
  40. let largeData = false;
  41. document.querySelector('#input-large-data').addEventListener('change', (e) => {
  42. const enabled = e.target.value === 'on';
  43. largeData = enabled;
  44. buildData();
  45. makeDatatable();
  46. });
  47. document.querySelector('#dark-theme').addEventListener('change', (e) => {
  48. const enabled = document.querySelector('#dark-theme:checked');
  49. if (enabled) {
  50. const link = document.createElement('link');
  51. link.href = 'src/dark.css';
  52. link.rel = 'stylesheet'
  53. link.id = 'dark-theme-stylesheet'
  54. document.head.appendChild(link);
  55. link.onload = () => datatable.setDimensions()
  56. } else {
  57. document.getElementById('dark-theme-stylesheet').remove();
  58. setTimeout(() => datatable.setDimensions())
  59. }
  60. })
  61. function buildTreeData() {
  62. columns = [
  63. { name: 'Files', width: 300 },
  64. { name: 'Size', width: 150, align: 'right' },
  65. { name: 'Last Updated', width: 200, align: 'right' },
  66. ]
  67. data = [
  68. {
  69. 'Files': 'All Folders',
  70. 'Size': '2M',
  71. 'Last Updated': '',
  72. 'indent': -1
  73. },
  74. {
  75. 'Files': 'Documents',
  76. 'Size': '2M',
  77. 'Last Updated': '',
  78. 'indent': 0
  79. },
  80. {
  81. 'Files': 'project.pdf',
  82. 'Size': '1M',
  83. 'Last Updated': 'Yesterday',
  84. 'indent': 1
  85. },
  86. {
  87. 'Files': 'my-face.png',
  88. 'Size': '500k',
  89. 'Last Updated': '2018-04-09',
  90. 'indent': 1
  91. },
  92. {
  93. 'Files': 'Projects',
  94. 'Size': '77M',
  95. 'Last Updated': '',
  96. 'indent': 0
  97. },
  98. {
  99. 'Files': 'xhiveframework-gantt',
  100. 'Size': '23M',
  101. 'Last Updated': '',
  102. 'indent': 1
  103. },
  104. {
  105. 'Files': 'dist',
  106. 'Size': '50k',
  107. 'Last Updated': '2018-06-01',
  108. 'indent': 2
  109. },
  110. {
  111. 'Files': 'package.json',
  112. 'Size': '5k',
  113. 'Last Updated': '2018-06-01',
  114. 'indent': 2
  115. },
  116. {
  117. 'Files': 'xhiveframework-datatable',
  118. 'Size': '54M',
  119. 'Last Updated': '',
  120. 'indent': 1
  121. },
  122. {
  123. 'Files': 'src',
  124. 'Size': '53k',
  125. 'Last Updated': 'A few seconds ago',
  126. 'indent': 2
  127. },
  128. {
  129. 'Files': 'dist',
  130. 'Size': '21k',
  131. 'Last Updated': 'A few seconds ago',
  132. 'indent': 2
  133. },
  134. ]
  135. data.map(d => d.indent++)
  136. }
  137. function buildData() {
  138. columns = [
  139. { name: "Name" },
  140. { name: "Position" },
  141. { name: "Office" },
  142. { name: "Extn." },
  143. {
  144. name: "Start Date",
  145. format: (value) => (value || '').split('/').reverse().join('/'),
  146. compareValue: (cell, keyword) => {
  147. const keywordValue = keyword.split('/').reverse().join('/')
  148. return [+new Date(cell.content), +new Date(keywordValue)];
  149. }
  150. },
  151. { name: "Salary", format: value => formatMoney(value) }
  152. ];
  153. data = [["Tiger Nixon", { content: "System Architect<br>New line", editable: false }, "Edinburgh", 5421, "2011/04/25", 320800], ["Garrett Winters", "Accountant", "Tokyo", 8422, "2011/07/25", 170750], ["Ashton Cox", "Junior Technical Author", "San Francisco", 1562, "2009/01/12", 86000], ["Cedric Kelly", "Senior Javascript Developer", "Edinburgh", 6224, "2012/03/29", 433060], ["Airi Satou", "Accountant", "Tokyo", 5407, "2008/11/28", 162700], ["Brielle Williamson", "Integration Specialist", "New York", 4804, "2012/12/02", 372000], ["Herrod Chandler", "Sales Assistant", "San Francisco", 9608, "2012/08/06", 137500], ["Rhona Davidson", "Integration Specialist", "Tokyo", 6200, "2010/10/14", 327900], ["Colleen Hurst", "Javascript Developer", "San Francisco", 2360, "2009/09/15", 205500], ["Sonya Frost", "Software Engineer", "Edinburgh", 1667, "2008/12/13", 103600], ["Jena Gaines", "Office Manager", "London", 3814, "2008/12/19", 90560], ["Quinn Flynn", "Support Lead", "Edinburgh", 9497, "2013/03/03", 342000], ["Charde Marshall", "Regional Director", "San Francisco", 6741, "2008/10/16", 470600], ["Haley Kennedy", "Senior Marketing Designer", "London", 3597, "2012/12/18", 313500], ["Tatyana Fitzpatrick", "Regional Director", "London", 1965, "2010/03/17", 385750], ["Michael Silva", "Marketing Designer", "London", 1581, "2012/11/27", 198500], ["Paul Byrd", "Chief Financial Officer (CFO)", "New York", 3059, "2010/06/09", 725000], ["Gloria Little", "Systems Administrator", "New York", 1721, "2009/04/10", 237500], ["Bradley Greer", "Software Engineer", "London", 2558, "2012/10/13", 132000], ["Dai Rios", "Personnel Lead", "Edinburgh", 2290, "2012/09/26", 217500], ["Jenette Caldwell", "Development Lead", "New York", 1937, "2011/09/03", 345000], ["Yuri Berry", "Chief Marketing Officer (CMO)", "New York", 6154, "2009/06/25", 675000], ["Caesar Vance", "Pre-Sales Support", "New York", 8330, "2011/12/12", 106450], ["Doris Wilder", "Sales Assistant", "Sidney", 3023, "2010/09/20", 85600], ["Angelica Ramos", "Chief Executive Officer (CEO)", "London", 5797, "2009/10/09", 1200000], ["Gavin Joyce", "Developer", "Edinburgh", 8822, "2010/12/22", 92575], ["Jennifer Chang", "Regional Director", "Singapore", 9239, "2010/11/14", 357650], ["Brenden Wagner", "Software Engineer", "San Francisco", 1314, "2011/06/07", 206850], ["Fiona Green", "Chief Operating Officer (COO)", "San Francisco", 2947, "2010/03/11", 850000], ["Shou Itou", "Regional Marketing", "Tokyo", 8899, "2011/08/14", 163000], ["Michelle House", "Integration Specialist", "Sidney", 2769, "2011/06/02", 95400], ["Suki Burks", "Developer", "London", 6832, "2009/10/22", 114500], ["Prescott Bartlett", "Technical Author", "London", 3606, "2011/05/07", 145000], ["Gavin Cortez", "Team Leader", "San Francisco", 2860, "2008/10/26", 235500], ["Martena Mccray", "Post-Sales support", "Edinburgh", 8240, "2011/03/09", 324050], ["Unity Butler", "Marketing Designer", "San Francisco", 5384, "2009/12/09", 85675], ["Howard Hatfield", "Office Manager", "San Francisco", 7031, "2008/12/16", 164500], ["Hope Fuentes", "Secretary", "San Francisco", 6318, "2010/02/12", 109850], ["Vivian Harrell", "Financial Controller", "San Francisco", 9422, "2009/02/14", 452500], ["Timothy Mooney", "Office Manager", "London", 7580, "2008/12/11", 136200], ["Jackson Bradshaw", "Director", "New York", 1042, "2008/09/26", 645750], ["Olivia Liang", "Support Engineer", "Singapore", 2120, "2011/02/03", 234500], ["Bruno Nash", "Software Engineer", "London", 6222, "2011/05/03", 163500], ["Sakura Yamamoto", "Support Engineer", "Tokyo", 9383, "2009/08/19", 139575], ["Thor Walton", "Developer", "New York", 8327, "2013/08/11", 98540], ["Finn Camacho", "Support Engineer", "San Francisco", 2927, "2009/07/07", 87500], ["Serge Baldwin", "Data Coordinator", "Singapore", 8352, "2012/04/09", 138575], ["Zenaida Frank", "Software Engineer", "New York", 7439, "2010/01/04", 125250], ["Zorita Serrano", "Software Engineer", "San Francisco", 4389, "2012/06/01", 115000], ["Jennifer Acosta", "Junior Javascript Developer", "Edinburgh", 3431, "2013/02/01", 75650], ["Cara Stevens", "Sales Assistant", "New York", 3990, "2011/12/06", 145600], ["Hermione Butler", "Regional Director", "London", 1016, "2011/03/21", 356250], ["Lael Greer", "Systems Administrator", "London", 6733, "2009/02/27", 103500], ["Jonas Alexander", "Developer", "San Francisco", 8196, "2010/07/14", 86500], ["Shad Decker", "Regional Director", "Edinburgh", 6373, "2008/11/13", 183000], ["Michael Bruce", "Javascript Developer", "Singapore", 5384, "2011/06/27", 183000], ["Donna Snider", "Customer Support", "New York", 4226, "2011/01/25", 112000], ["Donna Snider", "Customer Support", "LOS Angeles", 42.67, "2011/01/25", 112000]];
  154. if (largeData) {
  155. for (let i = 0; i < 10; i++) {
  156. data = data.concat(data);
  157. }
  158. }
  159. // data = data.slice(1, 3)
  160. }
  161. function makeDatatable(treeView = false) {
  162. console.log('No of Rows:', data.length)
  163. const start = performance.now();
  164. var datatable = new DataTable('section', {
  165. checkboxColumn: true,
  166. serialNoColumn: true,
  167. layout: 'fluid',
  168. columns,
  169. data,
  170. inlineFilters: true,
  171. dynamicRowHeight: true,
  172. treeView: treeView,
  173. showTotalRow: true,
  174. // direction: 'rtl',
  175. // language: 'myLang',
  176. // translations: {
  177. // myLang: {
  178. // "Sort Ascending": "Sort low to high",
  179. // "{count} cells copied": {
  180. // "1": "1 cell was copied",
  181. // "2": "2 cells were copied",
  182. // "default": "Many cells were copied"
  183. // }
  184. // }
  185. // },
  186. // filterRows(keyword, cells, colIndex) {
  187. // return cells
  188. // .filter(cell => cell.content.includes(keyword))
  189. // .map(cell => cell.rowIndex);
  190. // },
  191. getEditor(colIndex, rowIndex, value, parent) {
  192. // editing obj only for date field
  193. if (colIndex != 6) return;
  194. const $input = document.createElement('input');
  195. $input.type = 'date';
  196. parent.appendChild($input);
  197. const parse = value => value.replace(/\//g, '-');
  198. const format = value => value.replace(/\-/g, '/');
  199. return {
  200. initValue(value) {
  201. $input.focus();
  202. $input.value = parse(value);
  203. },
  204. setValue(value) {
  205. $input.value = parse(value);
  206. },
  207. getValue() {
  208. return format($input.value);
  209. }
  210. }
  211. },
  212. hooks: {
  213. columnTotal(columnValues, cell) {
  214. if (cell.colIndex === 5) {
  215. // calculated average for 5th column
  216. const sum = columnValues.reduce((acc, value) => {
  217. if (typeof value === 'number') {
  218. return acc + value
  219. }
  220. return acc
  221. }, 0);
  222. return sum / columnValues.length
  223. }
  224. if (cell.colIndex === 2) {
  225. return 'Total'
  226. }
  227. }
  228. }
  229. });
  230. console.log(performance.now() - start);
  231. window.datatable = datatable;
  232. }
  233. window.switchToTreeView = function () {
  234. datatable.destroy();
  235. buildTreeData();
  236. makeDatatable(true);
  237. }
  238. buildData();
  239. makeDatatable();
  240. })
  241. function formatMoney(amount, decimalCount = 2, decimal = ".", thousands = ",") {
  242. try {
  243. decimalCount = Math.abs(decimalCount);
  244. decimalCount = isNaN(decimalCount) ? 2 : decimalCount;
  245. const negativeSign = amount < 0 ? "-" : "";
  246. let i = parseInt(amount = Math.abs(Number(amount) || 0).toFixed(decimalCount)).toString();
  247. let j = (i.length > 3) ? i.length % 3 : 0;
  248. return negativeSign + (j ? i.substr(0, j) + thousands : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousands) + (decimalCount ? decimal + Math.abs(amount - i).toFixed(decimalCount).slice(2) : "");
  249. } catch (e) {
  250. console.log(e)
  251. }
  252. };
  253. </script>
  254. </body>
  255. </html>