Files
Global-Jain/public/js/datatable/datatable-skeleton.js
2025-11-05 10:37:10 +05:30

248 lines
10 KiB
JavaScript
Vendored

"use strict";
// Class Definition
let customDataTableWidget = {
element: null,
listingUrl: null,
deleteUrl: null,
searching: false,
serverSide: true,
processing: true,
columns: [],
dataTable: null,
search: [],
checkedRecords: [],
searchStatus: false,
defaultSortingIndex: 0,
defaultSortingOrder: 'desc',
length: 10,
skeleton: function () {
customDataTableWidget.dataTable = $(customDataTableWidget.element).DataTable({
// "dom": '<"top"i>rt<"bottom"flp>',
sDom: 'Rfrtlip',
scrollX: !0,
processing: customDataTableWidget.processing,
serverSide: customDataTableWidget.serverSide,
searching: customDataTableWidget.searching,
fixedHeader: true,
pageLength: customDataTableWidget.length,
ajax: {
url: customDataTableWidget.listingUrl,
data: function (d) {
if (customDataTableWidget.searchStatus) {
$(customDataTableWidget.search).each(function (index, value) {
if (value.type == 'select') {
d[value.key] = $("select[name='" + value.key + "']").val();
} else {
d[value.key] = $("input[name='" + value.key + "']").val();
}
});
}
return d;
}
},
order: [[customDataTableWidget.defaultSortingIndex, customDataTableWidget.defaultSortingOrder]],
columns: customDataTableWidget.columns
});
},
checkboxActionEvents: function () {
//checked all
$(document).on('click', '#checkbox-all', function () {
if (this.checked) {
$(".check-record").each(function () {
this.checked = true;
customDataTableWidget.checkedRecords.push($(this).val());
$("#recordRow-" + $(this).val()).attr('checked', true);
$("#recordRow-" + $(this).val()).css('background', '#E4E6EF');
});
} else {
$(".check-record").each(function () {
customDataTableWidget.checkedRecords.splice(0, customDataTableWidget.checkedRecords.length)
$("#recordRow-" + $(this).val()).attr('checked', false);
$("#recordRow-" + $(this).val()).css('background', '#FFFFFF');
this.checked = false;
});
}
customDataTableWidget.hideAndShowActionDropdown(customDataTableWidget.checkedRecords);
});
//checked single record based on click
$(document).on('click', '.check-record', function () {
let recordId = $(this).val();
if ($(this).is(":checked")) {
customDataTableWidget.checkedRecords.push(recordId);
$("#record-" + recordId).attr('checked', true);
$("#recordRow-" + recordId).css('background', '#E4E6EF');
} else if ($(this).is(":not(:checked)")) {
customDataTableWidget.checkedRecords = without(customDataTableWidget.checkedRecords, recordId);
$("#record-" + recordId).attr('checked', false);
$("#recordRow-" + recordId).css('background', '#FFFFFF');
}
customDataTableWidget.hideAndShowActionDropdown(customDataTableWidget.checkedRecords);
});
//checked record as it's whenever update the pagination of grid
customDataTableWidget.dataTable.on('draw.dt', function () {
$.each(customDataTableWidget.checkedRecords, function (index, value) {
$(document).find("#record-" + value).attr('checked', true);
$("#recordRow-" + value).css('background', '#E4E6EF');
});
});
$(document).on('click', '.action-type', function () {
let actionType = $(this).data('value');
Swal.fire({
title: "Are you sure?",
text: actionType == 'delete' ? "You won't be able to revert this!" : "",
icon: "warning",
showCancelButton: true,
confirmButtonText: "Yes, " + actionType + " it!",
cancelButtonText: "No, cancel!",
reverseButtons: true
}).then(function (result) {
if (result.value) {
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: "POST",
url: customDataTableWidget.multipleActionUrl,
data: {'checked_records': customDataTableWidget.checkedRecords, 'action_type': actionType},
success: function (res) {
if (res.success) {
toastr.success(res.message);
} else {
toastr.error(res.message);
}
customDataTableWidget.reset();
customDataTableWidget.dataTable.draw();
},
error: function (error) {
toastr.error(error);
}
});
}
});
});
$(document).on('click', '.excel-export', function () {
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: "POST",
url: customDataTableWidget.excelExportUrl,
data: {'checked_records': customDataTableWidget.checkedRecords},
success: function (res) {
if (res.status) {
var a = document.createElement("a");
a.href = res.file;
document.body.appendChild(a);
a.click();
a.remove();
} else {
toastr.error(res.message);
}
customDataTableWidget.reset();
customDataTableWidget.dataTable.draw();
},
error: function (error) {
toastr.error(error);
}
});
});
},
commonEvents: function () {
$("#kt_reset").on("click", function (e) {
e.preventDefault(),
$(".datatable-input").each(function () {
$(this).val("").selectpicker('refresh');
})
customDataTableWidget.searchStatus = false;
customDataTableWidget.dataTable.draw();
});
// change events
$(document).on('change', ".change-action", function () {
customDataTableWidget.searchStatus = true;
customDataTableWidget.dataTable.draw();
});
// keyup events
$(document).on('keyup', ".change-action", function () {
customDataTableWidget.searchStatus = true;
customDataTableWidget.dataTable.draw();
});
// click events for date range searching
$(document).on('click', ".applyBtn", function () {
customDataTableWidget.searchStatus = true;
customDataTableWidget.dataTable.draw();
});
//delete action
$(document).on('click', '.delete-confirmation', function (e) {
let userId = $(this).data('id');
Swal.fire({
title: "Are you sure?",
text: "You won't be able to revert this!",
icon: "warning",
showCancelButton: true,
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel!",
reverseButtons: true
}).then(function (result) {
if (result.value) {
$.ajax({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
type: "DELETE",
url: customDataTableWidget.deleteUrl.replace(':id', userId),
success: function (res) {
if (res.success) {
toastr.success(res.message);
} else {
toastr.error(res.message);
}
customDataTableWidget.dataTable.draw();
},
error: function (error) {
toastr.error(error);
}
});
}
});
});
},
reset: function () {
$(document).find('#checkboxActionDropdown').removeClass("active");
$(document).find('#checkbox-all').prop('checked', false);
customDataTableWidget.checkedRecords = [];
},
hideAndShowActionDropdown: function (checkboxArray) {
if (checkboxArray.length > 0) {
$(document).find('#checkboxActionDropdown').addClass("active");
$(document).find('#datatableSelectedRecords').html(checkboxArray.length);
} else {
$(document).find('#checkboxActionDropdown').removeClass("active");
}
},
init: function () {
this.skeleton();
this.checkboxActionEvents();
this.commonEvents();
},
configuration: function (data) {
if (data) {
this.element = data.element || null;
this.listingUrl = data.listingUrl || null;
this.deleteUrl = data.deleteUrl || null;
this.checkboxAction = data.checkboxAction || false;
this.multipleActionUrl = data.multipleActionUrl || null;
this.excelExportUrl = data.excelExportUrl || null;
this.columns = data.columns || [];
this.searching = data.searching || false;
this.serverSide = data.serverSide || true;
this.processing = data.processing || true;
this.search = data.search || [];
this.defaultSortingIndex = data.defaultSortingIndex || 0;
this.defaultSortingOrder = data.defaultSortingOrder || 'DESC';
this.length = data.length || 10;
}
customDataTableWidget.init();
}
}