<!DOCTYPE html> <html lang="fr"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tableau HTML</title> <style> .table-container { width: 100%; overflow-x: auto; margin-bottom: 10px; } table { border-collapse: separate; border-spacing: 0; width: 100%; table-layout: auto; } th, td { padding: 5px; text-align: center; border: 1px solid black; box-sizing: border-box; } .fixed-column { position: sticky; left: 0; background: white; z-index: 2; border-left: 2px solid black; border-right: 1px solid black; padding-left: 10px; padding-right: 10px; width: 222px; min-width: 222px; } .second-fixed-column { position: sticky; left: 222px; background: white; z-index: 2; border-right: 1px solid black; padding-left: 10px; padding-right: 10px; width: 100px; min-width: 100px; } th.fixed-column, th.second-fixed-column { top: 0; z-index: 3; } td:first-child { border-left: 2px solid black; } .active-cell { background-color: green; color: white; } .total-column { background-color: #f0f0f0; pointer-events: none; } .week-even { background-color: #e0f7fa; } .week-odd { background-color: #ffecb3; } .button-container { text-align: center; margin-top: 5px; } .date-even { background-color: #e0f7fa; } .date-odd { background-color: #ffecb3; } .disabled-cell { background-color: #d3d3d3; /* Gris pour les cellules désactivées */ pointer-events: none; /* Désactive le clic */ } </style> </head> <body> <div class="table-container"> <table id="data-table"> <tr> <th class="fixed-column">Nom</th> <th class="second-fixed-column">Total</th> <?php $start_date = new DateTime("2025-01-01"); $end_date = new DateTime("2025-12-31"); $holidays = [ "01/01", "01/05", "08/05", "14/07", "15/08", "01/11", "11/11", "25/12", date("d/m", strtotime("2025-05-29")), date("d/m", strtotime("2025-06-08")) ]; $dates = []; while ($start_date <= $end_date) { $dayOfWeek = $start_date->format("N"); $formattedDate = $start_date->format("d/m"); if ($dayOfWeek < 6 && !in_array($formattedDate, $holidays)) { $weekNumber = $start_date->format("W"); $dates[] = ["date" => $formattedDate, "week" => $weekNumber]; } $start_date->modify("+1 day"); } // Affichage des dates dans la première ligne foreach ($dates as $data) { // Appliquer les couleurs de fond alternées selon les semaines paires ou impaires $weekClass = ($data['week'] % 2 == 0) ? "date-even" : "date-odd"; echo "<th class='$weekClass'>{$data['date']}</th>"; } ?> </tr> <tr> <td class="fixed-column">Semaine</td> <td class="second-fixed-column"></td> <?php $currentWeek = null; $count = 1; // Compteur pour savoir combien de dates partagent la même semaine // Affichage des semaines dans la deuxième ligne foreach ($dates as $index => $data) { // Si on change de semaine, on fusionne les cellules des semaines précédentes if ($currentWeek !== $data['week']) { if ($currentWeek !== null) { // Fusionner les cellules des semaines précédentes echo "<td colspan='$count'>{$currentWeek}</td>"; } $currentWeek = $data['week']; $count = 1; // Réinitialiser le compteur pour la nouvelle semaine } else { $count++; // Incrémenter le compteur si la semaine est la même } } // Dernière cellule pour la semaine if ($currentWeek !== null) { echo "<td colspan='$count'>{$currentWeek}</td>"; } ?> </tr> </table> </div> <div class="button-container"> <button onclick="addRow()">Ajouter une ligne</button> </div> <script> function addRow() { let table = document.getElementById("data-table"); let row = table.insertRow(-1); let cell1 = row.insertCell(0); cell1.classList.add("fixed-column"); let input = document.createElement("input"); input.type = "text"; input.addEventListener("keydown", function(event) { if (event.key === "Enter") { input.blur(); } }); cell1.appendChild(input); let cell2 = row.insertCell(1); cell2.classList.add("second-fixed-column", "total-column"); cell2.innerText = "0"; let currentDate = new Date(); // Date actuelle en format Date JS for (let i = 2; i < table.rows[0].cells.length; i++) { let cell = row.insertCell(i); let dateText = table.rows[0].cells[i].innerText; let [day, month] = dateText.split("/").map(num => parseInt(num, 10)); // Créer un objet Date JavaScript let cellDate = new Date(currentDate.getFullYear(), month - 1, day); // Si la date est antérieure à aujourd'hui, désactiver la cellule if (cellDate < currentDate) { cell.classList.add("disabled-cell"); cell.style.pointerEvents = "none"; // Empêcher le clic } else { cell.onclick = function() { toggleCell(cell, row); }; } cell.innerText = "0"; } } function toggleCell(cell, row) { if (cell.innerText === '0') { cell.innerText = '1'; cell.classList.add('active-cell'); } else { cell.innerText = '0'; cell.classList.remove('active-cell'); } updateTotal(row); } function updateTotal(row) { let total = 0; for (let i = 2; i < row.cells.length; i++) { let cellValue = parseInt(row.cells[i].innerText); if (!isNaN(cellValue)) { total += cellValue; } } row.cells[1].innerText = total; } </script> </body> </html>