<!DOCTYPE html> <html lang="fr"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Tableau Sticky avec Défilement Horizontal</title> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"> <style> /* Conteneur pour la table avec défilement horizontal */ .table-container { display: flex; overflow-x: auto; margin-bottom: 10px; } /* Conteneur de la première colonne sticky */ .sticky-columns { position: sticky; left: 0; background-color: white; z-index: 2; width: 150px; border-right: 1px solid #000; } /* Conteneur des autres colonnes (dates, semaine, mois) */ .scrollable-columns { overflow-x: auto; white-space: nowrap; display: block; } table { table-layout: fixed; /* Fixe la largeur des colonnes */ width: 100%; } th, td { padding: 5px; text-align: center; border: 1px solid black; } .week-even { background-color: #e0f7fa; } .week-odd { background-color: #ffecb3; } .active-cell { background-color: green; color: white; } .inactive-cell { background-color: #d3d3d3; color: black; } .button-container { text-align: center; margin-top: 10px; } </style> </head> <body> <div class="table-container"> <!-- Première colonne fixe (Nom, Total, Mois, Semaine) --> <div class="sticky-columns"> <table id="data-table-left"> <thead> <tr> <th>Nom</th> <th>Total</th> </tr> </thead> <tbody> <tr> <td>Exemple Nom</td> <td>0</td> </tr> </tbody> </table> </div> <!-- Colonnes défilantes --> <div class="scrollable-columns"> <table id="data-table-right"> <thead> <tr> <!-- Génération dynamique des dates --> <?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"); if ($dayOfWeek < 6 && !in_array($formattedDate, $holidays)) { $weekNumber = $start_date->format("W"); $month = $start_date->format("m"); $dates[] = ["date" => $formattedDate, "week" => $weekNumber, "month" => $month]; } $start_date->modify("+1 day"); } $currentWeek = null; $weekClass = "week-even"; $months = [ "01" => "Janvier", "02" => "Février", "03" => "Mars", "04" => "Avril", "05" => "Mai", "06" => "Juin", "07" => "Juillet", "08" => "Août", "09" => "Septembre", "10" => "Octobre", "11" => "Novembre", "12" => "Décembre" ]; foreach ($dates as $data) { if ($currentWeek !== $data['week']) { $currentWeek = $data['week']; $weekClass = ($weekClass === "week-even") ? "week-odd" : "week-even"; } echo "<th class='$weekClass'>{$data['date']}</th>"; } ?> </tr> </thead> <tbody> <tr> <td>Mois</td> <?php $currentMonth = null; $count = 1; foreach ($dates as $index => $data) { if ($currentMonth !== $data['month']) { if ($currentMonth !== null) { echo "<td colspan='$count'>{$months[$currentMonth]}</td>"; } $currentMonth = $data['month']; $count = 1; } else { $count++; } if ($index == count($dates) - 1) { echo "<td colspan='$count'>{$months[$currentMonth]}</td>"; } } ?> </tr> <tr> <td>Semaine</td> <?php $currentWeek = null; $weekCount = 1; foreach ($dates as $index => $data) { if ($currentWeek !== $data['week']) { if ($currentWeek !== null) { echo "<td colspan='$weekCount'>{$currentWeek}</td>"; } $currentWeek = $data['week']; $weekCount = 1; } else { $weekCount++; } if ($index == count($dates) - 1) { echo "<td colspan='$weekCount'>{$currentWeek}</td>"; } } ?> </tr> </tbody> </table> </div> </div> <div class="button-container"> <button class="btn btn-primary" onclick="addRow()">Ajouter une ligne</button> </div> <script> function addRow() { let table = document.getElementById("data-table-left").getElementsByTagName('tbody')[0]; let row = table.insertRow(-1); let cell1 = row.insertCell(0); cell1.innerText = "Exemple Nom"; let cell2 = row.insertCell(1); cell2.innerText = "0"; // Ajout des nouvelles lignes dans la partie des colonnes défilantes let tableRight = document.getElementById("data-table-right").getElementsByTagName('tbody')[0]; let rowRight = tableRight.insertRow(-1); for (let i = 0; i < tableRight.rows[0].cells.length; i++) { let cell = rowRight.insertCell(i); cell.innerText = "0"; cell.classList.add("inactive-cell"); cell.onclick = function () { toggleCell(cell, rowRight); }; } } function toggleCell(cell, row) { if (cell.innerText === '0') { cell.innerText = '1'; cell.classList.add('active-cell'); cell.classList.remove('inactive-cell'); } else { cell.innerText = '0'; cell.classList.add('inactive-cell'); cell.classList.remove('active-cell'); } } </script> </body> </html>