File "tree_view08.php"
Full Path: /home/analogde/www/Design/2024_PHP_02_01_2025/tree_view08.php
File size: 3.68 KB
MIME-type: text/x-php
Charset: utf-8
<?php
// Initialiser le tableau multidimensionnel
$filesArray = [
"fichiers" => [
"fichier1" => ["fichier2", "fichier3"],
"toto1" => ["toto2", "toto3", "toto4"],
]
];
// Pour afficher le tableau pour vérification
echo '<pre>';
print_r($filesArray);
echo '</pre>';
// Convertir en JSON pour utilisation ultérieure
$jsonData = json_encode($filesArray);
?>
<!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>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" />
<style>
ul {
list-style-type: none;
padding-left: 20px;
position: relative;
}
li {
position: relative;
padding: 5px 0;
}
.toggle {
cursor: pointer;
color: blue;
text-decoration: underline;
}
.icon {
margin-right: 5px;
}
/* Lignes de connexion */
li:before {
content: '';
position: absolute;
left: -10px; /* Ajuster selon besoin */
top: 20%; /* Position verticale */
bottom: 50%; /* Ajuster selon besoin */
width: 1px;
background-color: #000;
}
li:last-child:before {
height: 50%; /* Pour le dernier enfant, ne pas continuer la ligne */
top: 50%; /* Positionner correctement */
}
li:after {
content: '';
position: absolute;
left: -10px; /* Ajuster selon besoin */
top: 50%;
width: 10px; /* Ajuster selon besoin */
height: 1px;
background-color: #000;
}
/* Cacher les sous-listes par défaut */
ul ul {
display: none; /* Caché par défaut */
}
</style>
</head>
<body>
<h1>Arborescence des fichiers</h1>
<ul id="fileTree"></ul>
<script>
// Utiliser le JSON généré par PHP
const filesData = <?php echo $jsonData; ?>;
function createTree(files) {
const ul = document.createElement('ul');
for (const [parent, children] of Object.entries(files.fichiers)) {
const li = document.createElement('li');
const span = document.createElement('span');
span.classList.add('toggle');
// Créer une icône pour le parent
const icon = document.createElement('i');
icon.classList.add('fas', 'fa-folder', 'icon'); // Icône de dossier
span.appendChild(icon);
span.appendChild(document.createTextNode(parent)); // Ajouter le nom du parent
li.appendChild(span);
if (children.length > 0) {
const childUl = createTree({ fichiers: { [parent]: children } });
li.appendChild(childUl); // Ajouter les sous-listes
}
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';
}
});
});
// Initialiser l'affichage des sous-listes
/*
document.querySelectorAll('#fileTree ul').forEach(ul => {
ul.style.display = 'none'; // Par défaut, caché
});*/
</script>
</body>
</html>