/** * app-ecommerce-product-list */ '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_product_table = $('.datatables-products'), productAdd = 'app-ecommerce-product-add.html', statusObj = { 1: { title: 'Scheduled', class: 'bg-label-warning' }, 2: { title: 'Publish', class: 'bg-label-success' }, 3: { title: 'Inactive', class: 'bg-label-danger' } }, categoryObj = { 0: { title: 'Household' }, 1: { title: 'Office' }, 2: { title: 'Electronics' }, 3: { title: 'Shoes' }, 4: { title: 'Accessories' }, 5: { title: 'Game' } }, stockObj = { 0: { title: 'Out_of_Stock' }, 1: { title: 'In_Stock' } }, stockFilterValObj = { 0: { title: 'Out of Stock' }, 1: { title: 'In Stock' } }; // E-commerce Products datatable if (dt_product_table.length) { var dt_products = dt_product_table.DataTable({ ajax: assetsPath + 'json/ecommerce-product-list.json', // JSON file to add data columns: [ // columns according to JSON { data: 'id' }, { data: 'id' }, { data: 'product_name' }, { data: 'category' }, { data: 'stock' }, { data: 'sku' }, { data: 'price' }, { data: 'quantity' }, { data: 'status' }, { 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 }, { // Product name and product_brand targets: 2, responsivePriority: 1, render: function (data, type, full, meta) { var $name = full['product_name'], $id = full['id'], $product_brand = full['product_brand'], $image = full['image']; if ($image) { // For Product image var $output = 'Product-' +
                $id +
                ''; } else { // For Product badge var stateNum = Math.floor(Math.random() * 6); var states = ['success', 'danger', 'warning', 'info', 'dark', 'primary', 'secondary']; var $state = states[stateNum], $name = full['product_brand'], $initials = $name.match(/\b\w/g) || []; $initials = (($initials.shift() || '') + ($initials.pop() || '')).toUpperCase(); $output = '' + $initials + ''; } // Creates full output for Product name and product_brand var $row_output = '
' + '
' + '
' + $output + '
' + '
' + '
' + '
' + $name + '
' + '' + $product_brand + '' + '
' + '
'; return $row_output; } }, { // Product Category targets: 3, responsivePriority: 5, render: function (data, type, full, meta) { var $category = categoryObj[full['category']].title; var categoryBadgeObj = { Household: '', Office: '', Electronics: '', Shoes: '', Accessories: '', Game: '' }; return ( "" + categoryBadgeObj[$category] + $category + '' ); } }, { // Stock targets: 4, orderable: false, responsivePriority: 3, render: function (data, type, full, meta) { var $stock = full['stock']; var stockSwitchObj = { Out_of_Stock: '', In_Stock: '' }; return ( "" + stockSwitchObj[stockObj[$stock].title] + '' + stockObj[$stock].title + '' + '' ); } }, { // Sku targets: 5, render: function (data, type, full, meta) { var $sku = full['sku']; return '' + $sku + ''; } }, { // price targets: 6, render: function (data, type, full, meta) { var $price = full['price']; return '' + $price + ''; } }, { // qty targets: 7, responsivePriority: 4, render: function (data, type, full, meta) { var $qty = full['qty']; return '' + $qty + ''; } }, { // Status targets: -2, render: function (data, type, full, meta) { var $status = full['status']; return ( '' + statusObj[$status].title + '' ); } }, { // Actions targets: -1, title: 'Actions', searchable: false, orderable: false, render: function (data, type, full, meta) { return ( '
' + '' + '' + '' + '
' ); } } ], order: [2, 'asc'], //set any columns order asc/desc dom: '<"card-header d-flex border-top rounded-0 flex-wrap py-0 flex-column flex-md-row align-items-start"' + '<"me-5 ms-n4 pe-5 mb-n6 mb-md-0"f>' + '<"d-flex justify-content-start justify-content-md-end align-items-baseline"<"dt-action-buttons d-flex flex-column align-items-start align-items-sm-center justify-content-sm-center pt-0 gap-sm-4 gap-sm-0 flex-sm-row"lB>>' + '>t' + '<"row"' + '<"col-sm-12 col-md-6"i>' + '<"col-sm-12 col-md-6"p>' + '>', lengthMenu: [7, 10, 20, 50, 70, 100], //for length of menu language: { sLengthMenu: '_MENU_', search: '', searchPlaceholder: 'Search Product', info: 'Displaying _START_ to _END_ of _TOTAL_ entries', paginate: { next: '', previous: '' } }, // Buttons with Dropdown buttons: [ { extend: 'collection', className: 'btn btn-label-secondary dropdown-toggle me-4', text: 'Export', buttons: [ { extend: 'print', text: 'Print', className: 'dropdown-item', exportOptions: { columns: [1, 2, 3, 4, 5, 6, 7], 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('product-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, 6, 7], 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('product-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, 6, 7], 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('product-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, 6, 7], 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('product-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, 6, 7], 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('product-name')) { result = result + item.lastChild.firstChild.textContent; } else if (item.innerText === undefined) { result = result + item.textContent; } else result = result + item.innerText; }); return result; } } } } ] }, { text: 'Add Product', className: 'add-new btn btn-primary', action: function () { window.location.href = productAdd; } } ], // For responsive popup responsive: { details: { display: $.fn.dataTable.Responsive.display.modal({ header: function (row) { var data = row.data(); return 'Details of ' + data['product_name']; } }), 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; } } }, initComplete: function () { // Adding status filter once table initialized this.api() .columns(-2) .every(function () { var column = this; var select = $( '' ) .appendTo('.product_status') .on('change', function () { var val = $.fn.dataTable.util.escapeRegex($(this).val()); column.search(val ? '^' + val + '$' : '', true, false).draw(); }); column .data() .unique() .sort() .each(function (d, j) { select.append(''); }); }); // Adding category filter once table initialized this.api() .columns(3) .every(function () { var column = this; var select = $( '' ) .appendTo('.product_category') .on('change', function () { var val = $.fn.dataTable.util.escapeRegex($(this).val()); column.search(val ? '^' + val + '$' : '', true, false).draw(); }); column .data() .unique() .sort() .each(function (d, j) { select.append(''); }); }); // Adding stock filter once table initialized this.api() .columns(4) .every(function () { var column = this; var select = $( '' ) .appendTo('.product_stock') .on('change', function () { var val = $.fn.dataTable.util.escapeRegex($(this).val()); column.search(val ? '^' + val + '$' : '', true, false).draw(); }); column .data() .unique() .sort() .each(function (d, j) { select.append(''); }); }); } }); $('.dataTables_length').addClass('mx-n2'); $('.dt-buttons').addClass('d-flex flex-wrap mb-6 mb-sm-0'); } // Delete Record $('.datatables-products 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').removeClass('form-control-sm'); $('.dataTables_length .form-select').removeClass('form-select-sm'); }, 300); });