설명 없음
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.

app-ecommerce-order-details.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * app-ecommerce-order-details Script
  3. */
  4. 'use strict';
  5. // Datatable (js)
  6. document.addEventListener('DOMContentLoaded', function (e) {
  7. var dt_details_table = document.querySelector('.datatables-order-details');
  8. // E-commerce Products datatable
  9. if (dt_details_table) {
  10. let tableTitle = document.createElement('h5');
  11. tableTitle.classList.add('card-title', 'mb-0');
  12. tableTitle.innerHTML = 'Order details';
  13. let tableEdit = document.createElement('h6');
  14. tableEdit.classList.add('m-0');
  15. tableEdit.innerHTML = '<a href=" javascript:void(0)">Edit</a>';
  16. var dt_products = new DataTable(dt_details_table, {
  17. ajax: assetsPath + 'json/ecommerce-order-details.json', // JSON file to add data
  18. columns: [
  19. // columns according to JSON
  20. { data: 'id' },
  21. { data: 'id', orderable: false, render: DataTable.render.select() },
  22. { data: 'product_name' },
  23. { data: 'price' },
  24. { data: 'qty' },
  25. { data: 'id' }
  26. ],
  27. columnDefs: [
  28. {
  29. // For Responsive
  30. className: 'control',
  31. searchable: false,
  32. orderable: false,
  33. responsivePriority: 2,
  34. targets: 0,
  35. render: function (data, type, full, meta) {
  36. return '';
  37. }
  38. },
  39. {
  40. // For Checkboxes
  41. targets: 1,
  42. orderable: false,
  43. searchable: false,
  44. responsivePriority: 3,
  45. checkboxes: true,
  46. render: function () {
  47. return '<input type="checkbox" class="dt-checkboxes form-check-input">';
  48. },
  49. checkboxes: {
  50. selectAllRender: '<input type="checkbox" class="form-check-input">'
  51. }
  52. },
  53. {
  54. targets: 2,
  55. responsivePriority: 1,
  56. searchable: false,
  57. orderable: false,
  58. render: function (data, type, full, meta) {
  59. const name = full['product_name'];
  60. const productBrand = full['product_info'];
  61. const image = full['image'];
  62. let output;
  63. if (image) {
  64. // For Product image
  65. output = `
  66. <img src="${assetsPath}img/products/${image}" alt="product-${name}" class="rounded-2">
  67. `;
  68. } else {
  69. // For Product badge
  70. const stateNum = Math.floor(Math.random() * 6);
  71. const states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
  72. const state = states[stateNum];
  73. const initials = (name.match(/\b\w/g) || []).slice(0, 2).join('').toUpperCase();
  74. output = `<span class="avatar-initial rounded-2 bg-label-${state}">${initials}</span>`;
  75. }
  76. // Creates full output for Product name and product info
  77. const rowOutput = `
  78. <div class="d-flex justify-content-start align-items-center text-nowrap">
  79. <div class="avatar-wrapper">
  80. <div class="avatar me-3">${output}</div>
  81. </div>
  82. <div class="d-flex flex-column">
  83. <h6 class="text-body mb-0">${name}</h6>
  84. <small>${productBrand}</small>
  85. </div>
  86. </div>`;
  87. return rowOutput;
  88. }
  89. },
  90. {
  91. // For Price
  92. targets: 3,
  93. searchable: false,
  94. orderable: false,
  95. render: function (data, type, full, meta) {
  96. const price = full['price'];
  97. const output = '<span>$' + price + '</span>';
  98. return output;
  99. }
  100. },
  101. {
  102. // For Qty
  103. targets: 4,
  104. searchable: false,
  105. orderable: false,
  106. render: function (data, type, full, meta) {
  107. const qty = full['qty'];
  108. const output = '<span class="text-body">' + qty + '</span>';
  109. return output;
  110. }
  111. },
  112. {
  113. // Total
  114. targets: 5,
  115. searchable: false,
  116. orderable: false,
  117. render: function (data, type, full, meta) {
  118. const total = full['qty'] * full['price'];
  119. const output = '<span class="text-body">' + total + '</span>';
  120. return output;
  121. }
  122. }
  123. ],
  124. select: {
  125. style: 'multi',
  126. selector: 'td:nth-child(2)'
  127. },
  128. order: [2, ''],
  129. layout: {
  130. topStart: {
  131. rowClass: 'row card-header border-bottom mx-0 px-3',
  132. features: [tableTitle]
  133. },
  134. topEnd: {
  135. features: [tableEdit]
  136. },
  137. bottomStart: {
  138. rowClass: 'mt-0',
  139. features: []
  140. },
  141. bottomEnd: {}
  142. },
  143. responsive: {
  144. details: {
  145. display: DataTable.Responsive.display.modal({
  146. header: function (row) {
  147. var data = row.data();
  148. return 'Details of ' + data['product_name'];
  149. }
  150. }),
  151. type: 'column',
  152. renderer: function (api, rowIdx, columns) {
  153. const data = columns
  154. .map(function (col) {
  155. return col.title !== '' // ? Do not show row in modal popup if title is blank (for check box)
  156. ? `<tr data-dt-row="${col.rowIndex}" data-dt-column="${col.columnIndex}">
  157. <td>${col.title}:</td>
  158. <td>${col.data}</td>
  159. </tr>`
  160. : '';
  161. })
  162. .join('');
  163. if (data) {
  164. const div = document.createElement('div');
  165. div.classList.add('table-responsive');
  166. const table = document.createElement('table');
  167. div.appendChild(table);
  168. table.classList.add('table');
  169. const tbody = document.createElement('tbody');
  170. tbody.innerHTML = data;
  171. table.appendChild(tbody);
  172. return div;
  173. }
  174. return false;
  175. }
  176. }
  177. }
  178. });
  179. }
  180. // Filter form control to default size
  181. // ? setTimeout used for order-details table initialization
  182. setTimeout(() => {
  183. const elementsToModify = [
  184. { selector: '.dt-layout-table', classToRemove: 'row mt-2' },
  185. { selector: '.dt-layout-start', classToAdd: 'px-3' },
  186. { selector: '.dt-layout-end', classToAdd: 'px-3' },
  187. { selector: '.dt-layout-full', classToRemove: 'col-md col-12', classToAdd: 'table-responsive' },
  188. { selector: '.dt-layout-full .table', classToAdd: 'mb-0' }
  189. ];
  190. // Delete record
  191. elementsToModify.forEach(({ selector, classToRemove, classToAdd }) => {
  192. document.querySelectorAll(selector).forEach(element => {
  193. if (classToRemove) {
  194. classToRemove.split(' ').forEach(className => element.classList.remove(className));
  195. }
  196. if (classToAdd) {
  197. classToAdd.split(' ').forEach(className => element.classList.add(className));
  198. }
  199. });
  200. });
  201. }, 100);
  202. });
  203. //sweet alert
  204. (function () {
  205. const deleteOrder = document.querySelector('.delete-order');
  206. // Suspend User javascript
  207. if (deleteOrder) {
  208. deleteOrder.onclick = function () {
  209. Swal.fire({
  210. title: 'Are you sure?',
  211. text: "You won't be able to revert order!",
  212. icon: 'warning',
  213. showCancelButton: true,
  214. confirmButtonText: 'Yes, Delete order!',
  215. customClass: {
  216. confirmButton: 'btn btn-primary me-2',
  217. cancelButton: 'btn btn-label-secondary'
  218. },
  219. buttonsStyling: false
  220. }).then(function (result) {
  221. if (result.value) {
  222. Swal.fire({
  223. icon: 'success',
  224. title: 'Deleted!',
  225. text: 'Order has been removed.',
  226. customClass: {
  227. confirmButton: 'btn btn-success'
  228. }
  229. });
  230. } else if (result.dismiss === Swal.DismissReason.cancel) {
  231. Swal.fire({
  232. title: 'Cancelled',
  233. text: 'Cancelled Delete :)',
  234. icon: 'error',
  235. customClass: {
  236. confirmButton: 'btn btn-success'
  237. }
  238. });
  239. }
  240. });
  241. };
  242. }
  243. //for custom year
  244. function getCurrentYear() {
  245. var currentYear = new Date().getFullYear();
  246. return currentYear;
  247. }
  248. var year = getCurrentYear();
  249. document.getElementById('orderYear').innerHTML = year;
  250. })();