My web app has the following tree view in order to show some files and files.
ul {
list-style-type: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.14.0/css/all.min.css" />
<ul id="myTreeSelector">
<li>
<span class="caret folder-selector"><i class="fa fa-folder"></i> first_folder</span>
<ul class="nested">
<li class="file"><i class="fa fa-file"></i> app1.dat</li>
<li class="file"><i class="fa fa-file"></i> app2.dat</li>
<li>
<span class="caret folder-selector"><i class="fa fa-folder"></i> second_folder</span>
<ul class="nested">
<li class="file"><i class="fa fa-file"></i> ret.dat</li>
<li><span class="caret folder-selector"><i class="fa fa-folder"></i> third_folder</span></li>
</ul>
</li>
</ul>
</li>
</ul>
This is just an static example, I want to create this folder dinamically from an input array using jQuery.
Following the example above (and adding one last element as new empty directory), my input array is as follows:
arrayOfFiles= ["/first_folder/app1.dat","/first_folder/app2.dat","/first_folder/second_folder/ret.dat", "/first_folder/second_folder/third_folder"]
How can I, using jQuery, iterate that array and create the proper folder structure? This is my try so far, I know I can use appendTo
to add content to the HTML, but it's so hard for me to manage the file-folder stuff.
general_ul = $("#myTreeSelector")
general_li = $('</li>').appendTo(general_ul);
arrayOfPaths.forEach( function(pathOrFile) {
var li = $('<li/>')
.html('<span class="caret folder-selector"><i class="fa fa-folder"></i> first_folder</span>')
.appendTo(general_li);
});
Thanks in advance.