File "table0027.php"

Full Path: /home/analogde/www/Dev tableau/table0027.php
File size: 6.36 KB
MIME-type: text/html
Charset: utf-8

<!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;
        }
        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-odd-header {
            background-color: #e0f7fa;
        }
        .week-even-header {
            background-color: #ffecb3;
        }
    </style>
</head>
<body>
    <div class="table-container">
        <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");
                    }
                    $currentWeek = null;
                    $weekClass = "week-even-header";
                    foreach ($dates as $data) {
                        if ($currentWeek !== $data['week']) {
                            $currentWeek = $data['week'];
                            $weekClass = ($weekClass === "week-even-header") ? "week-odd-header" : "week-even-header";
                        }
                        echo "<th class='$weekClass'>{$data['date']}</th>";
                    }
                ?>
            </tr>
            <tr>
                <th class="fixed-column">Semaine</th>
                <th class="second-fixed-column"></th>
                <?php 
                    $currentWeek = null;
                    $colspan = 0;
                    foreach ($dates as $index => $data) {
                        if ($currentWeek === $data['week']) {
                            $colspan++;
                        } else {
                            if ($colspan > 0) {
                                echo "<th colspan='$colspan'>$currentWeek</th>";
                            }
                            $currentWeek = $data['week'];
                            $colspan = 1;
                        }
                    }
                    if ($colspan > 0) {
                        echo "<th colspan='$colspan'>$currentWeek</th>";
                    }
                ?>
            </tr>
            <?php 
                $rows = ['toto', 'titi', 'lulu'];
                foreach ($rows as $row) {
                    echo "<tr><td class='fixed-column' onclick='editCell(this)'>$row</td>";
                    echo "<td class='second-fixed-column total-column'>0</td>";
                    foreach ($dates as $data) {
                        echo "<td onclick='toggleCell(this, this.parentElement)'>0</td>";
                    }
                    echo "</tr>";
                }
            ?>
        </table>
    </div>

    <script>
        function editCell(cell) {
            if (cell.querySelector("input")) return;
            var input = document.createElement("input");
            input.type = "text";
            input.value = cell.innerText;
            cell.innerHTML = '';
            cell.appendChild(input);
            input.focus();
            input.addEventListener('keydown', function(event) {
                if (event.key === 'Enter') {
                    cell.innerText = input.value;
                }
            });
            input.addEventListener('blur', function() {
                cell.innerText = input.value;
            });
        }

        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>