FancyTree - 如何放入数据内容生成 php 目录

FancyTree - How put in data-content generate php directory

我开始编程和FancyTree。而且我英语很烂^^。我被困住了,一点帮助也不是坏事,谢谢。

我设法为 FancyTree 生成了我的文件和文件夹的 PHP 树结构。到目前为止,一切都很好。 我的问题是我在 FancyTree 中找不到这个结构。当我构建 PHP 时,我必须将相关文件夹或文件所在的所有父文件夹放入“DATA-CONTENT”。

看看我的树: my tree

也许为了更多的理解: 树示例:

要在每个元素的数据内容中插入的期望结果:

在 Github,我开始了我的项目:https://github.com/ScottMeyer48/AllForOne

这是我的 PHP 代码:

<?php
$struct = listFolders();
echo json_encode($struct);

function listFolders($dir = __DIR__ . '/_donnees') {
    $dh = scandir($dir);
    $return = [];

    foreach ($dh as $folder) {
        if ($folder != '.' && $folder != '..') {
            if (is_dir($dir . '/' . $folder)) { // es-ce que c'est un répertoire ?
                $return[] = array(
                    'title' => $folder,
                    'folder' => true,
                    'expanded'=> true,
                    'data-content' => 'Here_Full_Path',
                    'children' => listFolders($dir . '/' . $folder, $key)
                );
            } else { // donc c'est un fichier
                $return[] = [
                    'title' => $folder,
                    'data-content' => 'Here_Full_Path',
                    // 'title' => '<a href="' . $Here_Full_Path. $folder . 'target="_self" ">' . $folder . '</a>',
                ];
            }
        }
    }
    return $return;
}
?>

这是有效但不完整的结果:

    {"expanded":true,"key":"root_1","title":"root","children":[
    {"expanded":true,"folder":true,"key":"_2","title":"n0_A_Fol1","data":{"data-content":"Here_Full_Path"},"children":[
        {"expanded":true,"folder":true,"key":"_3","title":"n1_A_Fol1","data":{"data-content":"Here_Full_Path"},"children":[
            {"expanded":true,"folder":true,"key":"_4","title":"n2_A_Fol1","data":{"data-content":"Here_Full_Path"},"children":[
                {"expanded":true,"folder":true,"key":"_5","title":"n3_A_Fol1","data":{"data-content":"Here_Full_Path"},"children":[
                    {"key":"_6","title":"n3_A_files1.txt","data":{"data-content":"Here_Full_Path"}},
                    {"key":"_7","title":"n3_A_files2.txt","data":{"data-content":"Here_Full_Path"}}]}]},
            {"key":"_8","title":"n2_A_files.txt","data":{"data-content":"Here_Full_Path"}}]}]},
    {"expanded":true,"folder":true,"key":"_9","title":"n0_B_Fol2","data":{"data-content":"Here_Full_Path"},"children":[
        {"expanded":true,"folder":true,"key":"_10","title":"n1_B_D1","data":{"data-content":"Here_Full_Path"},"children":[
            {"key":"_11","title":"n2_B_files1.txt","data":{"data-content":"Here_Full_Path"}},
            {"key":"_12","title":"n2_B_files2.txt","data":{"data-content":"Here_Full_Path"}}]},
        {"expanded":true,"folder":true,"key":"_13","title":"n1_B_D2","data":{"data-content":"Here_Full_Path"},"children":[
            {"key":"_14","title":"n2_B_files.txt","data":{"data-content":"Here_Full_Path"}}]},
        {"key":"_15","title":"n1_B_files.txt","data":{"data-content":"Here_Full_Path"}}]}]}

我寻求这个结果,我认为在 PHP 代码中将所有文件夹父级堆叠在数据内容中。 :

{"expanded":true,"key":"root_1","title":"root","children":[
    {"expanded":true,"folder":true,"key":"_2","title":"n0_A_Fol1","data":{"data-content":"/n0_A_Fol1"},"children":[
        {"expanded":true,"folder":true,"key":"_3","title":"n1_A_Fol1","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1"},"children":[
            {"expanded":true,"folder":true,"key":"_4","title":"n2_A_Fol1","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1"},"children":[
                {"expanded":true,"folder":true,"key":"_5","title":"n3_A_Fol1","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1/n3_A_Fol1"},"children":[
                    {"key":"_6","title":"n3_A_files1.txt","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1/n3_A_Fol1/"}},
                    {"key":"_7","title":"n3_A_files2.txt","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1/n3_A_Fol1/"}}]}]},
            {"key":"_8","title":"n2_A_files.txt","data":{"data-content":"/n0_A_Fol1/n1_A_Fol1/n2_A_Fol1/"}}]}]},
    {"expanded":true,"folder":true,"key":"_9","title":"n0_B_Fol2","data":{"data-content":"/n0_B_Fol2"},"children":[
        {"expanded":true,"folder":true,"key":"_10","title":"n1_B_D1","data":{"data-content":"/n0_B_Fol2/n1_B_D1"},"children":[
            {"key":"_11","title":"n2_B_files1.txt","data":{"data-content":"/n0_B_Fol2/n1_B_D1/"}},
            {"key":"_12","title":"n2_B_files2.txt","data":{"data-content":"/n0_B_Fol2/n1_B_D1/"}}]},
        {"expanded":true,"folder":true,"key":"_13","title":"n1_B_D2","data":{"data-content":"/n0_B_Fol2/n1_B_D2"},"children":[
            {"key":"_14","title":"n2_B_files.txt","data":{"data-content":"/n0_B_Fol2/n1_B_D2/"}}]},
        {"key":"_15","title":"n1_B_files.txt","data":{"data-content":"/n0_B_Fol2/n1_B_D1/"}}]}]}

我试过了,差不多了。但不幸的是,这对我来说太难了......

function ListFolder($Folder, $SkipFileExts, $SkipObjects) // Fonction pour lister les dossier
{
    $dir = opendir($Folder);
    while (false !== ($Current = readdir($dir))) // Boucle sur tout ce qu'il y a dans le répertoire
    {
        if ($Current !='.' && $Current != '..' && in_array($Current, $SkipObjects)===false)
        {
            if(is_dir($Folder.'/'.$Current)) // Si c'est un dossier
            {
                echo '<strong style="color: #ff0000;">'.$Current.'</strong><br>'; 

                $path_all = $Current;
                echo dirname($_SERVER['PHP_SELF']) . '/' . $path_all . '/';

                ListFolder($Folder.'/'.$Current, $SkipFileExts, $SkipObjects); // On liste les dossiers contenue dans le dossier (récursivité)
            }
            else
            {
                $FileExt = strtolower(substr(strrchr($Current ,'.'),1));
                if (in_array($FileExt, $SkipFileExts)===false) // On regarde si on dois afficher cette extension
                    echo $Current.'<br>';
            }
        }
    }
    closedir($dir); 
};

$Folder = '_donnees';
$SkipExts = array('tot', 'php', 'db');
$SkipObjects = array('UnDossier', 'UnFichier');
ListFolder($Folder, $SkipExts, $SkipObjects);

给出这个结果:

n0_A_Fol1
/fancyDIV/n0_A_Fol1/n1_A_Fol1
/fancyDIV/n1_A_Fol1/n2_A_Fol1
/fancyDIV/n2_A_Fol1/n3_A_Fol1
/fancyDIV/n3_A_Fol1/n3_A_files1.txt
n3_A_files2.txt
n2_A_files.txt
n0_B_Fol2
/fancyDIV/n0_B_Fol2/n1_B_files.txt
n1_B_D1
/fancyDIV/n1_B_D1/n2_B_files1.txt
n2_B_files2.txt
n1_B_D2
/fancyDIV/n1_B_D2/n2_B_files.txt

非常感谢你能给我的所有帮助


经过多次尝试,我自己找到了解决方案:)

我们当然可以做得更好。

<?php
$struct = listFolders();
echo json_encode($struct);

function listFolders($dir = __DIR__ . '/datausers') {
    $dh = scandir($dir);
    
    $return = [];

    foreach ($dh as $folder) {
        if ($folder != '.' && $folder != '..') {
            
            $url_site = "http://your_url";
            $Folder_data = "/datausers";
            
            if (is_dir($dir . '/' . $folder)) { // it's a folder
                
                $full_path_absolu = glob($dir . '/*');
                $full_path_relatif = str_replace ( __DIR__ . $Folder_data, "" , $full_path_absolu);
                $index_path = 0; 
                foreach ($full_path_relatif as $full_path_folder) {
                    // echo "[$index_path] => $full_path_folder </br>";
                    $index_path++;
                }

                $return[] = array(
                    'title' => $folder,
                    'data-content' => $full_path_folder,
                    'folder' => true,
                    'expanded'=> true,
                    'children' => listFolders($dir . '/' . $folder, $key)
                );
            } else { // it's a file

                $full_path_absolu = glob($dir . '/*');
                $full_path_relatif = str_replace ( __DIR__ . $Folder_data, "" , $full_path_absolu);
                $index_path = 0; 
                foreach ($full_path_relatif as $full_path_folder) {
                    // echo "[$index_path] => $full_path_folder </br>";
                    $index_path++;
                }

                $return[] = [
                    'title' => $folder,
                    'data-content' => $full_path_folder,
                    'title' => $folder,
                    // 'title' => '<a href="' . $url_site . $Folder_data . $full_path_folder . '">' . $folder . '</a>',
                ];
            }
        }
    }
    return $return;
}
?>

我的问题解决了

我不是在 php 中构建创造之路。 我正在使用 fancytree 通过此功能重建此路径

click: function (e, data) { 
  var node = data.node; 
  var path = node.getPath(); 
  console.log(path); 
},