/**
* app-ecommerce-order-list Script
*/
'use strict';
// Datatable (js)
document.addEventListener('DOMContentLoaded', function (e) {
let borderColor, bodyBg, headingColor;
borderColor = config.colors.borderColor;
bodyBg = config.colors.bodyBg;
headingColor = config.colors.headingColor;
// Variable declaration for table
const dt_order_table = document.querySelector('.datatables-order'),
statusObj = {
1: { title: 'Dispatched', class: 'bg-label-warning' },
2: { title: 'Delivered', class: 'bg-label-success' },
3: { title: 'Out for Delivery', class: 'bg-label-primary' },
4: { title: 'Ready to Pickup', class: 'bg-label-info' }
},
paymentObj = {
1: { title: 'Paid', class: 'text-success' },
2: { title: 'Pending', class: 'text-warning' },
3: { title: 'Failed', class: 'text-danger' },
4: { title: 'Cancelled', class: 'text-secondary' }
};
// E-commerce Products datatable
if (dt_order_table) {
const dt_products = new DataTable(dt_order_table, {
ajax: assetsPath + 'json/ecommerce-customer-order.json', // JSON file to add data
columns: [
// columns according to JSON
{ data: 'id' },
{ data: 'id', orderable: false, render: DataTable.render.select() },
{ data: 'order' },
{ data: 'date' },
{ data: 'customer' }, //email //avatar
{ data: 'payment' },
{ data: 'status' },
{ data: 'method' }, //method_number
{ data: 'id' }
],
columnDefs: [
{
// For Responsive
className: 'control',
searchable: false,
orderable: false,
responsivePriority: 2,
targets: 0,
render: function (data, type, full, meta) {
return '';
}
},
{
// For Checkboxes
targets: 1,
orderable: false,
searchable: false,
responsivePriority: 3,
checkboxes: true,
render: function () {
return '';
},
checkboxes: {
selectAllRender: ''
}
},
{
// Order ID
targets: 2,
render: function (data, type, full, meta) {
const order_id = full['order'];
// Creates full output for row
const row_output =
'#' + order_id + '';
return row_output;
}
},
{
targets: 3,
render: function (data, type, full, meta) {
const date = new Date(full.date);
const timeX = full['time'].substring(0, 5);
const formattedDate = date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
});
return `${formattedDate}, ${timeX}`;
}
},
{
targets: 4,
responsivePriority: 1,
render: function (data, type, full, meta) {
const name = full['customer'];
const email = full['email'];
const avatar = full['avatar'];
let output;
if (avatar) {
// For Avatar image
output = `
`;
} else {
// For Avatar badge
const stateNum = Math.floor(Math.random() * 6);
const states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
const state = states[stateNum];
const initials = (name.match(/\b\w/g) || []).slice(0, 2).join('').toUpperCase();
output = `${initials}`;
}
// Creates full output for row
const rowOutput = `
`;
return rowOutput;
}
},
{
targets: 5,
render: function (data, type, full, meta) {
const payment = full['payment'];
const paymentStatus = paymentObj[payment];
if (paymentStatus) {
return `
${paymentStatus.title}
`;
}
return data;
}
},
{
targets: -3,
render: function (data, type, full, meta) {
const status = full['status'];
const statusInfo = statusObj[status];
if (statusInfo) {
return `
${statusInfo.title}
`;
}
return data;
}
},
{
targets: -2,
render: function (data, type, full, meta) {
let method = full['method'];
let methodNumber = full['method_number'];
if (method === 'paypal') {
methodNumber = '@gmail.com';
}
return `
${methodNumber}
`;
}
},
{
targets: -1,
title: 'Actions',
searchable: false,
orderable: false,
render: function (data, type, full, meta) {
return `
`;
}
}
],
select: {
style: 'multi',
selector: 'td:nth-child(2)'
},
order: [3, 'asc'],
layout: {
topStart: {
search: {
placeholder: 'Search Order',
text: '_INPUT_'
}
},
topEnd: {
rowClass: 'row ms-3 my-0 justify-content-between',
features: [
{
pageLength: {
menu: [7, 10, 25, 50, 100],
text: '_MENU_'
}
},
{
buttons: [
{
extend: 'collection',
className: 'btn btn-label-primary dropdown-toggle',
text: ' Export',
buttons: [
{
extend: 'print',
text: `Print`,
className: 'dropdown-item',
exportOptions: {
columns: [3, 4, 5, 6, 7],
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
let result = '';
el.forEach(item => {
if (item.classList && item.classList.contains('user-name')) {
result += item.lastChild.firstChild.textContent;
} else {
result += item.textContent || item.innerText || '';
}
});
return result;
}
}
},
customize: function (win) {
win.document.body.style.color = headingColor;
win.document.body.style.borderColor = borderColor;
win.document.body.style.backgroundColor = bodyBg;
const table = win.document.body.querySelector('table');
table.classList.add('compact');
table.style.color = 'inherit';
table.style.borderColor = 'inherit';
table.style.backgroundColor = 'inherit';
}
},
{
extend: 'csv',
text: `Csv`,
className: 'dropdown-item',
exportOptions: {
columns: [3, 4, 5, 6, 7],
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
let result = '';
el.forEach(item => {
if (item.classList && item.classList.contains('user-name')) {
result += item.lastChild.firstChild.textContent;
} else {
result += item.textContent || item.innerText || '';
}
});
return result;
}
}
}
},
{
extend: 'excel',
text: `Excel`,
className: 'dropdown-item',
exportOptions: {
columns: [3, 4, 5, 6, 7],
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
let result = '';
el.forEach(item => {
if (item.classList && item.classList.contains('user-name')) {
result += item.lastChild.firstChild.textContent;
} else {
result += item.textContent || item.innerText || '';
}
});
return result;
}
}
}
},
{
extend: 'pdf',
text: `Pdf`,
className: 'dropdown-item',
exportOptions: {
columns: [3, 4, 5, 6, 7],
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
let result = '';
el.forEach(item => {
if (item.classList && item.classList.contains('user-name')) {
result += item.lastChild.firstChild.textContent;
} else {
result += item.textContent || item.innerText || '';
}
});
return result;
}
}
}
},
{
extend: 'copy',
text: `Copy`,
className: 'dropdown-item',
exportOptions: {
columns: [3, 4, 5, 6, 7],
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
const el = new DOMParser().parseFromString(inner, 'text/html').body.childNodes;
let result = '';
el.forEach(item => {
if (item.classList && item.classList.contains('user-name')) {
result += item.lastChild.firstChild.textContent;
} else {
result += item.textContent || item.innerText || '';
}
});
return result;
}
}
}
}
]
}
]
}
]
},
bottomStart: {
rowClass: 'row mx-3 justify-content-between',
features: ['info']
},
bottomEnd: 'paging'
},
language: {
paginate: {
next: '',
previous: '',
first: '',
last: ''
}
},
// For responsive popup
responsive: {
details: {
display: DataTable.Responsive.display.modal({
header: function (row) {
const data = row.data();
return 'Details of ' + data['customer'];
}
}),
type: 'column',
renderer: function (api, rowIdx, columns) {
const data = columns
.map(function (col) {
return col.title !== '' // Do not show row in modal popup if title is blank (for check box)
? `
| ${col.title}: |
${col.data} |
`
: '';
})
.join('');
if (data) {
const div = document.createElement('div');
div.classList.add('table-responsive');
const table = document.createElement('table');
div.appendChild(table);
table.classList.add('table');
const tbody = document.createElement('tbody');
tbody.innerHTML = data;
table.appendChild(tbody);
return div;
}
return false;
}
}
}
});
//? The 'delete-record' class is necessary for the functionality of the following code.
document.addEventListener('click', function (e) {
if (e.target.classList.contains('delete-record')) {
dt_products.row(e.target.closest('tr')).remove().draw();
const modalEl = document.querySelector('.dtr-bs-modal');
if (modalEl && modalEl.classList.contains('show')) {
const modal = bootstrap.Modal.getInstance(modalEl);
modal?.hide();
}
}
});
}
// Filter form control to default size
// ? setTimeout used for order-list table initialization
setTimeout(() => {
const elementsToModify = [
{ selector: '.dt-buttons .btn', classToRemove: 'btn-secondary', classToAdd: 'btn-label-secondary' },
{ selector: '.dt-search .form-control', classToRemove: 'form-control-sm', classToAdd: 'ms-0' },
{ selector: '.dt-length .form-select', classToRemove: 'form-select-sm' },
{ selector: '.dt-length', classToAdd: 'mt-md-6 mt-0' },
{ selector: '.dt-layout-table', classToRemove: 'row mt-2' },
{ selector: '.dt-layout-end', classToAdd: 'px-3 mt-0' },
{
selector: '.dt-layout-end .dt-buttons',
classToAdd: 'gap-2 px-3 mt-0 mb-md-0 mb-6'
},
{
selector: '.dt-layout-end .dt-buttons .btn-group',
classToAdd: 'mx-auto'
},
{ selector: '.dt-layout-start', classToAdd: 'px-3 mt-0' },
{ selector: '.dt-layout-full', classToRemove: 'col-md col-12', classToAdd: 'table-responsive' }
];
// Delete record
elementsToModify.forEach(({ selector, classToRemove, classToAdd }) => {
document.querySelectorAll(selector).forEach(element => {
if (classToRemove) {
classToRemove.split(' ').forEach(className => element.classList.remove(className));
}
if (classToAdd) {
classToAdd.split(' ').forEach(className => element.classList.add(className));
}
});
});
}, 100);
});