File "tree_view07.php"
Full Path: /home/analogde/www/Bonding/tree_view07.php
File size: 4.56 KB
MIME-type: text/x-php
Charset: utf-8
<?php
// Initialiser le tableau
$filesArray = [
"fichiers" => []
];
// Fonction pour ajouter un fichier avec ses enfants
function addFile($parent, $children) {
global $filesArray; // Accéder au tableau global
// Si le parent n'existe pas encore, on l'initialise
if (!isset($filesArray['fichiers'][$parent])) {
$filesArray['fichiers'][$parent] = [];
}
// Ajouter les enfants au parent
foreach ($children as $child) {
$filesArray['fichiers'][$parent][] = $child;
}
}
// Déclaration des fichiers et enfants
addFile("fichier1", ["fichier2", "fichier3"]);
addFile("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 = document.createElement('ul'); // Créer un nouvel ul pour les enfants
children.forEach(child => {
const childLi = document.createElement('li');
// Créer une case à cocher pour l'enfant
const childCheckbox = document.createElement('input');
childCheckbox.type = 'checkbox';
childCheckbox.id = child; // ID unique pour chaque enfant
childLi.appendChild(childCheckbox);
childLi.appendChild(document.createTextNode(child)); // Afficher le nom de l'enfant
childUl.appendChild(childLi);
});
li.appendChild(childUl); // Ajouter le ul des enfants au li du parent
}
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';
}*/
});
});
</sc