duanguanye4124
2012-04-16 16:30 阅读 39

文件夹选择对话的性能

I need to present a folder selection of the entire hard drive of the user (script runs on the user pc). From that list the user can choose one or more folders. I am using the jquery library dynatree (http://code.google.com/p/dynatree/) for presentation.

My script works like this:

1.List all folders of a specified drive letter or path and parse it into Dynatree-readable format via this recursive function:

Code:

function getDirectory( $path = '.', $level = 0, $id = 0){ 
$ignore = array( 'cgi-bin', '.', '..' , 'windows', 'user_folders', 'user_profiles'); 
$dh = @opendir( $path ); 
$zaehl = 0; 
if($level != 0) echo(', children: [ ');
while( false !== ( $file = readdir( $dh ))){ 
$zaehl++;
      if( !in_array( strtolower($file), $ignore ) ){ 
        $spaces = str_repeat( ' ', ( $level * 4 ) ); 
        if( is_dir( "$path/$file" ) ){
        echo('{title: "'.$file.'", isFolder: true, key: "'.$path.'/'.$file.'"');
    echo("
"); 
            getDirectory( "$path/$file", ($level+1), $id.'.'.$zaehl); 
    echo('},');
    echo("
");

        }       

    } 

} 
 if($level != 0)
    {
        echo(']');
    }
closedir( $dh ); 
} 

2.Present the list using Dynatree:

        $("#tree3").dynatree({

        checkbox: true,

        selectMode: 3,

        children: treeData,

        onSelect: function(select, node) {

            var selKeys = $.map(node.tree.getSelectedNodes(), function(node){

                return node.data.key;

            });
            var selRootNodes = node.tree.getSelectedNodes(true);
            var selRootKeys = $.map(selRootNodes, function(node){
                return node.data.key;
            });
            $("#echoSelectionRootKeys3").text(selRootKeys.join(", "));
            $("#echoSelectionRoots3").text(selRootNodes.join(", "));
        },
        onDblClick: function(node, event) {
            node.toggleSelect();
        },
        onKeydown: function(node, event) {
            if( event.which == 32 ) {
                node.toggleSelect();
                return false;
            }
        }
    });

I don´t think that process number 2 is the problem, as i basically copied it from the creators tutorial. I think that I´ve made a mistake during process 1 while listing the folders.

Running the getDirectory script with a parameter of a harddrive with few folders it works like a charm. When it comes to (typically) harddrives with lots of nested folders it lasts there several minutes.

Does anybody know a better way of listing the folders or a complete different solution for presenting a server-side folder creation dialogue?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dpwjx32578146 dpwjx32578146 2012-04-18 12:44

    If you are complaining about speed, I would suggest Lazy Loading. If you are loading every folder/file in your directory, it will become very slow.

    点赞 评论 复制链接分享
  • duanpo8329 duanpo8329 2012-04-16 16:58

    A complete different approach would be to use PHP for getting content of a specific folder only (no recursion). If you use AJAX to do the recursion and to browse deeper into your directory structure, you can do that in parallel.

    I would assume that this will increase speed dramatically. Additionally you can show already something everybody will be pleased to see data so soon, even if it's not completed.

    点赞 评论 复制链接分享