Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

app-ecommerce-order-list.js 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /**
  2. * app-ecommerce-order-list Script
  3. */
  4. 'use strict';
  5. // Datatable (js)
  6. document.addEventListener('DOMContentLoaded', function (e) {
  7. let borderColor, bodyBg, headingColor;
  8. borderColor = config.colors.borderColor;
  9. bodyBg = config.colors.bodyBg;
  10. headingColor = config.colors.headingColor;
  11. // Variable declaration for table
  12. const dt_order_table = document.querySelector('.datatables-order'),
  13. statusObj = {
  14. 1: { title: 'Dispatched', class: 'bg-label-warning' },
  15. 2: { title: 'Delivered', class: 'bg-label-success' },
  16. 3: { title: 'Out for Delivery', class: 'bg-label-primary' },
  17. 4: { title: 'Ready to Pickup', class: 'bg-label-info' }
  18. },
  19. paymentObj = {
  20. 1: { title: 'Paid', class: 'text-success' },
  21. 2: { title: 'Pending', class: 'text-warning' },
  22. 3: { title: 'Failed', class: 'text-danger' },
  23. 4: { title: 'Cancelled', class: 'text-secondary' }
  24. };
  25. // E-commerce Products datatable
  26. if (dt_order_table) {
  27. const dt_products = new DataTable(dt_order_table, {
  28. ajax: assetsPath + 'json/ecommerce-customer-order.json', // JSON file to add data
  29. columns: [
  30. // columns according to JSON
  31. { data: 'id' },
  32. { data: 'id', orderable: false, render: DataTable.render.select() },
  33. { data: 'order' },
  34. { data: 'date' },
  35. { data: 'customer' }, //email //avatar
  36. { data: 'payment' },
  37. { data: 'status' },
  38. { data: 'method' }, //method_number
  39. { data: 'id' }
  40. ],
  41. columnDefs: [
  42. {
  43. // For Responsive
  44. className: 'control',
  45. searchable: false,
  46. orderable: false,
  47. responsivePriority: 2,
  48. targets: 0,
  49. render: function (data, type, full, meta) {
  50. return '';
  51. }
  52. },
  53. {
  54. // For Checkboxes
  55. targets: 1,
  56. orderable: false,
  57. searchable: false,
  58. responsivePriority: 3,
  59. checkboxes: true,
  60. render: function () {
  61. return '<input type="checkbox" class="dt-checkboxes form-check-input">';
  62. },
  63. checkboxes: {
  64. selectAllRender: '<input type="checkbox" class="form-check-input">'
  65. }
  66. },
  67. {
  68. // Order ID
  69. targets: 2,
  70. render: function (data, type, full, meta) {
  71. const order_id = full['order'];
  72. // Creates full output for row
  73. const row_output =
  74. '<a href=" ' + baseUrl + 'app/ecommerce/order/details"><span>#' + order_id + '</span></a>';
  75. return row_output;
  76. }
  77. },
  78. {
  79. targets: 3,
  80. render: function (data, type, full, meta) {
  81. const date = new Date(full.date);
  82. const timeX = full['time'].substring(0, 5);
  83. const formattedDate = date.toLocaleDateString('en-US', {
  84. month: 'short',
  85. day: 'numeric',
  86. year: 'numeric'
  87. });
  88. return `<span class="text-nowrap">${formattedDate}, ${timeX}</span>`;
  89. }
  90. },
  91. {
  92. targets: 4,
  93. responsivePriority: 1,
  94. render: function (data, type, full, meta) {
  95. const name = full['customer'];
  96. const email = full['email'];
  97. const avatar = full['avatar'];
  98. let output;
  99. if (avatar) {
  100. // For Avatar image
  101. output = `<img src="${assetsPath}img/avatars/${avatar}" alt="Avatar" class="rounded-circle">`;
  102. } else {
  103. // For Avatar badge
  104. const stateNum = Math.floor(Math.random() * 6);
  105. const states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
  106. const state = states[stateNum];
  107. const initials = (name.match(/\b\w/g) || []).slice(0, 2).join('').toUpperCase();
  108. output = `<span class="avatar-initial rounded-circle bg-label-${state}">${initials}</span>`;
  109. }
  110. // Creates full output for row
  111. const rowOutput = `
  112. <div class="d-flex justify-content-start align-items-center order-name text-nowrap">
  113. <div class="avatar-wrapper">
  114. <div class="avatar avatar-sm me-3">
  115. ${output}
  116. </div>
  117. </div>
  118. <div class="d-flex flex-column">
  119. <h6 class="m-0"><a href="${baseUrl}pages/profile-user" class="text-heading">${name}</a></h6>
  120. <small>${email}</small>
  121. </div>
  122. </div>`;
  123. return rowOutput;
  124. }
  125. },
  126. {
  127. targets: 5,
  128. render: function (data, type, full, meta) {
  129. const payment = full['payment'];
  130. const paymentStatus = paymentObj[payment];
  131. if (paymentStatus) {
  132. return `
  133. <h6 class="mb-0 align-items-center d-flex w-px-100 ${paymentStatus.class}">
  134. <i class="icon-base bx bxs-circle icon-8px me-1"></i>${paymentStatus.title}
  135. </h6>`;
  136. }
  137. return data;
  138. }
  139. },
  140. {
  141. targets: -3,
  142. render: function (data, type, full, meta) {
  143. const status = full['status'];
  144. const statusInfo = statusObj[status];
  145. if (statusInfo) {
  146. return `
  147. <span class="badge px-2 ${statusInfo.class} text-capitalized">
  148. ${statusInfo.title}
  149. </span>`;
  150. }
  151. return data;
  152. }
  153. },
  154. {
  155. targets: -2,
  156. render: function (data, type, full, meta) {
  157. let method = full['method'];
  158. let methodNumber = full['method_number'];
  159. if (method === 'paypal') {
  160. methodNumber = '@gmail.com';
  161. }
  162. return `
  163. <div class="d-flex align-items-center text-nowrap">
  164. <img src="${assetsPath}img/icons/payments/${method}.png" alt="${method}" width="29">
  165. <span><i class="icon-base bx bx-dots-horizontal-rounded mt-1"></i>${methodNumber}</span>
  166. </div>`;
  167. }
  168. },
  169. {
  170. targets: -1,
  171. title: 'Actions',
  172. searchable: false,
  173. orderable: false,
  174. render: function (data, type, full, meta) {
  175. return `
  176. <div class="d-flex justify-content-sm-start align-items-sm-center">
  177. <button class="btn btn-icon dropdown-toggle hide-arrow" data-bs-toggle="dropdown">
  178. <i class="icon-base bx bx-dots-vertical-rounded icon-md"></i>
  179. </button>
  180. <div class="dropdown-menu dropdown-menu-end m-0">
  181. <a href="${baseUrl}app/ecommerce/order/details" class="dropdown-item">View</a>
  182. <a href="javascript:void(0);" class="dropdown-item delete-record">Delete</a>
  183. </div>
  184. </div>`;
  185. }
  186. }
  187. ],
  188. select: {
  189. style: 'multi',
  190. selector: 'td:nth-child(2)'
  191. },
  192. order: [3, 'asc'],
  193. layout: {
  194. topStart: {
  195. search: {
  196. placeholder: 'Search Order',
  197. text: '_INPUT_'
  198. }
  199. },
  200. topEnd: {
  201. rowClass: 'row ms-3 my-0 justify-content-between',
  202. features: [
  203. {
  204. pageLength: {
  205. menu: [7, 10, 25, 50, 100],
  206. text: '_MENU_'
  207. }
  208. },
  209. {
  210. buttons: [
  211. {
  212. extend: 'collection',
  213. className: 'btn btn-label-primary dropdown-toggle',
  214. text: '<span class="d-flex align-items-center gap-2"><i class="icon-base bx bx-export icon-sm"></i> <span class="d-none d-sm-inline-block">Export</span></span>',
  215. buttons: [
  216. {
  217. extend: 'print',
  218. text: `<span class="d-flex align-items-center"><i class="icon-base bx bx-printer me-1"></i>Print</span>`,
  219. className: 'dropdown-item',
  220. exportOptions: {
  221. columns: [3, 4, 5, 6, 7],
  222. format: {
  223. body: function (inner, coldex, rowdex) {
  224. if (inner.length <= 0) return inner;
  225. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  226. let result = '';
  227. el.forEach(item => {
  228. if (item.classList && item.classList.contains('user-name')) {
  229. result += item.lastChild.firstChild.textContent;
  230. } else {
  231. result += item.textContent || item.innerText || '';
  232. }
  233. });
  234. return result;
  235. }
  236. }
  237. },
  238. customize: function (win) {
  239. win.document.body.style.color = headingColor;
  240. win.document.body.style.borderColor = borderColor;
  241. win.document.body.style.backgroundColor = bodyBg;
  242. const table = win.document.body.querySelector('table');
  243. table.classList.add('compact');
  244. table.style.color = 'inherit';
  245. table.style.borderColor = 'inherit';
  246. table.style.backgroundColor = 'inherit';
  247. }
  248. },
  249. {
  250. extend: 'csv',
  251. text: `<span class="d-flex align-items-center"><i class="icon-base bx bx-file me-1"></i>Csv</span>`,
  252. className: 'dropdown-item',
  253. exportOptions: {
  254. columns: [3, 4, 5, 6, 7],
  255. format: {
  256. body: function (inner, coldex, rowdex) {
  257. if (inner.length <= 0) return inner;
  258. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  259. let result = '';
  260. el.forEach(item => {
  261. if (item.classList && item.classList.contains('user-name')) {
  262. result += item.lastChild.firstChild.textContent;
  263. } else {
  264. result += item.textContent || item.innerText || '';
  265. }
  266. });
  267. return result;
  268. }
  269. }
  270. }
  271. },
  272. {
  273. extend: 'excel',
  274. text: `<span class="d-flex align-items-center"><i class="icon-base bx bxs-file-export me-1"></i>Excel</span>`,
  275. className: 'dropdown-item',
  276. exportOptions: {
  277. columns: [3, 4, 5, 6, 7],
  278. format: {
  279. body: function (inner, coldex, rowdex) {
  280. if (inner.length <= 0) return inner;
  281. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  282. let result = '';
  283. el.forEach(item => {
  284. if (item.classList && item.classList.contains('user-name')) {
  285. result += item.lastChild.firstChild.textContent;
  286. } else {
  287. result += item.textContent || item.innerText || '';
  288. }
  289. });
  290. return result;
  291. }
  292. }
  293. }
  294. },
  295. {
  296. extend: 'pdf',
  297. text: `<span class="d-flex align-items-center"><i class="icon-base bx bxs-file-pdf me-1"></i>Pdf</span>`,
  298. className: 'dropdown-item',
  299. exportOptions: {
  300. columns: [3, 4, 5, 6, 7],
  301. format: {
  302. body: function (inner, coldex, rowdex) {
  303. if (inner.length <= 0) return inner;
  304. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  305. let result = '';
  306. el.forEach(item => {
  307. if (item.classList && item.classList.contains('user-name')) {
  308. result += item.lastChild.firstChild.textContent;
  309. } else {
  310. result += item.textContent || item.innerText || '';
  311. }
  312. });
  313. return result;
  314. }
  315. }
  316. }
  317. },
  318. {
  319. extend: 'copy',
  320. text: `<i class="icon-base bx bx-copy me-1"></i>Copy`,
  321. className: 'dropdown-item',
  322. exportOptions: {
  323. columns: [3, 4, 5, 6, 7],
  324. format: {
  325. body: function (inner, coldex, rowdex) {
  326. if (inner.length <= 0) return inner;
  327. const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
  328. let result = '';
  329. el.forEach(item => {
  330. if (item.classList && item.classList.contains('user-name')) {
  331. result += item.lastChild.firstChild.textContent;
  332. } else {
  333. result += item.textContent || item.innerText || '';
  334. }
  335. });
  336. return result;
  337. }
  338. }
  339. }
  340. }
  341. ]
  342. }
  343. ]
  344. }
  345. ]
  346. },
  347. bottomStart: {
  348. rowClass: 'row mx-3 justify-content-between',
  349. features: ['info']
  350. },
  351. bottomEnd: 'paging'
  352. },
  353. language: {
  354. paginate: {
  355. next: '<i class="icon-base bx bx-chevron-right scaleX-n1-rtl icon-18px"></i>',
  356. previous: '<i class="icon-base bx bx-chevron-left scaleX-n1-rtl icon-18px"></i>',
  357. first: '<i class="icon-base bx bx-chevrons-left scaleX-n1-rtl icon-18px"></i>',
  358. last: '<i class="icon-base bx bx-chevrons-right scaleX-n1-rtl icon-18px"></i>'
  359. }
  360. },
  361. // For responsive popup
  362. responsive: {
  363. details: {
  364. display: DataTable.Responsive.display.modal({
  365. header: function (row) {
  366. const data = row.data();
  367. return 'Details of ' + data['customer'];
  368. }
  369. }),
  370. type: 'column',
  371. renderer: function (api, rowIdx, columns) {
  372. const data = columns
  373. .map(function (col) {
  374. return col.title !== '' // Do not show row in modal popup if title is blank (for check box)
  375. ? `<tr data-dt-row="${col.rowIndex}" data-dt-column="${col.columnIndex}">
  376. <td>${col.title}:</td>
  377. <td>${col.data}</td>
  378. </tr>`
  379. : '';
  380. })
  381. .join('');
  382. if (data) {
  383. const div = document.createElement('div');
  384. div.classList.add('table-responsive');
  385. const table = document.createElement('table');
  386. div.appendChild(table);
  387. table.classList.add('table');
  388. const tbody = document.createElement('tbody');
  389. tbody.innerHTML = data;
  390. table.appendChild(tbody);
  391. return div;
  392. }
  393. return false;
  394. }
  395. }
  396. }
  397. });
  398. //? The 'delete-record' class is necessary for the functionality of the following code.
  399. document.addEventListener('click', function (e) {
  400. if (e.target.classList.contains('delete-record')) {
  401. dt_products.row(e.target.closest('tr')).remove().draw();
  402. const modalEl = document.querySelector('.dtr-bs-modal');
  403. if (modalEl && modalEl.classList.contains('show')) {
  404. const modal = bootstrap.Modal.getInstance(modalEl);
  405. modal?.hide();
  406. }
  407. }
  408. });
  409. }
  410. // Filter form control to default size
  411. // ? setTimeout used for order-list table initialization
  412. setTimeout(() => {
  413. const elementsToModify = [
  414. { selector: '.dt-buttons .btn', classToRemove: 'btn-secondary', classToAdd: 'btn-label-secondary' },
  415. { selector: '.dt-search .form-control', classToRemove: 'form-control-sm', classToAdd: 'ms-0' },
  416. { selector: '.dt-length .form-select', classToRemove: 'form-select-sm' },
  417. { selector: '.dt-length', classToAdd: 'mt-md-6 mt-0' },
  418. { selector: '.dt-layout-table', classToRemove: 'row mt-2' },
  419. { selector: '.dt-layout-end', classToAdd: 'px-3 mt-0' },
  420. {
  421. selector: '.dt-layout-end .dt-buttons',
  422. classToAdd: 'gap-2 px-3 mt-0 mb-md-0 mb-6'
  423. },
  424. {
  425. selector: '.dt-layout-end .dt-buttons .btn-group',
  426. classToAdd: 'mx-auto'
  427. },
  428. { selector: '.dt-layout-start', classToAdd: 'px-3 mt-0' },
  429. { selector: '.dt-layout-full', classToRemove: 'col-md col-12', classToAdd: 'table-responsive' }
  430. ];
  431. // Delete record
  432. elementsToModify.forEach(({ selector, classToRemove, classToAdd }) => {
  433. document.querySelectorAll(selector).forEach(element => {
  434. if (classToRemove) {
  435. classToRemove.split(' ').forEach(className => element.classList.remove(className));
  436. }
  437. if (classToAdd) {
  438. classToAdd.split(' ').forEach(className => element.classList.add(className));
  439. }
  440. });
  441. });
  442. }, 100);
  443. });