No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tables-datatables-extensions.js 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. /**
  2. * DataTables Extensions (js)
  3. */
  4. 'use strict';
  5. document.addEventListener('DOMContentLoaded', function (e) {
  6. const dt_scrollable_table = document.querySelector('.dt-scrollableTable');
  7. let dt_scrollableTable;
  8. // Scrollable
  9. // --------------------------------------------------------------------
  10. if (dt_scrollable_table) {
  11. dt_scrollableTable = new DataTable(dt_scrollable_table, {
  12. ajax: assetsPath + 'json/table-datatable.json',
  13. columns: [
  14. { data: 'full_name' },
  15. { data: 'post' },
  16. { data: 'email' },
  17. { data: 'city' },
  18. { data: 'start_date' },
  19. { data: 'salary' },
  20. { data: 'age' },
  21. { data: 'experience' },
  22. { data: '' },
  23. { data: '' }
  24. ],
  25. columnDefs: [
  26. {
  27. // Label
  28. targets: -2,
  29. render: function (data, type, full, meta) {
  30. const statusNumber = full.status;
  31. const statuses = {
  32. 1: { title: 'Current', class: 'bg-label-primary' },
  33. 2: { title: 'Professional', class: 'bg-label-success' },
  34. 3: { title: 'Rejected', class: 'bg-label-danger' },
  35. 4: { title: 'Resigned', class: 'bg-label-warning' },
  36. 5: { title: 'Applied', class: 'bg-label-info' }
  37. };
  38. if (typeof statuses[statusNumber] === 'undefined') {
  39. return data;
  40. }
  41. return `
  42. <span class="badge ${statuses[statusNumber].class}">
  43. ${statuses[statusNumber].title}
  44. </span>
  45. `;
  46. }
  47. },
  48. {
  49. // Actions
  50. targets: -1,
  51. title: 'Actions',
  52. searchable: false,
  53. className: 'd-flex align-items-center',
  54. orderable: false,
  55. render: function (data, type, full, meta) {
  56. return (
  57. '<div class="d-inline-block">' +
  58. '<a href="javascript:;" class="btn btn-icon dropdown-toggle hide-arrow" data-bs-toggle="dropdown"><i class="bx bx-dots-vertical-rounded icon-base"></i></a>' +
  59. '<div class="dropdown-menu dropdown-menu-end m-0">' +
  60. '<a href="javascript:;" class="dropdown-item">Details</a>' +
  61. '<a href="javascript:;" class="dropdown-item">Archive</a>' +
  62. '<div class="dropdown-divider"></div>' +
  63. '<a href="javascript:;" class="dropdown-item text-danger delete-record">Delete</a>' +
  64. '</div>' +
  65. '</div>' +
  66. '<a href="javascript:;" class="item-edit text-body"><i class="bx bxs-edit icon-base"></i></a>'
  67. );
  68. }
  69. }
  70. ],
  71. // Scroll options
  72. scrollY: '300px',
  73. scrollX: true,
  74. layout: {
  75. topStart: {
  76. rowClass: 'row mx-3 my-0 justify-content-between',
  77. features: [
  78. {
  79. pageLength: {
  80. menu: [7, 10, 25, 50, 100],
  81. text: 'Show_MENU_entries'
  82. }
  83. }
  84. ]
  85. },
  86. topEnd: {
  87. search: {
  88. placeholder: ''
  89. }
  90. },
  91. bottomStart: {
  92. rowClass: 'row mx-3 justify-content-between',
  93. features: ['info']
  94. },
  95. bottomEnd: 'paging'
  96. },
  97. language: {
  98. paginate: {
  99. next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
  100. previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
  101. first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
  102. last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
  103. }
  104. },
  105. initComplete: function (settings, json) {
  106. // Add the mti-n1 class to the first row in tbody
  107. dt_scrollable_table.querySelector('tbody tr:first-child').classList.add('border-top-0');
  108. }
  109. });
  110. }
  111. // FixedHeader
  112. // --------------------------------------------------------------------
  113. const dt_fixedheader_table = document.querySelector('.dt-fixedheader');
  114. let dt_fixedheader;
  115. if (dt_fixedheader_table) {
  116. dt_fixedheader = new DataTable(dt_fixedheader_table, {
  117. ajax: assetsPath + 'json/table-datatable.json',
  118. columns: [
  119. { data: '' },
  120. { data: 'id', orderable: false, render: DataTable.render.select() },
  121. { data: 'id' },
  122. { data: 'full_name' },
  123. { data: 'email' },
  124. { data: 'start_date' },
  125. { data: 'salary' },
  126. { data: 'status' },
  127. { data: '' }
  128. ],
  129. columnDefs: [
  130. {
  131. className: 'control',
  132. orderable: false,
  133. targets: 0,
  134. responsivePriority: 3,
  135. render: function (data, type, full, meta) {
  136. return '';
  137. }
  138. },
  139. {
  140. // For Checkboxes
  141. targets: 1,
  142. orderable: false,
  143. render: function () {
  144. return '<input type="checkbox" class="dt-checkboxes form-check-input">';
  145. },
  146. checkboxes: {
  147. selectAllRender: '<input type="checkbox" class="form-check-input">'
  148. },
  149. responsivePriority: 4
  150. },
  151. {
  152. targets: 2,
  153. visible: false
  154. },
  155. {
  156. // Avatar image/badge, Name and post
  157. targets: 3,
  158. render: function (data, type, full, meta) {
  159. const userImg = full.avatar;
  160. const name = full.full_name;
  161. const post = full.post;
  162. let output;
  163. if (userImg) {
  164. // For Avatar image
  165. output = `<img src="${assetsPath}img/avatars/${userImg}" alt="Avatar" class="rounded-circle">`;
  166. } else {
  167. // For Avatar badge
  168. const stateNum = Math.floor(Math.random() * 6);
  169. const states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
  170. const state = states[stateNum];
  171. const initials = (name.match(/\b\w/g) || []).map(i => i.toUpperCase()).join('');
  172. output = `<span class="avatar-initial rounded-circle bg-label-${state}">${initials}</span>`;
  173. }
  174. // Creates full output for row
  175. const rowOutput = `
  176. <div class="d-flex justify-content-start align-items-center">
  177. <div class="avatar-wrapper">
  178. <div class="avatar me-2">
  179. ${output}
  180. </div>
  181. </div>
  182. <div class="d-flex flex-column">
  183. <span class="emp_name text-truncate">${name}</span>
  184. <small class="emp_post text-truncate text-body-secondary">${post}</small>
  185. </div>
  186. </div>
  187. `;
  188. return rowOutput;
  189. },
  190. responsivePriority: 5
  191. },
  192. {
  193. responsivePriority: 1,
  194. targets: 4
  195. },
  196. {
  197. responsivePriority: 2,
  198. targets: 6
  199. },
  200. {
  201. // Label
  202. targets: -2,
  203. render: function (data, type, full, meta) {
  204. const statusNumber = full.status;
  205. const statuses = {
  206. 1: { title: 'Current', class: 'bg-label-primary' },
  207. 2: { title: 'Professional', class: 'bg-label-success' },
  208. 3: { title: 'Rejected', class: 'bg-label-danger' },
  209. 4: { title: 'Resigned', class: 'bg-label-warning' },
  210. 5: { title: 'Applied', class: 'bg-label-info' }
  211. };
  212. if (typeof statuses[statusNumber] === 'undefined') {
  213. return data;
  214. }
  215. return `
  216. <span class="badge ${statuses[statusNumber].class}">
  217. ${statuses[statusNumber].title}
  218. </span>
  219. `;
  220. }
  221. },
  222. {
  223. // Actions
  224. targets: -1,
  225. title: 'Actions',
  226. className: 'd-flex align-items-center',
  227. orderable: false,
  228. render: function (data, type, full, meta) {
  229. return (
  230. '<div class="d-inline-block">' +
  231. '<a href="javascript:;" class="btn btn-icon dropdown-toggle hide-arrow" data-bs-toggle="dropdown"><i class="bx bx-dots-vertical-rounded icon-base"></i></a>' +
  232. '<div class="dropdown-menu dropdown-menu-end m-0">' +
  233. '<a href="javascript:;" class="dropdown-item">Details</a>' +
  234. '<a href="javascript:;" class="dropdown-item">Archive</a>' +
  235. '<div class="dropdown-divider"></div>' +
  236. '<a href="javascript:;" class="dropdown-item text-danger delete-record">Delete</a>' +
  237. '</div>' +
  238. '</div>' +
  239. '<a href="javascript:;" class="btn btn-icon item-edit"><i class="bx bxs-edit icon-base"></i></a>'
  240. );
  241. }
  242. }
  243. ],
  244. select: {
  245. style: 'multi',
  246. selector: 'td:nth-child(2)'
  247. },
  248. order: [[2, 'desc']],
  249. layout: {
  250. topStart: {
  251. rowClass: 'row mx-3 my-0 justify-content-between',
  252. features: [
  253. {
  254. pageLength: {
  255. menu: [7, 10, 25, 50, 100],
  256. text: 'Show_MENU_entries'
  257. }
  258. }
  259. ]
  260. },
  261. topEnd: {
  262. search: {
  263. placeholder: ''
  264. }
  265. },
  266. bottomStart: {
  267. rowClass: 'row mx-3 justify-content-between',
  268. features: ['info']
  269. },
  270. bottomEnd: 'paging'
  271. },
  272. displayLength: 7,
  273. language: {
  274. paginate: {
  275. next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
  276. previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
  277. first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
  278. last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
  279. }
  280. },
  281. responsive: {
  282. details: {
  283. display: DataTable.Responsive.display.modal({
  284. header: function (row) {
  285. var data = row.data();
  286. return 'Details of ' + data['full_name'];
  287. }
  288. }),
  289. type: 'column',
  290. renderer: function (api, rowIdx, columns) {
  291. const data = columns
  292. .map(function (col) {
  293. return col.title !== '' // Do not show row in modal popup if title is blank (for check box)
  294. ? `<tr data-dt-row="${col.rowIndex}" data-dt-column="${col.columnIndex}">
  295. <td>${col.title}:</td>
  296. <td>${col.data}</td>
  297. </tr>`
  298. : '';
  299. })
  300. .join('');
  301. if (data) {
  302. const div = document.createElement('div');
  303. div.classList.add('table-responsive');
  304. const table = document.createElement('table');
  305. div.appendChild(table);
  306. table.classList.add('table');
  307. const tbody = document.createElement('tbody');
  308. tbody.innerHTML = data;
  309. table.appendChild(tbody);
  310. return div;
  311. }
  312. return false;
  313. }
  314. }
  315. }
  316. });
  317. // Fixed header
  318. if (window.Helpers.isNavbarFixed()) {
  319. const navHeight = document.getElementById('layout-navbar').offsetHeight;
  320. new DataTable.FixedHeader(dt_fixedheader).headerOffset(navHeight);
  321. } else {
  322. new DataTable.FixedHeader(dt_fixedheader);
  323. }
  324. //? The 'delete-record' class is necessary for the functionality of the following code.
  325. document.addEventListener('click', function (e) {
  326. if (e.target.classList.contains('delete-record')) {
  327. dt_fixedheader.row(e.target.closest('tr')).remove().draw();
  328. const modalEl = document.querySelector('.dtr-bs-modal');
  329. if (modalEl && modalEl.classList.contains('show')) {
  330. const modal = bootstrap.Modal.getInstance(modalEl);
  331. modal?.hide();
  332. }
  333. }
  334. });
  335. }
  336. // FixedColumns
  337. // --------------------------------------------------------------------
  338. const dt_fixedcolumns_table = document.querySelector('.dt-fixedcolumns');
  339. let dt_fixedcolumns;
  340. if (dt_fixedcolumns_table) {
  341. let tableTitle = document.createElement('h5');
  342. tableTitle.classList.add('card-title', 'mb-0', 'text-md-start', 'text-center', 'pb-md-0', 'pb-6');
  343. tableTitle.innerHTML = 'Fixed Columns';
  344. dt_fixedcolumns = new DataTable(dt_fixedcolumns_table, {
  345. ajax: assetsPath + 'json/table-datatable.json',
  346. columns: [
  347. { data: 'full_name' },
  348. { data: 'post' },
  349. { data: 'email' },
  350. { data: 'city' },
  351. { data: 'start_date' },
  352. { data: 'salary' },
  353. { data: 'age' },
  354. { data: 'experience' },
  355. { data: 'status' },
  356. { data: 'id' }
  357. ],
  358. columnDefs: [
  359. {
  360. // Label
  361. targets: -2,
  362. render: function (data, type, full, meta) {
  363. const statusNumber = full.status;
  364. const statuses = {
  365. 1: { title: 'Current', class: 'bg-label-primary' },
  366. 2: { title: 'Professional', class: 'bg-label-success' },
  367. 3: { title: 'Rejected', class: 'bg-label-danger' },
  368. 4: { title: 'Resigned', class: 'bg-label-warning' },
  369. 5: { title: 'Applied', class: 'bg-label-info' }
  370. };
  371. if (typeof statuses[statusNumber] === 'undefined') {
  372. return data;
  373. }
  374. return `
  375. <span class="badge ${statuses[statusNumber].class}">
  376. ${statuses[statusNumber].title}
  377. </span>
  378. `;
  379. }
  380. },
  381. {
  382. // Actions
  383. targets: -1,
  384. title: 'Actions',
  385. searchable: false,
  386. className: 'd-flex align-items-center',
  387. orderable: false,
  388. render: function (data, type, full, meta) {
  389. return (
  390. '<div class="d-inline-block">' +
  391. '<a href="javascript:;" class="btn btn-icon dropdown-toggle hide-arrow" data-bs-toggle="dropdown"><i class="bx bx-dots-vertical-rounded icon-base"></i></a>' +
  392. '<div class="dropdown-menu dropdown-menu-end m-0">' +
  393. '<a href="javascript:;" class="dropdown-item">Details</a>' +
  394. '<a href="javascript:;" class="dropdown-item">Archive</a>' +
  395. '<div class="dropdown-divider"></div>' +
  396. '<a href="javascript:;" class="dropdown-item text-danger delete-record"></i>Delete</a>' +
  397. '</div>' +
  398. '</div>' +
  399. '<a href="javascript:;" class="item-edit text-body"><i class="bx bxs-edit icon-base"></i></a>'
  400. );
  401. }
  402. }
  403. ],
  404. layout: {
  405. topStart: {
  406. rowClass: 'row card-header pt-0 pb-0',
  407. features: [tableTitle]
  408. },
  409. topEnd: {
  410. search: {
  411. placeholder: ''
  412. }
  413. },
  414. bottomStart: {
  415. rowClass: 'row mx-3 justify-content-between',
  416. features: ['info']
  417. },
  418. bottomEnd: 'paging'
  419. },
  420. scrollY: 300,
  421. scrollX: true,
  422. scrollCollapse: true,
  423. paging: false,
  424. info: false,
  425. // Fixed column option
  426. fixedColumns: {
  427. start: 1
  428. },
  429. initComplete: function (settings, json) {
  430. // Add the mti-n1 class to the first row in tbody
  431. dt_fixedcolumns_table.querySelector('tbody tr:first-child').classList.add('border-top-0');
  432. }
  433. });
  434. //? The 'delete-record' class is necessary for the functionality of the following code.
  435. document.addEventListener('click', function (e) {
  436. if (e.target.classList.contains('delete-record')) {
  437. dt_fixedcolumns.row(e.target.closest('tr')).remove().draw();
  438. const modalEl = document.querySelector('.dtr-bs-modal');
  439. if (modalEl && modalEl.classList.contains('show')) {
  440. const modal = bootstrap.Modal.getInstance(modalEl);
  441. modal?.hide();
  442. }
  443. }
  444. });
  445. }
  446. // Select
  447. // --------------------------------------------------------------------
  448. const dt_select_table = document.querySelector('.dt-select-table');
  449. let dt_select;
  450. if (dt_select_table) {
  451. dt_select = new DataTable(dt_select_table, {
  452. ajax: assetsPath + 'json/table-datatable.json',
  453. columns: [
  454. { data: 'id', orderable: false, render: DataTable.render.select() },
  455. { data: 'full_name' },
  456. { data: 'post' },
  457. { data: 'email' },
  458. { data: 'city' },
  459. { data: 'start_date' },
  460. { data: 'salary' },
  461. { data: 'status' }
  462. ],
  463. columnDefs: [
  464. {
  465. // For Checkboxes
  466. targets: 0,
  467. searchable: false,
  468. orderable: false,
  469. render: function () {
  470. return '<input type="checkbox" class="dt-checkboxes form-check-input">';
  471. },
  472. checkboxes: {
  473. selectRow: true,
  474. selectAllRender: '<input type="checkbox" class="form-check-input">'
  475. }
  476. },
  477. {
  478. // Label
  479. targets: -1,
  480. render: function (data, type, full, meta) {
  481. const statusNumber = full.status;
  482. const statuses = {
  483. 1: { title: 'Current', class: 'bg-label-primary' },
  484. 2: { title: 'Professional', class: 'bg-label-success' },
  485. 3: { title: 'Rejected', class: 'bg-label-danger' },
  486. 4: { title: 'Resigned', class: 'bg-label-warning' },
  487. 5: { title: 'Applied', class: 'bg-label-info' }
  488. };
  489. if (typeof statuses[statusNumber] === 'undefined') {
  490. return data;
  491. }
  492. return `
  493. <span class="badge ${statuses[statusNumber].class}">
  494. ${statuses[statusNumber].title}
  495. </span>
  496. `;
  497. }
  498. }
  499. ],
  500. order: [[1, 'desc']],
  501. layout: {
  502. topStart: {
  503. rowClass: 'row mx-3 my-0 justify-content-between',
  504. features: [
  505. {
  506. pageLength: {
  507. menu: [7, 10, 25, 50, 100],
  508. text: 'Show_MENU_entries'
  509. }
  510. }
  511. ]
  512. },
  513. topEnd: {
  514. search: {
  515. placeholder: ''
  516. }
  517. },
  518. bottomStart: {
  519. rowClass: 'row mx-3 justify-content-between',
  520. features: ['info']
  521. },
  522. bottomEnd: 'paging'
  523. },
  524. language: {
  525. paginate: {
  526. next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
  527. previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
  528. first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
  529. last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
  530. }
  531. },
  532. select: {
  533. // Select style
  534. style: 'multi'
  535. }
  536. });
  537. }
  538. // Filter form control to default size
  539. // ? setTimeout used for multilingual table initialization
  540. setTimeout(() => {
  541. const elementsToModify = [
  542. { selector: '.dt-search .form-control', classToRemove: 'form-control-sm', classToAdd: 'ms-4' },
  543. { selector: '.dt-length .form-select', classToRemove: 'form-select-sm' },
  544. { selector: '.dt-layout-table', classToRemove: 'row mt-2' },
  545. { selector: '.dt-layout-end', classToAdd: 'mt-0' },
  546. { selector: '.dt-layout-end .dt-search', classToAdd: 'mt-0 mt-md-6' },
  547. { selector: '.dt-layout-full', classToRemove: 'col-md col-12', classToAdd: 'table-responsive' }
  548. ];
  549. // Delete record
  550. elementsToModify.forEach(({ selector, classToRemove, classToAdd }) => {
  551. document.querySelectorAll(selector).forEach(element => {
  552. if (classToRemove) {
  553. classToRemove.split(' ').forEach(className => element.classList.remove(className));
  554. }
  555. if (classToAdd) {
  556. classToAdd.split(' ').forEach(className => element.classList.add(className));
  557. }
  558. });
  559. });
  560. }, 100);
  561. });