/**
* app-ecommerce-order-list Script
*/
'use strict';
// Datatable (jquery)
$(function () {
let borderColor, bodyBg, headingColor;
if (isDarkStyle) {
borderColor = config.colors_dark.borderColor;
bodyBg = config.colors_dark.bodyBg;
headingColor = config.colors_dark.headingColor;
} else {
borderColor = config.colors.borderColor;
bodyBg = config.colors.bodyBg;
headingColor = config.colors.headingColor;
}
// Variable declaration for table
var dt_order_table = $('.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.length) {
var dt_products = dt_order_table.DataTable({
ajax: assetsPath + 'json/ecommerce-customer-order.json', // JSON file to add data
columns: [
// columns according to JSON
{ data: 'id' },
{ data: 'id' },
{ data: 'order' },
{ data: 'date' },
{ data: 'customer' }, //email //avatar
{ data: 'payment' },
{ data: 'status' },
{ data: 'method' }, //method_number
{ data: '' }
],
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,
checkboxes: {
selectAllRender: ''
},
render: function () {
return '';
},
searchable: false
},
{
// Order ID
targets: 2,
render: function (data, type, full, meta) {
var $order_id = full['order'];
// Creates full output for row
var $row_output = '#' + $order_id + '';
return $row_output;
}
},
{
// Date and Time
targets: 3,
render: function (data, type, full, meta) {
var date = new Date(full.date); // convert the date string to a Date object
var timeX = full['time'].substring(0, 5);
var formattedDate = date.toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric',
time: 'numeric'
});
return '' + formattedDate + ', ' + timeX + '';
}
},
{
// Customers
targets: 4,
responsivePriority: 1,
render: function (data, type, full, meta) {
var $name = full['customer'],
$email = full['email'],
$avatar = full['avatar'];
if ($avatar) {
// For Avatar image
var $output =
'
';
} else {
// For Avatar badge
var stateNum = Math.floor(Math.random() * 6);
var states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary'];
var $state = states[stateNum],
$name = full['customer'],
$initials = $name.match(/\b\w/g) || [];
$initials = (($initials.shift() || '') + ($initials.pop() || '')).toUpperCase();
$output = '' + $initials + '';
}
// Creates full output for row
var $row_output =
'
' +
'
' +
'
' +
$output +
'
' +
'
' +
'
' +
'
';
return $row_output;
}
},
{
targets: 5,
render: function (data, type, full, meta) {
var $payment = full['payment'],
$paymentObj = paymentObj[$payment];
if ($paymentObj) {
return (
'' +
'' +
$paymentObj.title +
'
'
);
}
return data;
}
},
{
// Status
targets: -3,
render: function (data, type, full, meta) {
var $status = full['status'];
return (
'' +
statusObj[$status].title +
''
);
}
},
{
// Payment Method
targets: -2,
render: function (data, type, full, meta) {
var $method = full['method'];
var $method_number = full['method_number'];
if ($method == 'paypal') {
$method_number = '@gmail.com';
}
return (
'' +
'

' +
'
' +
$method_number +
'' +
'
'
);
}
},
{
// Actions
targets: -1,
title: 'Actions',
searchable: false,
orderable: false,
render: function (data, type, full, meta) {
return (
'' +
'' +
'' +
'
'
);
}
}
],
order: [3, 'asc'], //set any columns order asc/desc
dom:
'<"card-header d-flex flex-column flex-md-row align-items-start align-items-md-center pb-md-0 pt-0"<"d-flex align-items-md-center justify-content-md-end gap-4"l<"dt-action-buttons"B>>' +
'>t' +
'<"row mx-1"' +
'<"col-sm-12 col-md-6"i>' +
'<"col-sm-12 col-md-6"p>' +
'>',
lengthMenu: [10, 40, 60, 80, 100], //for length of menu
language: {
sLengthMenu: '_MENU_',
search: '',
searchPlaceholder: 'Search Order',
info: 'Displaying _START_ to _END_ of _TOTAL_ entries'
},
// Buttons with Dropdown
buttons: [
{
extend: 'collection',
className: 'btn btn-outline-secondary dropdown-toggle waves-effect waves-light',
text: ' Export',
buttons: [
{
extend: 'print',
text: 'Print',
className: 'dropdown-item',
exportOptions: {
columns: [1, 2, 3, 4, 5],
// prevent avatar to be print
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
var el = $.parseHTML(inner);
var result = '';
$.each(el, function (index, item) {
if (item.classList !== undefined && item.classList.contains('user-name')) {
result = result + item.lastChild.firstChild.textContent;
} else if (item.innerText === undefined) {
result = result + item.textContent;
} else result = result + item.innerText;
});
return result;
}
}
},
customize: function (win) {
//customize print view for dark
$(win.document.body)
.css('color', headingColor)
.css('border-color', borderColor)
.css('background-color', bodyBg);
$(win.document.body)
.find('table')
.addClass('compact')
.css('color', 'inherit')
.css('border-color', 'inherit')
.css('background-color', 'inherit');
}
},
{
extend: 'csv',
text: 'Csv',
className: 'dropdown-item',
exportOptions: {
columns: [1, 2, 3, 4, 5],
// prevent avatar to be display
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
var el = $.parseHTML(inner);
var result = '';
$.each(el, function (index, item) {
if (item.classList !== undefined && item.classList.contains('user-name')) {
result = result + item.lastChild.firstChild.textContent;
} else if (item.innerText === undefined) {
result = result + item.textContent;
} else result = result + item.innerText;
});
return result;
}
}
}
},
{
extend: 'excel',
text: 'Excel',
className: 'dropdown-item',
exportOptions: {
columns: [1, 2, 3, 4, 5],
// prevent avatar to be display
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
var el = $.parseHTML(inner);
var result = '';
$.each(el, function (index, item) {
if (item.classList !== undefined && item.classList.contains('user-name')) {
result = result + item.lastChild.firstChild.textContent;
} else if (item.innerText === undefined) {
result = result + item.textContent;
} else result = result + item.innerText;
});
return result;
}
}
}
},
{
extend: 'pdf',
text: 'Pdf',
className: 'dropdown-item',
exportOptions: {
columns: [1, 2, 3, 4, 5],
// prevent avatar to be display
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
var el = $.parseHTML(inner);
var result = '';
$.each(el, function (index, item) {
if (item.classList !== undefined && item.classList.contains('user-name')) {
result = result + item.lastChild.firstChild.textContent;
} else if (item.innerText === undefined) {
result = result + item.textContent;
} else result = result + item.innerText;
});
return result;
}
}
}
},
{
extend: 'copy',
text: 'Copy',
className: 'dropdown-item',
exportOptions: {
columns: [1, 2, 3, 4, 5],
// prevent avatar to be display
format: {
body: function (inner, coldex, rowdex) {
if (inner.length <= 0) return inner;
var el = $.parseHTML(inner);
var result = '';
$.each(el, function (index, item) {
if (item.classList !== undefined && item.classList.contains('user-name')) {
result = result + item.lastChild.firstChild.textContent;
} else if (item.innerText === undefined) {
result = result + item.textContent;
} else result = result + item.innerText;
});
return result;
}
}
}
}
]
}
],
// For responsive popup
responsive: {
details: {
display: $.fn.dataTable.Responsive.display.modal({
header: function (row) {
var data = row.data();
return 'Details of ' + data['customer'];
}
}),
type: 'column',
renderer: function (api, rowIdx, columns) {
var data = $.map(columns, function (col, i) {
return col.title !== '' // ? Do not show row in modal popup if title is blank (for check box)
? '' +
'| ' +
col.title +
':' +
' | ' +
'' +
col.data +
' | ' +
'
'
: '';
}).join('');
return data ? $('').append(data) : false;
}
}
}
});
$('.dt-action-buttons').addClass('pt-0');
}
// Delete Record
$('.datatables-order tbody').on('click', '.delete-record', function () {
dt_products.row($(this).parents('tr')).remove().draw();
});
// Filter form control to default size
// ? setTimeout used for multilingual table initialization
setTimeout(() => {
$('.dataTables_filter .form-control').addClass('ms-0');
}, 300);
});