form add users multi select okay

This commit is contained in:
Fred Tempez 2023-11-22 14:09:07 +01:00
parent 54f2765247
commit 3a07eaabce

View File

@ -12,6 +12,8 @@
*/ */
$(document).ready((function () { $(document).ready((function () {
$("#courseFilterGroup, #courseFilterFirstName, #courseFilterLastName").change(function () { $("#courseFilterGroup, #courseFilterFirstName, #courseFilterLastName").change(function () {
saveCheckboxState(); saveCheckboxState();
$("#courseUsersFilterForm").submit(); $("#courseUsersFilterForm").submit();
@ -31,6 +33,12 @@ $(document).ready((function () {
] ]
}); });
// Handle checkbox change event
$('.checkboxSelect').on('change', function () {
// Save checkbox state to cookies or local storage
saveCheckboxState();
});
// Handle checkbox state on DataTables draw event // Handle checkbox state on DataTables draw event
table.on('draw', function () { table.on('draw', function () {
// Restore checkbox state from cookies or local storage // Restore checkbox state from cookies or local storage
@ -38,28 +46,37 @@ $(document).ready((function () {
}); });
// Function to save checkbox state // Restore checkbox state on page load
restoreCheckboxState();
function saveCheckboxState() { function saveCheckboxState() {
var checkboxState = [];
// Récupérer d'abord les données existantes dans le localStorage
var existingData = JSON.parse(localStorage.getItem('checkboxState')) || {};
console.log(existingData);
// Ajouter ou mettre à jour les données actuelles
$('.checkboxSelect').each(function () { $('.checkboxSelect').each(function () {
checkboxState.push({ var checkboxId = $(this).attr('id');
'rowIndex': $(this).closest('tr').index(), var checked = $(this).prop('checked');
'checked': $(this).prop('checked') existingData[checkboxId] = checked;
});
}); });
// Use cookies or local storage to store checkbox state
localStorage.setItem('checkboxState', JSON.stringify(checkboxState)); // Sauvegarder les données mises à jour dans le localStorage
localStorage.setItem('checkboxState', JSON.stringify(existingData));
} }
// Function to restore checkbox state // Function to restore checkbox state
function restoreCheckboxState() { function restoreCheckboxState() {
var checkboxState = JSON.parse(localStorage.getItem('checkboxState')) || []; var checkboxState = JSON.parse(localStorage.getItem('checkboxState')) || {};
checkboxState.forEach(function (item) { // console.log(checkboxState);
var rowIndex = item.rowIndex; for (var checkboxId in checkboxState) {
var checked = item.checked; if (checkboxState.hasOwnProperty(checkboxId)) {
// Update checkbox state based on stored information var checked = checkboxState[checkboxId];
$('#example tbody tr:eq(' + rowIndex + ') .checkboxSelect').prop('checked', checked); // Update checkbox state based on stored information
}); $('#' + checkboxId).prop('checked', checked);
}
}
} }
})); }));