File "tree_view02.html"

Full Path: /home/analogde/www/Design/2024_PHP_02_01_2025/tree_view02.html
File size: 2.05 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>Tree Viewer</title>
    <style>
        ul {
            list-style-type: none;
            padding-left: 20px;
        }
        .toggle {
            cursor: pointer;
            color: blue;
            text-decoration: underline;
        }
    </style>
</head>
<body>

<h1>Arborescence des fichiers</h1>
<ul id="fileTree"></ul>

<script>
    const filesData = [
        {
            "name": "fichier1",
            "children": [
                { "name": "fichier2" },
                { "name": "fichier3" }
            ]
        },
        {
            "name": "toto1",
            "children": [
                { "name": "toto2" },
                { "name": "toto3" },
                { "name": "toto4" }
            ]
        }
    ];

    function createTree(files) {
        const ul = document.createElement('ul');

        files.forEach(file => {
            const li = document.createElement('li');
            const span = document.createElement('span');
            span.classList.add('toggle');
            span.textContent = file.name;

            li.appendChild(span);

            if (file.children) {
                const childUl = createTree(file.children);
                li.appendChild(childUl);
            }

            ul.appendChild(li);
        });

        return ul;
    }

    const fileTree = createTree(filesData);
    document.getElementById('fileTree').appendChild(fileTree);

    
    document.querySelectorAll('.toggle').forEach(item => {
        item.addEventListener('click', event => {
            const nextUl = item.nextElementSibling;
            if (nextUl) {
                nextUl.style.display = nextUl.style.display === 'none' || nextUl.style.display === '' ? 'block' : 'none';
            }
        });
    });

    // Initial state: hide all nested lists
    /*document.querySelectorAll('#fileTree ul').forEach(ul => {
        ul.style.display = 'none';
    });*/


</script>

</body>
</html>