'
);
}
}
],
order: [[2, 'desc']],
dom:
'<"card-header d-flex rounded-0 flex-wrap pb-md-0 pt-0"' +
'<"me-5 ms-n2"f>' +
'<"d-flex justify-content-start justify-content-md-end align-items-baseline"<"dt-action-buttons d-flex align-items-start align-items-md-center justify-content-sm-center gap-4"lB>>' +
'>t' +
'<"row mx-1"' +
'<"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',
info: 'Displaying _START_ to _END_ of _TOTAL_ entries',
paginate: {
next: '',
previous: ''
}
},
// Buttons with Dropdown
buttons: [
{
extend: 'collection',
className: 'btn btn-outline-secondary dropdown-toggle me-4 waves-effect waves-light',
text: 'Export ',
buttons: [
{
extend: 'print',
title: 'Users',
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', config.colors.headingColor)
.css('border-color', config.colors.borderColor)
.css('background-color', config.colors.body);
$(win.document.body)
.find('table')
.addClass('compact')
.css('color', 'inherit')
.css('border-color', 'inherit')
.css('background-color', 'inherit');
}
},
{
extend: 'csv',
title: 'Users',
text: 'Csv',
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;
}
}
}
},
{
extend: 'excel',
title: 'Users',
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',
title: 'Users',
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',
title: 'Users',
text: 'Copy',
className: 'dropdown-item',
exportOptions: {
columns: [1, 2, 3, 4, 5],
// prevent avatar to be copy
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 User',
className: 'add-new btn btn-primary waves-effect waves-light',
attr: {
'data-bs-toggle': 'offcanvas',
'data-bs-target': '#offcanvasAddUser'
}
}
],
// For responsive popup
responsive: {
details: {
display: $.fn.dataTable.Responsive.display.modal({
header: function (row) {
var data = row.data();
return 'Details of ' + data['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;
}
}
}
});
}
// Delete Record
$(document).on('click', '.delete-record', function () {
var user_id = $(this).data('id'),
dtrModal = $('.dtr-bs-modal.show');
// hide responsive modal in small screen
if (dtrModal.length) {
dtrModal.modal('hide');
}
// sweetalert for confirmation of delete
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
customClass: {
confirmButton: 'btn btn-primary me-3',
cancelButton: 'btn btn-label-secondary'
},
buttonsStyling: false
}).then(function (result) {
if (result.value) {
// delete the data
$.ajax({
type: 'DELETE',
url: `${baseUrl}user-list/${user_id}`,
success: function () {
dt_user.draw();
},
error: function (error) {
console.log(error);
}
});
// success sweetalert
Swal.fire({
icon: 'success',
title: 'Deleted!',
text: 'The user has been deleted!',
customClass: {
confirmButton: 'btn btn-success'
}
});
} else if (result.dismiss === Swal.DismissReason.cancel) {
Swal.fire({
title: 'Cancelled',
text: 'The User is not deleted!',
icon: 'error',
customClass: {
confirmButton: 'btn btn-success'
}
});
}
});
});
// edit record
$(document).on('click', '.edit-record', function () {
var user_id = $(this).data('id'),
dtrModal = $('.dtr-bs-modal.show');
// hide responsive modal in small screen
if (dtrModal.length) {
dtrModal.modal('hide');
}
// changing the title of offcanvas
$('#offcanvasAddUserLabel').html('Edit User');
// get data
$.get(`${baseUrl}user-list\/${user_id}\/edit`, function (data) {
$('#user_id').val(data.id);
$('#add-user-fullname').val(data.name);
$('#add-user-email').val(data.email);
});
});
// changing the title
$('.add-new').on('click', function () {
$('#user_id').val(''); //reseting input field
$('#offcanvasAddUserLabel').html('Add User');
});
// validating form and updating user's data
const addNewUserForm = document.getElementById('addNewUserForm');
// user form validation
const fv = FormValidation.formValidation(addNewUserForm, {
fields: {
name: {
validators: {
notEmpty: {
message: 'Please enter fullname'
}
}
},
email: {
validators: {
notEmpty: {
message: 'Please enter your email'
},
emailAddress: {
message: 'The value is not a valid email address'
}
}
},
userContact: {
validators: {
notEmpty: {
message: 'Please enter your contact'
}
}
},
company: {
validators: {
notEmpty: {
message: 'Please enter your company'
}
}
}
},
plugins: {
trigger: new FormValidation.plugins.Trigger(),
bootstrap5: new FormValidation.plugins.Bootstrap5({
// Use this for enabling/changing valid/invalid class
eleValidClass: '',
rowSelector: function (field, ele) {
// field is the field name & ele is the field element
return '.mb-5';
}
}),
submitButton: new FormValidation.plugins.SubmitButton(),
// Submit the form when all fields are valid
// defaultSubmit: new FormValidation.plugins.DefaultSubmit(),
autoFocus: new FormValidation.plugins.AutoFocus()
}
}).on('core.form.valid', function () {
// adding or updating user when form successfully validate
$.ajax({
data: $('#addNewUserForm').serialize(),
url: `${baseUrl}user-list`,
type: 'POST',
success: function (status) {
dt_user.draw();
offCanvasForm.offcanvas('hide');
// sweetalert
Swal.fire({
icon: 'success',
title: `Successfully ${status}!`,
text: `User ${status} Successfully.`,
customClass: {
confirmButton: 'btn btn-success'
}
});
},
error: function (err) {
offCanvasForm.offcanvas('hide');
Swal.fire({
title: 'Duplicate Entry!',
text: 'Your email should be unique.',
icon: 'error',
customClass: {
confirmButton: 'btn btn-success'
}
});
}
});
});
// clearing form data when offcanvas hidden
offCanvasForm.on('hidden.bs.offcanvas', function () {
fv.resetForm(true);
});
const phoneMaskList = document.querySelectorAll('.phone-mask');
// Phone Number
if (phoneMaskList) {
phoneMaskList.forEach(function (phoneMask) {
new Cleave(phoneMask, {
phone: true,
phoneRegionCode: 'US'
});
});
}
});