var itemTable; let confirmUpdateListener = false; function submitItem() { // Get all selected checkboxes var selectedCheckboxes = $('.selectedItem-checkbox:checked'); // Prepare data array to hold item details var requestData = []; // Iterate over selected checkboxes selectedCheckboxes.each(function () { var $row = $(this).closest('tr'); // Get the DataTable row index var rowIndex = itemTable.row($row).index(); // Get the row data using DataTable API var rowData = itemTable.row(rowIndex).data(); // Extract itemCartId and qty from rowData var itemCartId = rowData.itemCartId; var itemNo = rowData.itemNo; var qty = $row.find('.editable-qty').val() || rowData.qty; // Construct an object with item details var itemData = { itemCartId: itemCartId, ItemNo: itemNo, qty: qty }; // Push itemData into requestData array requestData.push(itemData); }); console.log('requestData', requestData); if (selectedCheckboxes.length <= 0) { alert('No selected item!'); confirmUpdateListener = false; return; } else { $('#addDateNeeded').modal('show'); // Set the z-index of viewApproverDetails modal $('#addDateNeeded').css('z-index', 1060); if (!confirmUpdateListener) { document.getElementById('btnConfirmUpdate').addEventListener('click', function () { var DateNeeded = document.getElementById('dateNeeded').value; // Validate DateNeeded before showing the confirmation dialog if (!DateNeeded) { alert('Please choose date needed!'); confirmUpdateListener = false; return; } // Show a confirmation dialog const confirmation = confirm('Are you sure you want to proceed?'); // Log data for debugging //console.log('DateNeeded', DateNeeded); //console.log('requestData', requestData); if (confirmation) { // If all validation is passed then proceed to confirmation to backend confirmPostPR(DateNeeded, requestData); } }); // Set the flag to true to indicate that the listener is attached confirmUpdateListener = true; } } } function confirmPostPR(DateNeeded, requestData) { var loader = $('#overlay, #loader').css('z-index', 1060); let IsInternalRequest = true; $.ajax({ url: '/ItemMgmt/PostPurchRequest', type: 'POST', data: { ItemCartIds: requestData, DateNeeded: DateNeeded, IsInternalRequest: IsInternalRequest }, // Pass requestData array to the backend success: function (response) { if (response.success) { itemTable.ajax.reload(); $('#addDateNeeded').modal('hide'); alert('Successfully requested!'); var totalSelectedLabel = $('#totalSelected'); totalSelectedLabel.text(''); } else { itemTable.ajax.reload(); alert('Failed: ' + response.response); } }, beforeSend: function () { // Show the loader before making the AJAX request loader.show(); }, complete: function () { // Hide the loader after the AJAX request is complete (success or error) loader.hide(); } }); } function renderItembtns(data, row) { var jsonData = JSON.stringify(row).replace(/"/g, """); var buttonsHtml = ''; // var statusNumber = parseInt(row.status, 10); buttonsHtml += ' '; return buttonsHtml; } $(document).ready(function () { var loader = $('#overlay, #loader'); var submitButton = $('#btnSubmitItem'); var totalSelectedLabel = $('#totalSelected'); // Reference to the label to display the total selected count UserRights = document.getElementById("roleRights").value; let IsInternalRequest = true; itemTable = $('#ItemCartTable').DataTable({ ajax: { url: '/ItemMgmt/GetItemCart', // Replace with your API endpoint type: 'GET', data: { IsInternalRequest }, beforeSend: function () { // Show the loader before making the AJAX request loader.show(); }, complete: function () { loader.hide(); } }, // Check for the "No Data" response and display the message 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 ''; } }, { 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 }, ], "columnDefs": [ { "targets": [5], // Index of the 'qty' column (0-based) "render": function (data, type, row) { // Render the 'qty' column as an input field return ''; } } ], 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 () { 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); 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) { var loader = $('#overlay, #loader'); // Check if UserName or Password is empty or null var ItemCartId = data.itemCartId; var ItemNo = data.itemNo; const confirmation = confirm('Are you sure you want to proceed?'); if (confirmation) { $.ajax({ url: '/ItemMgmt/PostPutItemCart', type: 'POST', data: { ItemNo, ItemCartId }, success: function (response) { if (response.success) { itemTable.ajax.reload(); var totalSelectedLabel = $('#totalSelected'); totalSelectedLabel.text(''); alert('Selected item deleted!'); } else { loader.hide(); alert('Failed: ' + response.response); } }, beforeSend: function () { // Show the loader before making the AJAX request loader.show(); }, complete: function () { // Hide the loader after the AJAX request is complete (success or error) loader.hide(); } }); } }