/** * DataTables Basic */ 'use strict'; let fv, offCanvasEl; document.addEventListener('DOMContentLoaded', function (e) { (function () { const formAddNewRecord = document.getElementById('form-add-new-record'); setTimeout(() => { const newRecord = document.querySelector('.create-new'), offCanvasElement = document.querySelector('#add-new-record'); // To open offCanvas, to add new record if (newRecord) { newRecord.addEventListener('click', function () { offCanvasEl = new bootstrap.Offcanvas(offCanvasElement); // Empty fields on offCanvas open (offCanvasElement.querySelector('.dt-full-name').value = ''), (offCanvasElement.querySelector('.dt-post').value = ''), (offCanvasElement.querySelector('.dt-email').value = ''), (offCanvasElement.querySelector('.dt-date').value = ''), (offCanvasElement.querySelector('.dt-salary').value = ''); // Open offCanvas with form offCanvasEl.show(); }); } }, 200); // Form validation for Add new record fv = FormValidation.formValidation(formAddNewRecord, { fields: { basicFullname: { validators: { notEmpty: { message: 'The name is required' } } }, basicPost: { validators: { notEmpty: { message: 'Post field is required' } } }, basicEmail: { validators: { notEmpty: { message: 'The Email is required' }, emailAddress: { message: 'The value is not a valid email address' } } }, basicDate: { validators: { notEmpty: { message: 'Joining Date is required' }, date: { format: 'MM/DD/YYYY', message: 'The value is not a valid date' } } }, basicSalary: { validators: { notEmpty: { message: 'Basic Salary is required' } } } }, plugins: { trigger: new FormValidation.plugins.Trigger(), bootstrap5: new FormValidation.plugins.Bootstrap5({ // Use this for enabling/changing valid/invalid class // eleInvalidClass: '', eleValidClass: '', rowSelector: '.col-sm-12' }), submitButton: new FormValidation.plugins.SubmitButton(), // defaultSubmit: new FormValidation.plugins.DefaultSubmit(), autoFocus: new FormValidation.plugins.AutoFocus() }, init: instance => { instance.on('plugins.message.placed', function (e) { if (e.element.parentElement.classList.contains('input-group')) { e.element.parentElement.insertAdjacentElement('afterend', e.messageElement); } }); } }); // FlatPickr Initialization & Validation flatpickr(formAddNewRecord.querySelector('[name="basicDate"]'), { enableTime: false, // See https://flatpickr.js.org/formatting/ dateFormat: 'm/d/Y', // After selecting a date, we need to revalidate the field onChange: function () { fv.revalidateField('basicDate'); } }); })(); }); // datatable (jquery) $(function () { var dt_basic_table = $('.datatables-basic'), dt_complex_header_table = $('.dt-complex-header'), dt_row_grouping_table = $('.dt-row-grouping'), dt_multilingual_table = $('.dt-multilingual'), dt_basic; // DataTable with buttons // -------------------------------------------------------------------- if (dt_basic_table.length) { dt_basic = dt_basic_table.DataTable({ ajax: assetsPath + 'json/table-datatable.json', columns: [ { data: '' }, { data: 'id' }, { data: 'id' }, { data: 'full_name' }, { data: 'email' }, { data: 'start_date' }, { data: 'salary' }, { data: 'status' }, { data: '' } ], columnDefs: [ { // For Responsive className: 'control', orderable: false, searchable: 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: '' } }, { targets: 2, searchable: false, visible: false }, { // Avatar image/badge, Name and post targets: 3, responsivePriority: 4, render: function (data, type, full, meta) { var $user_img = full['avatar'], $name = full['full_name'], $post = full['post']; if ($user_img) { // For Avatar image var $output = 'Avatar'; } else { // For Avatar badge var stateNum = Math.floor(Math.random() * 6); var states = ['success', 'danger', 'warning', 'info', 'primary', 'secondary']; var $state = states[stateNum], $name = full['full_name'], $initials = $name.match(/\b\w/g) || []; $initials = (($initials.shift() || '') + ($initials.pop() || '')).toUpperCase(); $output = '' + $initials + ''; } // Creates full output for row var $row_output = '
' + '
' + '
' + $output + '
' + '
' + '
' + '' + $name + '' + '' + $post + '' + '
' + '
'; return $row_output; } }, { responsivePriority: 1, targets: 4 }, { // Label targets: -2, render: function (data, type, full, meta) { var $status_number = full['status']; var $status = { 1: { title: 'Current', class: 'bg-label-primary' }, 2: { title: 'Professional', class: ' bg-label-success' }, 3: { title: 'Rejected', class: ' bg-label-danger' }, 4: { title: 'Resigned', class: ' bg-label-warning' }, 5: { title: 'Applied', class: ' bg-label-info' } }; if (typeof $status[$status_number] === 'undefined') { return data; } return ( '' + $status[$status_number].title + '' ); } }, { // Actions targets: -1, title: 'Actions', orderable: false, searchable: false, render: function (data, type, full, meta) { return ( '
' + '' + '' + '
' + '' ); } } ], order: [[2, 'desc']], dom: '<"card-header flex-column flex-md-row"<"head-label text-center"><"dt-action-buttons text-end pt-3 pt-md-0"B>><"row"<"col-sm-12 col-md-6"l><"col-sm-12 col-md-6 d-flex justify-content-center justify-content-md-end"f>>t<"row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>', displayLength: 7, lengthMenu: [7, 10, 25, 50, 75, 100], buttons: [ { extend: 'collection', className: 'btn btn-label-primary dropdown-toggle me-2', text: ' Export', buttons: [ { extend: 'print', text: 'Print', className: 'dropdown-item', exportOptions: { columns: [3, 4, 5, 6, 7], // 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; } } }, customize: function (win) { //customize print view for dark $(win.document.body) .css('color', config.colors.headingColor) .css('border-color', config.colors.borderColor) .css('background-color', config.colors.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: [3, 4, 5, 6, 7], // 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: [3, 4, 5, 6, 7], // 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: [3, 4, 5, 6, 7], // 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: [3, 4, 5, 6, 7], // 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; } } } } ] }, { text: ' Add New Record', className: 'create-new btn btn-primary' } ], responsive: { details: { display: $.fn.dataTable.Responsive.display.modal({ header: function (row) { var data = row.data(); return 'Details of ' + data['full_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; } } } }); $('div.head-label').html('
DataTable with Buttons
'); } // Add New record // ? Remove/Update this code as per your requirements var count = 101; // On form submit, if form is valid fv.on('core.form.valid', function () { var $new_name = $('.add-new-record .dt-full-name').val(), $new_post = $('.add-new-record .dt-post').val(), $new_email = $('.add-new-record .dt-email').val(), $new_date = $('.add-new-record .dt-date').val(), $new_salary = $('.add-new-record .dt-salary').val(); if ($new_name != '') { dt_basic.row .add({ id: count, full_name: $new_name, post: $new_post, email: $new_email, start_date: $new_date, salary: '$' + $new_salary, status: 5 }) .draw(); count++; // Hide offcanvas using javascript method offCanvasEl.hide(); } }); // Delete Record $('.datatables-basic tbody').on('click', '.delete-record', function () { dt_basic.row($(this).parents('tr')).remove().draw(); }); // Complex Header DataTable // -------------------------------------------------------------------- if (dt_complex_header_table.length) { var dt_complex = dt_complex_header_table.DataTable({ ajax: assetsPath + 'json/table-datatable.json', columns: [ { data: 'full_name' }, { data: 'email' }, { data: 'city' }, { data: 'post' }, { data: 'salary' }, { data: 'status' }, { data: '' } ], columnDefs: [ { // Label targets: -2, render: function (data, type, full, meta) { var $status_number = full['status']; var $status = { 1: { title: 'Current', class: 'bg-label-primary' }, 2: { title: 'Professional', class: ' bg-label-success' }, 3: { title: 'Rejected', class: ' bg-label-danger' }, 4: { title: 'Resigned', class: ' bg-label-warning' }, 5: { title: 'Applied', class: ' bg-label-info' } }; if (typeof $status[$status_number] === 'undefined') { return data; } return ( '' + $status[$status_number].title + '' ); } }, { // Actions targets: -1, title: 'Actions', orderable: false, render: function (data, type, full, meta) { return ( '
' + '' + '' + '
' + '' ); } } ], dom: '<"row"<"col-sm-12 col-md-6"l><"col-sm-12 col-md-6 d-flex justify-content-center justify-content-md-end"f>><"table-responsive"t><"row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>', displayLength: 7, lengthMenu: [7, 10, 25, 50, 75, 100] }); } // Row Grouping // -------------------------------------------------------------------- var groupColumn = 2; if (dt_row_grouping_table.length) { var groupingTable = dt_row_grouping_table.DataTable({ ajax: assetsPath + 'json/table-datatable.json', columns: [ { data: '' }, { data: 'full_name' }, { data: 'post' }, { data: 'email' }, { data: 'city' }, { data: 'start_date' }, { data: 'salary' }, { data: 'status' }, { data: '' } ], columnDefs: [ { // For Responsive className: 'control', orderable: false, targets: 0, searchable: false, render: function (data, type, full, meta) { return ''; } }, { visible: false, targets: groupColumn }, { // Label targets: -2, render: function (data, type, full, meta) { var $status_number = full['status']; var $status = { 1: { title: 'Current', class: 'bg-label-primary' }, 2: { title: 'Professional', class: ' bg-label-success' }, 3: { title: 'Rejected', class: ' bg-label-danger' }, 4: { title: 'Resigned', class: ' bg-label-warning' }, 5: { title: 'Applied', class: ' bg-label-info' } }; if (typeof $status[$status_number] === 'undefined') { return data; } return ( '' + $status[$status_number].title + '' ); } }, { // Actions targets: -1, title: 'Actions', orderable: false, searchable: false, render: function (data, type, full, meta) { return ( '
' + '' + '' + '
' + '' ); } } ], order: [[groupColumn, 'asc']], dom: '<"row"<"col-sm-12 col-md-6"l><"col-sm-12 col-md-6 d-flex justify-content-center justify-content-md-end"f>>t<"row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>', displayLength: 7, lengthMenu: [7, 10, 25, 50, 75, 100], drawCallback: function (settings) { var api = this.api(); var rows = api.rows({ page: 'current' }).nodes(); var last = null; api .column(groupColumn, { page: 'current' }) .data() .each(function (group, i) { if (last !== group) { $(rows) .eq(i) .before(''); last = group; } }); }, responsive: { details: { display: $.fn.dataTable.Responsive.display.modal({ header: function (row) { var data = row.data(); return 'Details of ' + data['full_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) ? '' + ' ' + '' + '' : ''; }).join(''); return data ? $('
' + group + '
' + col.title + ':' + '' + col.data + '
').append(data) : false; } } } }); // Order by the grouping $('.dt-row-grouping tbody').on('click', 'tr.group', function () { var currentOrder = groupingTable.order()[0]; if (currentOrder[0] === groupColumn && currentOrder[1] === 'asc') { groupingTable.order([groupColumn, 'desc']).draw(); } else { groupingTable.order([groupColumn, 'asc']).draw(); } }); } // Multilingual DataTable // -------------------------------------------------------------------- var lang = 'German'; if (dt_multilingual_table.length) { var table_language = dt_multilingual_table.DataTable({ ajax: assetsPath + 'json/table-datatable.json', columns: [ { data: '' }, { data: 'full_name' }, { data: 'post' }, { data: 'email' }, { data: 'start_date' }, { data: 'salary' }, { data: 'status' }, { data: '' } ], columnDefs: [ { // For Responsive className: 'control', orderable: false, targets: 0, searchable: false, render: function (data, type, full, meta) { return ''; } }, { // Label targets: -2, render: function (data, type, full, meta) { var $status_number = full['status']; var $status = { 1: { title: 'Current', class: 'bg-label-primary' }, 2: { title: 'Professional', class: ' bg-label-success' }, 3: { title: 'Rejected', class: ' bg-label-danger' }, 4: { title: 'Resigned', class: ' bg-label-warning' }, 5: { title: 'Applied', class: ' bg-label-info' } }; if (typeof $status[$status_number] === 'undefined') { return data; } return ( '' + $status[$status_number].title + '' ); } }, { // Actions targets: -1, title: 'Actions', orderable: false, searchable: false, render: function (data, type, full, meta) { return ( '
' + '' + '' + '
' + '' ); } } ], language: { url: '//cdn.datatables.net/plug-ins/9dcbecd42ad/i18n/' + lang + '.json' }, displayLength: 7, dom: '<"row"<"col-sm-12 col-md-6"l><"col-sm-12 col-md-6 d-flex justify-content-center justify-content-md-end"f>>t<"row"<"col-sm-12 col-md-6"i><"col-sm-12 col-md-6"p>>', lengthMenu: [7, 10, 25, 50, 75, 100], responsive: { details: { display: $.fn.dataTable.Responsive.display.modal({ header: function (row) { var data = row.data(); return 'Details of ' + data['full_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) ? '' + ' ' + '' + '' : ''; }).join(''); return data ? $('
' + col.title + ':' + '' + col.data + '
').append(data) : false; } } } }); } // 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); });