简体   繁体   English

递归函数为Angular.Treeview创建Treeview

[英]Recursive Function to create Treeview for Angular.Treeview

I'm trying to come up with a recursive function that I can use to create the tree view structure that angular tree view uses. 我试图提出一个递归函数,可以用来创建角树视图使用的树视图结构。

Here's a link that shows the structure: https://github.com/eu81273/angular.treeview 这是显示结构的链接: https : //github.com/eu81273/angular.treeview

I'm trying to create a tree view based on a file directory on the server and wanted to pass it back from the Java code with JSON. 我试图基于服务器上的文件目录创建树形视图,并希望将其从Java代码中以JSON传递回来。 I'm struggling mostly with creating the function that will loop through the directory and create the necessary structure with children. 我主要是在创建将遍历目录并创建带有子项的必要结构的函数而苦苦挣扎。

I've created a POJO class to reflect the tree view structure server side. 我创建了一个POJO类来反映树视图结构服务器端。 I was trying to use the following function as a basis for this one: 我试图使用以下功能作为此功能的基础:

private static void listFiles(File rootDir, String[] files){
    for (String fileName: files){
        File fileOrDir = new File(rootDir, fileName); 
        if (fileOrDir.isDirectory()){
            listFiles(fileOrDir, fileOrDir.list());
        } else {
            System.out.println(fileOrDir); 
        }
    }
}

Here's a clip of what the object looks like: 以下是对象外观的片段:

public class AngularTreeview {
private String label; 
private String id; 
private Collection<AngularTreeview> children;
}

An suggestions on the function would be awesome. 关于功能的建议会很棒。 Also alternatives on how to browse the file system on the server or easier ways to create the tree view structure is good to. 另外,关于如何浏览服务器上文件系统或创建树形视图结构的更简便方法的替代方法也很不错。 Thanks! 谢谢!

public static int level = 0;

private static void listFiles(File rootDir, String[] files){

    String name = rootDir.toString();       
    System.out.println(name.substring(name.lastIndexOf('\\') + 1));

    for (String fileName: files){
        File fileOrDir = new File(rootDir, fileName); 
        if (fileOrDir.isDirectory()){   

            level += 1;
            for(int i = 0; i < level; ++i)
                System.out.print('*');          

            listFiles(fileOrDir, fileOrDir.list());
            level -= 1;         
        } else {            
            for(int i = 0; i <= level; ++i)
                System.out.print('*');
            System.out.println(fileOrDir); 
        }           
    }   

}

you can use other way to keep track of level and replace ('*') by your needed spaces or '\\t'. 您可以使用其他方式跟踪级别,并用所需的空格或'\\ t'代替('*')。

    File root = new File("C:\\Root");
    String[] files = root.list();
    listFiles(root, files);

output for your example : 您的示例的输出:

Root
*admin *管理员
**C:\\Root\\admin\\subAdmin1.txt ** C:\\ Root \\ admin \\ subAdmin1.txt
**subAdmin2 ** subAdmin2
***subAdmin2-1 *** subAdmin2-1
****C:\\Root\\admin\\subAdmin2\\subAdmin2-1\\subAdmin2-1-1.txt **** C:\\ Root \\ admin \\ subAdmin2 \\ subAdmin2-1 \\ subAdmin2-1-1.txt
****C:\\Root\\admin\\subAdmin2\\subAdmin2-1\\subAdmin2-1-2.txt **** C:\\ Root \\ admin \\ subAdmin2 \\ subAdmin2-1 \\ subAdmin2-1-2.txt
*guest *客人
**C:\\Root\\guest\\subguest1.txt ** C:\\ Root \\ guest \\ subguest1.txt
**subguest2 ** subguest2
*user *用户
**C:\\Root\\user\\superuser1.txt ** C:\\ Root \\ user \\ superuser1.txt
**superUser2 ** superUser2

This is what I ended up going with, I'm still working out using the results with angular treeview but I'll post that when it works. 这就是我最终要解决的问题,我仍在使用带有角树视图的结果进行计算,但会在工作时将其发布。

public AngularTreeview getDirectoryTreeview(){
    File node = new File("C:\\[yourlocation]");     
    String[] subNote = node.list();

    AngularTreeview treeview = new AngularTreeview(node.getName());
    treeview.setChildren(setChildrenFunction(node, subNote));

    return treeview; 
}

public Collection<AngularTreeview> setChildrenFunction(File rootDir, String[] subfiles) {
    Collection<AngularTreeview> treecol = new ArrayList<AngularTreeview>(); 

    for (String fileName : subfiles){

        AngularTreeview child = new AngularTreeview(fileName);
        File fileOrDir = new File(rootDir, fileName); 
        if (fileOrDir.isDirectory()){
            child.setChildren(setChildrenFunction(fileOrDir, fileOrDir.list()));
        }

        treecol.add(child); 
    } 
    return treecol; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM