Przedstawię mój problem. Utworzyłem sobie drzewko folderów przy pomocy jQuery Folder Tree Plugin autorstwa Giannis Koutsaftakis. Generalnie skrypt bardzo fajnie działa. Jednak zabrakło mi funkcjonalności filtrowania. Postanowiłem to dorobić. Niestety utknąłem, że hej. Otóż kiedy kliknę w plusik obok folderu, aby go rozwinąć funkcja create_node wykounuje mi się dwa razy, z tymże za pierwszym razem w ogóle nie biorąc pod uwagę parametru filtra, a dokładniej rzecz ujmując o.filter jest undefined. Czy mógłby ktoś powiedzieć co jest nie tak??

Skrypt wywołujący pierwsze filtrowanie:

  1. function filtruj(val)
  2. {
  3. ids = val.split('|');
  4. data = 'akcja=getPath&produktID='+ids[0];
  5. $.post
  6. (
  7. 'ajax/palety.ajax.php',
  8. data,
  9. function (path)
  10. {
  11. paths = path.split(',');
  12. $('#directoryTree').html('');
  13. $('#directoryTree').folderTree({root: 'zdjecia/', script: 'js/jquery_folder_tree/jquery.foldertree.php', loadMessage: 'Ładuję strukturę...', filter: paths});
  14. }
  15. )
  16. }


Pełne źródło pluginu z moją mała modyfikacją:

  1. (function($) {
  2. $.fn.folderTree = function(o) {
  3.  
  4. if( !o ) var o = {};
  5. if( o.root == undefined ) o.root = '/'; //e.g. /root/folder/
  6. if( o.script == undefined ) o.script = 'jquery.foldertree.php';
  7. if( o.loadMessage == undefined ) o.loadMessage = 'Loading...';
  8.  
  9. return this.each(function() { //Builds the whole tree
  10.  
  11. function create_node (dir, target, fol){
  12. var toadd = '<ul class="jqueryFolderTree"><li><a class="home folder sel" href="'+o.root+'">Home</a></li></ul>';
  13.  
  14. if($(fol).hasClass("sel")){
  15. $(fol).removeClass('folder').addClass('waitb');
  16. }else{
  17. $(fol).removeClass('folder').addClass('wait');
  18. }
  19.  
  20.  
  21. $.post(o.script, { dir: dir, filter: o.filter }, function(data) {
  22. $(fol).removeClass('wait waitb').addClass('folder');
  23. if(dir == o.root){ //if is the root dir
  24.  
  25. data = toadd+data;
  26. target.html(data);
  27. target.find("ul.jqueryFolderTree").show();
  28. }else{
  29.  
  30. target.append(data);
  31. target.find("ul.jqueryFolderTree").css({'padding-left':'20px'}).show();
  32. }
  33. });
  34. }
  35.  
  36. $(this).delegate(".collapsed", "click", function(e){
  37. $(this).removeClass("collapsed").addClass("expanded");
  38. var cur_li = $(this).closest("li");
  39. var ul_to = cur_li.find("ul.jqueryFolderTree").first();
  40. if(ul_to.length > 0){
  41. ul_to.show();
  42. }else{
  43. create_node($(this).attr('rel'), cur_li, $(this).next('li a.folder') );
  44. }
  45.  
  46. });
  47.  
  48.  
  49. $(this).delegate(".expanded", "click", function(e){
  50. $(this).removeClass("expanded").addClass("collapsed");
  51. var cur_li = $(this).closest("li");
  52. var ul_to = cur_li.find("ul.jqueryFolderTree").first();
  53. ul_to.hide();
  54. });
  55.  
  56. $(this).delegate(".folder", "click", function(e){
  57. $(".folder", $(this).attr("id")).removeClass("sel");
  58. $(this).addClass("sel");
  59. e.preventDefault();
  60. });
  61.  
  62. $(this).html('<ul class="jqueryFolderTree"><li class="wait">' + o.loadMessage + '</li></ul>');
  63. alert('Hello');
  64. create_node(o.root, $(this));
  65. });
  66.  
  67. }
  68. })(jQuery);


W stosunku do oryginału dodany jest w linii przesyłającej ajaxem dane do skryptu php parametr filter.

Skrypt php nie ma raczej znaczenia. On tylko wyświetla odpowiednio sformatowane drzewko.
Jak nadmieniłem na początku, w momencie kliknięcia w plusik, do rozwinięcia gałęzi drzewka, funkcja create_node wykonuje się 2 razy. DLACZEGO?questionmark.gifquestionmark.gif?

Chociaż w zasadzie kod php też może się przydać.

  1. <?php
  2. //-------------- CONFIG VARS ---------------------------------//
  3.  
  4. $basefolder = ''; //just the name
  5. $base = '/srv/allegro/';
  6. //var_dump($_POST);
  7. //-------------- END FILE BROWSER CONFIG VARS-----------------//
  8.  
  9. function in_multiarray($elem, $array)
  10. {
  11. $top = sizeof($array) - 1;
  12. $bottom = 0;
  13. while($bottom <= $top)
  14. {
  15. if($array[$bottom] == $elem)
  16. return true;
  17. else if(is_array($array[$bottom]))
  18. if(in_multiarray($elem, $array[$bottom]))
  19. return true;
  20.  
  21. $bottom++;
  22. }
  23. return false;
  24. }
  25.  
  26. if (isset($_REQUEST['dir']))
  27. {
  28. $dir=urldecode($_REQUEST['dir']);
  29. }
  30. else
  31. {
  32. $dir='';
  33. }
  34.  
  35. if (isset($_REQUEST['filter']))
  36. {
  37. $filter = $_REQUEST['filter'];
  38. $i = 0;
  39. foreach ($filter as $fil)
  40. {
  41. $f[$i] = explode('/', $fil);
  42. $i++;
  43. }
  44. }
  45. //sleep(1);
  46. //var_dump($f);
  47. if (file_exists($base.$dir))
  48. {
  49. $files = scandir($base.$dir);
  50. natcasesort($files);
  51. if (count($files) > 2)
  52. {
  53. /* The 2 accounts for . and .. */
  54. echo '<ul class="jqueryFolderTree" style="display: none;" >'; //style="display: none;"
  55. // All dirs
  56. foreach($files as $file)
  57. {
  58. if (!isset($filter))
  59. {
  60. if (file_exists($base.$dir.$file) && $file != '.' && $file != '..' && is_dir($base.$dir.$file))
  61. {
  62. if(check_for_subdirs($base.$dir.$file)==true)
  63. {
  64. echo '<li><a href="#" class="collapsed" rel="'.$dir.$file.'/"></a><a href="'.$dir.$file.'/" class="folder" onclick="pobierzPliki(\''.$dir.$file.'\');">'.$file.'</a></li>';
  65. }
  66. else
  67. {
  68. echo '<li><a href="#" class="nosubs" rel="'.$dir.$file.'/"></a><a href="'.$dir.$file.'/" class="folder" onclick="pobierzPliki(\''.$dir.$file.'\');">'.$file.'</a></li>';
  69. }
  70. }
  71. }
  72. else
  73. {
  74. if (file_exists($base.$dir.$file) && $file != '.' && $file != '..' && is_dir($base.$dir.$file) && in_multiarray($file, $f))
  75. {
  76. if(check_for_subdirs($base.$dir.$file)==true)
  77. {
  78. echo '<li><a href="#" class="collapsed" rel="'.$dir.$file.'/"></a><a href="'.$dir.$file.'/" class="folder" onclick="pobierzPliki(\''.$dir.$file.'\');">'.$file.'</a></li>';
  79. }
  80. else
  81. {
  82. echo '<li><a href="#" class="nosubs" rel="'.$dir.$file.'/"></a><a href="'.$dir.$file.'/" class="folder" onclick="pobierzPliki(\''.$dir.$file.'\');">'.$file.'</a></li>';
  83. }
  84. }
  85. }
  86. }
  87. echo '</ul>';
  88. }
  89. }
  90.  
  91. function check_for_subdirs($path)
  92. {
  93. $found = false;
  94. $items = scandir($path);
  95. foreach($items as $item)
  96. {
  97. if ($item != '.' && $item != '..' && is_dir($path.'/'.$item))
  98. {
  99. $found=true;
  100. break;
  101. }
  102. else
  103. {
  104. $found=false;
  105. }
  106. }
  107. return $found;
  108. }
  109.  
  110. ?>