Update Your JavaScript to Handle the Modal:
Modify your existing JavaScript to display the modal when the "Delete" button is clicked and handle the confirmation or cancellation actions within the modal.
<script>
pdata = {};
const selectedPhones = new Set();
function checkSessionStatus() {
const checkboxes = document.querySelectorAll('tbody input[type="checkbox"]');
const deleteBtn = document.getElementById('deletebtn');
const selectAllCheckbox = document.getElementById('selectAll');
selectAllCheckbox.addEventListener('change', function() {
checkboxes.forEach(checkbox => {
checkbox.checked = this.checked;
const phone = checkbox.closest('tr').cells[1].textContent.trim();
if (this.checked) {
selectedPhones.add(phone);
// Optionally, you can collect more data here if needed
} else {
selectedPhones.delete(phone);
}
});
});
checkboxes.forEach((checkbox) => {
checkbox.addEventListener('change', function() {
const row = this.closest('tr');
const phone = row.cells[1].textContent.trim();
if (this.checked) {
selectedPhones.add(phone);
// Collect additional data if necessary
} else {
selectedPhones.delete(phone);
}
});
});
deleteBtn.addEventListener('click', function() {
if (selectedPhones.size > 0) {
showDeleteConfirmationModal();
} else {
alert('No groups selected for deletion.');
}
});
}
function showDeleteConfirmationModal() {
const modal = document.getElementById('deleteConfirmationModal');
modal.style.display = 'block';
// Get modal elements
const closeModal = modal.querySelector('.close');
const confirmDeleteBtn = modal.querySelector('.confirm-delete');
const cancelDeleteBtn = modal.querySelector('.cancel-delete');
// When the user clicks on <span> (x), close the modal
closeModal.onclick = function() {
modal.style.display = 'none';
};
// When the user clicks on "Cancel", close the modal
cancelDeleteBtn.onclick = function() {
modal.style.display = 'none';
};
// When the user clicks on "Confirm", proceed to delete
confirmDeleteBtn.onclick = function() {
deleteSelectedRows();
modal.style.display = 'none';
};
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = 'none';
}
};
}
function deleteSelectedRows() {
if (selectedPhones.size === 0) {
alert('No groups selected for deletion.');
return;
}
console.log('Phones to delete:', Array.from(selectedPhones)); // Debug log
fetch('/delete-rows-groups', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
},
body: JSON.stringify({ phones: Array.from(selectedPhones) })
})
.then(response => response.json())
.then(data => {
if (data.success) {
selectedPhones.forEach(phone => {
const rows = document.querySelectorAll('tbody tr');
for (let row of rows) {
if (row.cells[1] && row.cells[1].textContent.trim() === phone) {
row.remove();
break;
}
}
});
selectedPhones.clear();
alert('Selected groups deleted successfully.');
} else {
alert('Error deleting groups: ' + data.message);
}
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while deleting groups.');
});
}
function sendreqprofile(){
fetch('/handleprofilegroupdata', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
},
body: JSON.stringify(pdata)
})
.then(response => response.json())
.then(data => {
console.log('Profile data sent successfully:', data);
})
.catch(error => {
console.error('Error sending profile data:', error);
});
}
const viewIdBtn = document.getElementById('aview');
console.log('viewIdBtn:', viewIdBtn);
if (viewIdBtn) {
viewIdBtn.addEventListener('click', function() {
console.log('Button clicked!');
console.log(pdata);
sendreqprofile();
});
} else {
console.error('Button with id "aview" not found');
}
checkSessionStatus();
$(document).ready(function () {
$("#table").DataTable({
pagingType: "full_numbers",
scrollX: true,
});
});
</script>