fix edycji ajax
This commit is contained in:
		| @@ -69,117 +69,127 @@ | |||||||
| {% block extra_js %} | {% block extra_js %} | ||||||
| {{ super() }} | {{ super() }} | ||||||
| <script> | <script> | ||||||
| document.addEventListener("DOMContentLoaded", function() { |     document.addEventListener("DOMContentLoaded", function() { | ||||||
|     // Logika edycji w tabeli |         // Logika edycji w tabeli | ||||||
|     document.querySelectorAll(".edit-btn").forEach(button => { |         document.querySelectorAll(".edit-btn").forEach(button => { | ||||||
|         button.addEventListener("click", function() { |             button.addEventListener("click", function(event) { | ||||||
|             let row = this.closest("tr"); |                 event.preventDefault();  // ✅ Zapobiega przeładowaniu strony | ||||||
|             let entryId = this.getAttribute("data-id"); |  | ||||||
|             let ipCell = row.querySelector(".ip-address"); |  | ||||||
|             let hostnameCell = row.querySelector(".hostname"); |  | ||||||
|             let saveButton = row.querySelector(".save-btn"); |  | ||||||
|      |      | ||||||
|             let ipInput = document.createElement("input"); |                 let row = this.closest("tr"); | ||||||
|             ipInput.type = "text"; |                 let entryId = this.getAttribute("data-id"); | ||||||
|             ipInput.className = "form-control form-control-sm"; |                 let ipCell = row.querySelector(".ip-address"); | ||||||
|             ipInput.value = ipCell.textContent.trim(); |                 let hostnameCell = row.querySelector(".hostname"); | ||||||
|  |                 let saveButton = row.querySelector(".save-btn"); | ||||||
|      |      | ||||||
|             let hostnameInput = document.createElement("input"); |                 // Jeśli pola edycji już istnieją, nie duplikuj ich | ||||||
|             hostnameInput.type = "text"; |                 if (ipCell.querySelector("input") || hostnameCell.querySelector("input")) { | ||||||
|             hostnameInput.className = "form-control form-control-sm"; |                     return; | ||||||
|             hostnameInput.value = hostnameCell.textContent.trim(); |                 } | ||||||
|      |      | ||||||
|             ipCell.textContent = ""; |                 let ipInput = document.createElement("input"); | ||||||
|             hostnameCell.textContent = ""; |                 ipInput.type = "text"; | ||||||
|             ipCell.appendChild(ipInput); |                 ipInput.className = "form-control form-control-sm"; | ||||||
|             hostnameCell.appendChild(hostnameInput); |                 ipInput.value = ipCell.textContent.trim(); | ||||||
|      |      | ||||||
|             this.classList.add("d-none"); |                 let hostnameInput = document.createElement("input"); | ||||||
|             saveButton.classList.remove("d-none"); |                 hostnameInput.type = "text"; | ||||||
|  |                 hostnameInput.className = "form-control form-control-sm"; | ||||||
|  |                 hostnameInput.value = hostnameCell.textContent.trim(); | ||||||
|      |      | ||||||
|             saveButton.addEventListener("click", function() { |                 ipCell.textContent = ""; | ||||||
|                 let newIp = ipInput.value.trim(); |                 hostnameCell.textContent = ""; | ||||||
|                 let newHostname = hostnameInput.value.trim(); |                 ipCell.appendChild(ipInput); | ||||||
|  |                 hostnameCell.appendChild(hostnameInput); | ||||||
|      |      | ||||||
|                 fetch(`/local-defaults/update/${entryId}`, { |                 this.classList.add("d-none"); | ||||||
|                     method: "POST", |                 saveButton.classList.remove("d-none"); | ||||||
|                     headers: { "Content-Type": "application/json" }, |  | ||||||
|                     body: JSON.stringify({ ip_address: newIp, hostname: newHostname }) |  | ||||||
|                 }) |  | ||||||
|                 .then(response => response.json()) |  | ||||||
|                 .then(data => { |  | ||||||
|                     if (data.status === "success") { |  | ||||||
|                         ipCell.textContent = newIp; |  | ||||||
|                         hostnameCell.textContent = newHostname; |  | ||||||
|      |      | ||||||
|                         button.classList.remove("d-none"); |                 saveButton.addEventListener("click", function(event) { | ||||||
|                         saveButton.classList.add("d-none"); |                     event.preventDefault();  // ✅ Zapobiega przeładowaniu strony | ||||||
|                     } else { |      | ||||||
|                         alert("Błąd: " + data.message); |                     let newIp = ipInput.value.trim(); | ||||||
|                     } |                     let newHostname = hostnameInput.value.trim(); | ||||||
|                 }) |      | ||||||
|                 .catch(error => { |                     fetch(`/local-defaults/update/${entryId}`, { | ||||||
|                     alert("Wystąpił błąd podczas zapisywania: " + error); |                         method: "POST", | ||||||
|                 }); |                         headers: { "Content-Type": "application/json" }, | ||||||
|  |                         body: JSON.stringify({ ip_address: newIp, hostname: newHostname }) | ||||||
|  |                     }) | ||||||
|  |                     .then(response => response.json()) | ||||||
|  |                     .then(data => { | ||||||
|  |                         if (data.status === "success") { | ||||||
|  |                             ipCell.textContent = newIp; | ||||||
|  |                             hostnameCell.textContent = newHostname; | ||||||
|  |      | ||||||
|  |                             button.classList.remove("d-none"); | ||||||
|  |                             saveButton.classList.add("d-none"); | ||||||
|  |                         } else { | ||||||
|  |                             alert("Błąd: " + data.message); | ||||||
|  |                         } | ||||||
|  |                     }) | ||||||
|  |                     .catch(error => { | ||||||
|  |                         alert("Wystąpił błąd podczas zapisywania: " + error); | ||||||
|  |                     }); | ||||||
|  |                 }, { once: true });  // ✅ Zapobiega dodaniu wielu eventów do przycisku "Zapisz" | ||||||
|  |             }); | ||||||
|  |         }); | ||||||
|  |      | ||||||
|  |         // Logika zaznaczania i usuwania wpisów | ||||||
|  |         let deleteButton = document.getElementById("deleteSelectedBtn"); | ||||||
|  |         let checkboxes = document.querySelectorAll(".entry-checkbox"); | ||||||
|  |         let selectAllCheckbox = document.getElementById("selectAll"); | ||||||
|  |      | ||||||
|  |         function updateDeleteButtonState() { | ||||||
|  |             let anyChecked = Array.from(checkboxes).some(checkbox => checkbox.checked); | ||||||
|  |             deleteButton.disabled = !anyChecked; | ||||||
|  |         } | ||||||
|  |      | ||||||
|  |         checkboxes.forEach(checkbox => { | ||||||
|  |             checkbox.addEventListener("change", updateDeleteButtonState); | ||||||
|  |         }); | ||||||
|  |      | ||||||
|  |         selectAllCheckbox.addEventListener("change", function() { | ||||||
|  |             let isChecked = this.checked; | ||||||
|  |             checkboxes.forEach(checkbox => checkbox.checked = isChecked); | ||||||
|  |             updateDeleteButtonState(); | ||||||
|  |         }); | ||||||
|  |      | ||||||
|  |         deleteButton.addEventListener("click", function() { | ||||||
|  |             let selectedIds = Array.from(checkboxes) | ||||||
|  |                                    .filter(checkbox => checkbox.checked) | ||||||
|  |                                    .map(checkbox => checkbox.value); | ||||||
|  |      | ||||||
|  |             if (selectedIds.length === 0) { | ||||||
|  |                 alert("Nie zaznaczono żadnych wpisów do usunięcia."); | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |      | ||||||
|  |             if (!confirm("Czy na pewno chcesz usunąć zaznaczone wpisy?")) { | ||||||
|  |                 return; | ||||||
|  |             } | ||||||
|  |      | ||||||
|  |             fetch("/local-defaults/delete", { | ||||||
|  |                 method: "POST", | ||||||
|  |                 headers: { "Content-Type": "application/json" }, | ||||||
|  |                 body: JSON.stringify({ entry_ids: selectedIds }) | ||||||
|  |             }) | ||||||
|  |             .then(response => response.json()) | ||||||
|  |             .then(data => { | ||||||
|  |                 if (data.status === "success") { | ||||||
|  |                     selectedIds.forEach(id => { | ||||||
|  |                         let row = document.querySelector(`input[value='${id}']`).closest("tr"); | ||||||
|  |                         row.remove(); | ||||||
|  |                     }); | ||||||
|  |                     updateDeleteButtonState(); | ||||||
|  |                 } else { | ||||||
|  |                     alert("Błąd: " + data.message); | ||||||
|  |                 } | ||||||
|  |             }) | ||||||
|  |             .catch(error => { | ||||||
|  |                 alert("Wystąpił błąd podczas usuwania: " + error); | ||||||
|             }); |             }); | ||||||
|         }); |         }); | ||||||
|     }); |     }); | ||||||
|  |     </script> | ||||||
|      |      | ||||||
|     // Logika zaznaczania i usuwania wpisów |  | ||||||
|     let deleteButton = document.getElementById("deleteSelectedBtn"); |  | ||||||
|     let checkboxes = document.querySelectorAll(".entry-checkbox"); |  | ||||||
|     let selectAllCheckbox = document.getElementById("selectAll"); |  | ||||||
|  |  | ||||||
|     function updateDeleteButtonState() { |  | ||||||
|         let anyChecked = Array.from(checkboxes).some(checkbox => checkbox.checked); |  | ||||||
|         deleteButton.disabled = !anyChecked; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     checkboxes.forEach(checkbox => { |  | ||||||
|         checkbox.addEventListener("change", updateDeleteButtonState); |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     selectAllCheckbox.addEventListener("change", function() { |  | ||||||
|         let isChecked = this.checked; |  | ||||||
|         checkboxes.forEach(checkbox => checkbox.checked = isChecked); |  | ||||||
|         updateDeleteButtonState(); |  | ||||||
|     }); |  | ||||||
|  |  | ||||||
|     deleteButton.addEventListener("click", function() { |  | ||||||
|         let selectedIds = Array.from(checkboxes) |  | ||||||
|                                .filter(checkbox => checkbox.checked) |  | ||||||
|                                .map(checkbox => checkbox.value); |  | ||||||
|  |  | ||||||
|         if (selectedIds.length === 0) { |  | ||||||
|             alert("Nie zaznaczono żadnych wpisów do usunięcia."); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         if (!confirm("Czy na pewno chcesz usunąć zaznaczone wpisy?")) { |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         fetch("/local-defaults/delete", { |  | ||||||
|             method: "POST", |  | ||||||
|             headers: { "Content-Type": "application/json" }, |  | ||||||
|             body: JSON.stringify({ entry_ids: selectedIds }) |  | ||||||
|         }) |  | ||||||
|         .then(response => response.json()) |  | ||||||
|         .then(data => { |  | ||||||
|             if (data.status === "success") { |  | ||||||
|                 selectedIds.forEach(id => { |  | ||||||
|                     let row = document.querySelector(`input[value='${id}']`).closest("tr"); |  | ||||||
|                     row.remove(); |  | ||||||
|                 }); |  | ||||||
|                 updateDeleteButtonState(); |  | ||||||
|             } else { |  | ||||||
|                 alert("Błąd: " + data.message); |  | ||||||
|             } |  | ||||||
|         }) |  | ||||||
|         .catch(error => { |  | ||||||
|             alert("Wystąpił błąd podczas usuwania: " + error); |  | ||||||
|         }); |  | ||||||
|     }); |  | ||||||
| }); |  | ||||||
| </script> |  | ||||||
| {% endblock %} | {% endblock %} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Mateusz Gruszczyński
					Mateusz Gruszczyński