简体   繁体   English

如何使用JavaScript使用字符串路径列表创建动态树结构

[英]how to create dynamic tree structure with list of string paths using javascript

I have a list of strings with are paths to some file with "/" separator that are available to my jsp/html page . 我有一个字符串列表,这些字符串是指向带有“ /”分隔符的文件的路径,这些路径可用于我的jsp / html页面。 ie

d1/d2/d3/file1.c
d1/d2/d3/file2.java
d1/d2/file3.jsp
d1/d2/file4.asp
d1/d2/d3/d4/file4.asp
d11/d22/d33/file5.txt

The above list of string paths are available to my page. 上面的字符串路径列表可用于我的页面。 I need to make a dynamic tree structre with above data in below tree structure using javascript. 我需要使用javascript在下面的树结构中使用上面的数据制作一个动态树结构。

+d1/d2/
 file3.jsp
 file4.asp

+d1/d2/d3/
 file1.c
 file2.java

+d1/d2/d3/d4/
 file4.asp

+d11/d22/d33/
 file5.txt

When I click on the common path like +di/d2 it should expand to show all the files under that directory , and when again click on it should hide the child files. 当我单击+ di / d2之类的通用路径时,它应该展开以显示该目录下的所有文件,而再次单击它时,则应隐藏子文件。 Simmilarly for all other nodes. 类似地,用于所有其他节点。

Since you have no code to show us, I'm not going to write it all for you. 由于您没有可向我们展示的代码,因此我不会为您编写所有代码。 Just an overview, to tell you the method, you'll have to write your own code: 只是概述,要告诉您该方法,您必须编写自己的代码:

  1. Store the paths as strings in javascript 将路径以字符串形式存储在javascript中
  2. Call string.split("/") on each of them and store results in arrays 分别对它们调用string.split(“ /”)并将结果存储在数组中
  3. Create object that will hold the tree 创建将容纳树的对象
  4. Match all top leveled arrays that are similar, and then append all of the unique top level directories as children to the object created in step 3. 匹配所有相似的顶级数组,然后将所有唯一的顶级目录作为子级附加到在步骤3中创建的对象。
  5. Continue process using a trickle-down method, adding children to the children of the top level directories. 使用细流向下方法继续处理,将子级添加到顶级目录的子级中。
  6. Render the results probably as a list, by appending the children to the list objects that you create, using the trickle down method again. 再次通过使用细流向下方法将子级附加到您创建的列表对象中,从而可能将结果呈现为列表。

For reference: Javascript building tree hierarchy 供参考: Javascript构建树层次结构

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

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