根据数组生成文件列表

编程入门 行业动态 更新时间:2024-10-08 14:44:38
本文介绍了根据数组生成文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试了一些事情,但是这个星期我感觉我的大脑正在休假,所以我需要完成这件事..所以我希望有人可以帮助我.

I tried a few things but this week i feel like my brain's having holidays and i need to complete this thing.. so i hope someone can help me.

我需要基于保存在数据库中的哈希值创建文件列表.的外观如下:

I need to create a filelist based on a hash which is saved into a database. The has looks like this:

['file1', 'dir1/file2', 'dir1/subdir1/file3']

输出应如下所示:

  • 文件1
  • dir1
    • file2
    • subdir1
      • file3
      • file1
      • dir1
        • file2
        • subdir1
          • file3

          在html中,最好是这样(用js扩展它以折叠和多选)

          in html, preferrably like this (to extend it with js to fold and multiselect)

          <ul> <li>file1 <li>dir1</li> <ul> <li>file2</li> <li>subdir1</li> <ul> <li>file3</li> </ul> </ul> </ul>

          我正在使用Ruby on Rails,并尝试在RJS模板中实现这一点.但这并不重要.您还可以提供一些详细的伪代码来帮助我.

          I'm using Ruby on Rails and try to achieve this in an RJS template. But this don't really matters. You can also help me with some detailed pseudo-code.

          有人知道如何解决吗?

          编辑

          感谢大家提供这些解决方案.清单有效,我将其扩展为可折叠的解决方案以显示/隐藏目录内容.我仍然有一个问题:该代码旨在在同步项后面的复选框中提供完整的文件路径.基于sris的解决方案,我只能读取当前文件及其子文件,而不能读取根目录的整个路径.为了更好的理解:

          Thanks to everyone for these solutions. Listing works, i extended it to a foldable solution to show/hide directory contents. I still have one problem: The code aims to have complete file paths in checkboxes behind the entries for a synchronisation. Based on sris' solution, i can only read the current file and it's subs, but not the whole path from the root. For a better understanding:

          当前:

          [x] dir1 [x] dir2 [x] file1

          给我

          与文本显示相同值的复选框,例如[x] file1的"file1".但是我需要的是完整路径,例如[x] file1的"dir1/dir2/file1".

          a checkbox with the same value a sthe text displays, e.g "file1" for [x] file1. But what i need is a full path, e.g "dir1/dir2/file1" for [x] file1.

          有人还提示如何添加吗?

          Does someone have another hint how to add this?

          推荐答案

          以下是您可以用来启发的快速实现.此实现不考虑输入Array中文件的顺序.

          Here's a quick implementation you can use for inspiration. This implementation disregards the order of files in the input Array.

          我已经更新了解决方案,可以根据需要保存整个路径.

          I've updated the solution to save the entire path as you required.

          dirs = ['file1', 'dir1/file2', 'dir1/subdir1/file3', 'dir1/subdir1/file5'] tree = {} dirs.each do |path| current = tree path.split("/").inject("") do |sub_path,dir| sub_path = File.join(sub_path, dir) current[sub_path] ||= {} current = current[sub_path] sub_path end end def print_tree(prefix, node) puts "#{prefix}<ul>" node.each_pair do |path, subtree| puts "#{prefix} <li>[#{path[1..-1]}] #{File.basename(path)}</li>" print_tree(prefix + " ", subtree) unless subtree.empty? end puts "#{prefix}</ul>" end print_tree "", tree

          此代码将产生像您的示例一样正确缩进的HTML.但是,由于未对Ruby中的哈希(1.8.6)进行排序,因此无法保证文件的顺序.

          This code will produce properly indented HTML like your example. But since Hashes in Ruby (1.8.6) aren't ordered the order of the files can't be guaranteed.

          产生的输出将如下所示:

          The output produced will look like this:

          <ul> <li>[dir1] dir1</li> <ul> <li>[dir1/subdir1] subdir1</li> <ul> <li>[dir1/subdir1/file3] file3</li> <li>[dir1/subdir1/file5] file5</li> </ul> <li>[dir1/file2] file2</li> </ul> <li>[file1] file1</li> </ul>

          我希望这可以作为如何获取路径和文件名的示例.

          I hope this serves as an example of how you can get both the path and the filename.

更多推荐

根据数组生成文件列表

本文发布于:2023-11-12 14:20:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581729.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   文件列表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!