231 lines
8.9 KiB
JavaScript
231 lines
8.9 KiB
JavaScript
function updateDepartmentId() {
|
|
var chargeToElement = document.getElementById('chargeTo');
|
|
if (!chargeToElement) {
|
|
console.error('chargeTo element not found');
|
|
return;
|
|
}
|
|
var selectedOption = chargeToElement.options[chargeToElement.selectedIndex];
|
|
if (!selectedOption) {
|
|
console.error('No option selected');
|
|
return;
|
|
}
|
|
document.getElementById('departmentId').value = selectedOption.value;
|
|
document.getElementById('chargeTo').addEventListener('change', updateDepartmentId);
|
|
}
|
|
function renderItembtns(data, row) {
|
|
var jsonData = JSON.stringify(row).replace(/"/g, """);
|
|
var buttonsHtml = '';
|
|
// var statusNumber = parseInt(row.status, 10);
|
|
buttonsHtml += '<button onclick="deleteCartItemById(' + jsonData + ')" class="btn btn-default">' +
|
|
'<i class="fa-solid fa-trash-can fa-xl" style="color: #Ff0000;" aria-hidden="true"></i>' +
|
|
'</button > ';
|
|
|
|
return buttonsHtml;
|
|
}
|
|
function showHideLabelButtons() {
|
|
if (RequestTypeId == 1) {
|
|
let labelnormalPR = document.getElementById('normalPR');
|
|
let specialPR = document.getElementById('specialPR');
|
|
let chargeTo = document.getElementById('chargeTo');
|
|
let labelChargeTo = document.getElementById('labelChargeTo');
|
|
|
|
labelnormalPR.style.display = 'none';
|
|
specialPR.style.display = 'none';
|
|
chargeTo.style.display = 'none';
|
|
labelChargeTo.style.display = 'none';
|
|
} else if (RequestTypeId == 2){
|
|
let specialPR = document.getElementById('specialPR');
|
|
let internalRequest = document.getElementById('internalRequest');
|
|
let chargeTo = document.getElementById('chargeTo');
|
|
let labelChargeTo = document.getElementById('labelChargeTo');
|
|
|
|
internalRequest.style.display = 'none';
|
|
specialPR.style.display = 'none';
|
|
chargeTo.style.display = 'none';
|
|
labelChargeTo.style.display = 'none';
|
|
} else {
|
|
let labelnormalPR = document.getElementById('normalPR');
|
|
let internalRequest = document.getElementById('internalRequest');
|
|
|
|
labelnormalPR.style.display = 'none';
|
|
internalRequest.style.display = 'none';
|
|
}
|
|
}
|
|
$(document).ready(function () {
|
|
loader = $('#overlay, #loader');
|
|
var submitButton = $('#btnSubmitItem');
|
|
var totalSelectedLabel = $('#totalSelected');
|
|
|
|
UserRights = document.getElementById("roleRights").value;
|
|
RequestTypeId = document.getElementById("requestTypeId").value;
|
|
showHideLabelButtons();
|
|
itemTable = $('#ItemCartTable').DataTable({
|
|
ajax: {
|
|
url: '/ItemMgmt/GetItemCart',
|
|
type: 'GET',
|
|
data: { RequestTypeId },
|
|
beforeSend: function () {
|
|
// Show the loader before making the AJAX request
|
|
loader.show();
|
|
},
|
|
complete: function () {
|
|
loader.hide();
|
|
}
|
|
},
|
|
initComplete: function () {
|
|
var api = this.api();
|
|
var data = api.ajax.json();
|
|
|
|
if (!data || !data.data || data.data === "No Data") {
|
|
// Display the "No record available" message
|
|
$('.dataTables_empty').html("No record available");
|
|
}
|
|
updateSubmitButtonVisibility();
|
|
},
|
|
columns: [
|
|
{
|
|
data: 'itemCartId',
|
|
render: function () {
|
|
return '<input type="checkbox" class="selectedItem-checkbox" />';
|
|
}
|
|
},
|
|
{ data: 'itemNo' },
|
|
{ data: 'itemName' },
|
|
{ data: 'itemCategoryName' },
|
|
{ data: 'prTypeId' },
|
|
{ data: 'qty' },
|
|
{
|
|
data: null,
|
|
render: function (data, type, row) {
|
|
return renderItembtns(data, row);
|
|
}
|
|
},
|
|
{ data: 'isActive', visible: false },
|
|
{ data: 'itemCartId', visible: false },
|
|
{ data: 'cartItemCount', visible: false },
|
|
{ data: 'createdDate', visible: false },
|
|
{ data: 'requestTypeId', visible: false },
|
|
],
|
|
|
|
"columnDefs": [
|
|
{
|
|
"targets": [5], // Index of the 'qty' column (0-based)
|
|
"render": function (data, type, row) {
|
|
// Render the 'qty' column as an input field
|
|
return '<input type="number" class="editable-qty" style="width:60px;" value="' + data + '" />';
|
|
}
|
|
}
|
|
],
|
|
rowCallback: function (row, data) {
|
|
var cartItemCount = data.cartItemCount; // Retrieve the cart item count from the data object
|
|
var itemCount = parseInt(cartItemCount, 10); // Parse the text content as an integer
|
|
|
|
var prTypeIdCell = $('td:eq(4)', row);
|
|
var PRTypeId = prTypeIdCell.text();
|
|
var PRTypeIdNumer = parseInt(PRTypeId, 10);
|
|
// console.log('ItemCartIds', ItemCartId);
|
|
// Check if the parsed integer is a valid number
|
|
if (!isNaN(itemCount)) {
|
|
$('#cartCount').text(itemCount); // Update the cart count in the navbar
|
|
} else {
|
|
$('#cartCount').text('0'); // If the parsed integer is NaN, set the cart count to 0
|
|
}
|
|
if (!isNaN(PRTypeIdNumer)) {
|
|
if (PRTypeIdNumer === 1) {
|
|
prTypeIdCell.text('Goods').addClass('next-facilitator');
|
|
} else {
|
|
prTypeIdCell.text('Service').addClass('next-facilitator');
|
|
}
|
|
}
|
|
},
|
|
//responsive: true,
|
|
order: [[10, 'desc']],
|
|
language: {
|
|
emptyTable: "No record available"
|
|
},
|
|
error: function (xhr, error, thrown) {
|
|
console.log('DataTables error:', error);
|
|
console.log('Status:', Status);
|
|
console.log('Details:', xhr.responseText);
|
|
window.location.href = '/Home/Logout';
|
|
}
|
|
});
|
|
// Function to update the visibility of the submit button
|
|
function updateSubmitButtonVisibility() {
|
|
var isEmpty = itemTable.data().length === 0; // Check if the table is empty
|
|
|
|
// Toggle the visibility of the submit button based on whether the table is empty or not
|
|
submitButton.toggle(!isEmpty);
|
|
}
|
|
|
|
// Event handler for individual checkbox change
|
|
$('#ItemCartTable').on('change', '.selectedItem-checkbox', function () {
|
|
var row = $(this).closest('tr'); // Get the closest row for the checkbox
|
|
if ($(this).prop('checked')) {
|
|
row.addClass('selected-row'); // Add highlight class if checked
|
|
} else {
|
|
row.removeClass('selected-row'); // Remove highlight class if unchecked
|
|
}
|
|
updateTotalSelectedCount(); // Update the total selected count
|
|
});
|
|
|
|
// Event handler for "Select All" checkbox change in the header
|
|
$('#selectAllHeaderCheckbox').on('change', function () {
|
|
var isChecked = $(this).prop('checked'); // Check if "Select All" checkbox is checked
|
|
|
|
// Check or uncheck all checkboxes in the table based on the state of "Select All" checkbox
|
|
$('.selectedItem-checkbox').prop('checked', isChecked);
|
|
|
|
// Highlight or unhighlight all rows based on "Select All" checkbox state
|
|
if (isChecked) {
|
|
$('#ItemCartTable tbody tr').addClass('selected-row');
|
|
} else {
|
|
$('#ItemCartTable tbody tr').removeClass('selected-row');
|
|
}
|
|
updateTotalSelectedCount(); // Update the total selected count
|
|
});
|
|
function updateTotalSelectedCount() {
|
|
var totalSelected = $('.selectedItem-checkbox:checked').length; // Count the checked checkboxes
|
|
totalSelectedLabel.text(totalSelected); // Update the label with the count
|
|
}
|
|
})
|
|
function deleteCartItemById(data) {
|
|
loader = $('#overlay, #loader');
|
|
|
|
var ItemCartId = data.itemCartId;
|
|
var ItemNo = data.itemNo;
|
|
|
|
showConfirmation({
|
|
title: 'Item Cart Deletion',
|
|
message: 'Are you sure you want to delete this item? This action cannot be undone.',
|
|
type: 'danger',
|
|
confirmText: 'Yes, Delete',
|
|
cancelText: 'Keep Item'
|
|
}).then((confirmed) => {
|
|
if (confirmed) {
|
|
$.ajax({
|
|
url: '/ItemMgmt/PostPutItemCart',
|
|
type: 'POST',
|
|
data: { ItemNo, ItemCartId },
|
|
success: function (response) {
|
|
if (response.success) {
|
|
itemTable.ajax.reload();
|
|
var totalSelectedLabel = $('#totalSelected');
|
|
totalSelectedLabel.text('');
|
|
showToast('success', 'Selected item deleted!', 'Success', 4000);
|
|
} else {
|
|
loader.hide();
|
|
showToast('error', response.response, title + ' failed', 4000);
|
|
}
|
|
},
|
|
beforeSend: function () {
|
|
loader.show();
|
|
},
|
|
complete: function () {
|
|
loader.hide();
|
|
}
|
|
});
|
|
}
|
|
});
|
|
}
|