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:
function filtruj(val)
{
data = 'akcja=getPath&produktID='+ids[0];
$.post
(
'ajax/palety.ajax.php',
data,
function (path)
{
$('#directoryTree').html('');
$('#directoryTree').folderTree({root: 'zdjecia/', script: 'js/jquery_folder_tree/jquery.foldertree.php', loadMessage: 'Ładuję strukturę...', filter: paths});
}
)
}
Pełne źródło pluginu z moją mała modyfikacją:
(function($) {
$.fn.folderTree = function(o) {
if( !o ) var o = {};
if( o.root == undefined ) o.root = '/'; //e.g. /root/folder/
if( o.script == undefined ) o.script = 'jquery.foldertree.php';
if( o.loadMessage == undefined ) o.loadMessage = 'Loading...';
return this
.each(function() { //Builds the whole tree
function create_node
(dir, target
, fol
){ var toadd = '<ul class="jqueryFolderTree"><li><a class="home folder sel" href="'+o.root+'">Home</a></li></ul>';
if($(fol).hasClass("sel")){
$(fol).removeClass('folder').addClass('waitb');
}else{
$(fol).removeClass('folder').addClass('wait');
}
$
.post
(o
.script
, { dir: dir, filter
: o
.filter
}, function(data
) { $(fol).removeClass('wait waitb').addClass('folder');
if(dir == o
.root
){ //if is the root dir
data = toadd+data;
target.html(data);
target.find("ul.jqueryFolderTree").show();
}else{
target.append(data);
target.find("ul.jqueryFolderTree").css({'padding-left':'20px'}).show();
}
});
}
$(this).delegate(".collapsed", "click", function(e){
$(this).removeClass("collapsed").addClass("expanded");
var cur_li = $(this).closest("li");
var ul_to = cur_li.find("ul.jqueryFolderTree").first();
if(ul_to.length > 0){
ul_to.show();
}else{
create_node
($
(this
).attr
('rel'), cur_li
, $
(this
).next('li a.folder') ); }
});
$(this).delegate(".expanded", "click", function(e){
$(this).removeClass("expanded").addClass("collapsed");
var cur_li = $(this).closest("li");
var ul_to = cur_li.find("ul.jqueryFolderTree").first();
ul_to.hide();
});
$(this).delegate(".folder", "click", function(e){
$(".folder", $(this).attr("id")).removeClass("sel");
$(this).addClass("sel");
e.preventDefault();
});
$(this).html('<ul class="jqueryFolderTree"><li class="wait">' + o.loadMessage + '</li></ul>');
alert('Hello');
create_node(o.root, $(this));
});
}
})(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?(IMG:
style_emoticons/default/questionmark.gif) (IMG:
style_emoticons/default/questionmark.gif) ?
Chociaż w zasadzie kod php też może się przydać.
<?php
//-------------- CONFIG VARS ---------------------------------//
$basefolder = ''; //just the name
$base = '/srv/allegro/';
//var_dump($_POST);
//-------------- END FILE BROWSER CONFIG VARS-----------------//
function in_multiarray($elem, $array)
{
$bottom = 0;
while($bottom <= $top)
{
if($array[$bottom] == $elem)
return true;
if(in_multiarray($elem, $array[$bottom]))
return true;
$bottom++;
}
return false;
}
if (isset($_REQUEST['dir'])) {
}
else
{
$dir='';
}
if (isset($_REQUEST['filter'])) {
$filter = $_REQUEST['filter'];
$i = 0;
foreach ($filter as $fil)
{
$i++;
}
}
//sleep(1);
//var_dump($f);
{
$files = scandir($base.$dir);
{
/* The 2 accounts for . and .. */
echo '<ul class="jqueryFolderTree" style="display: none;" >'; //style="display: none;" // All dirs
foreach($files as $file)
{
{
if (file_exists($base.$dir.$file) && $file != '.' && $file != '..' && is_dir($base.$dir.$file)) {
if(check_for_subdirs($base.$dir.$file)==true)
{
echo '<li><a href="#" class="collapsed" rel="'.$dir.$file.'/"></a><a href="'.$dir.$file.'/" class="folder" onclick="pobierzPliki(\''.$dir.$file.'\');">'.$file.'</a></li>'; }
else
{
echo '<li><a href="#" class="nosubs" rel="'.$dir.$file.'/"></a><a href="'.$dir.$file.'/" class="folder" onclick="pobierzPliki(\''.$dir.$file.'\');">'.$file.'</a></li>'; }
}
}
else
{
if (file_exists($base.$dir.$file) && $file != '.' && $file != '..' && is_dir($base.$dir.$file) && in_multiarray
($file, $f)) {
if(check_for_subdirs($base.$dir.$file)==true)
{
echo '<li><a href="#" class="collapsed" rel="'.$dir.$file.'/"></a><a href="'.$dir.$file.'/" class="folder" onclick="pobierzPliki(\''.$dir.$file.'\');">'.$file.'</a></li>'; }
else
{
echo '<li><a href="#" class="nosubs" rel="'.$dir.$file.'/"></a><a href="'.$dir.$file.'/" class="folder" onclick="pobierzPliki(\''.$dir.$file.'\');">'.$file.'</a></li>'; }
}
}
}
}
}
function check_for_subdirs($path)
{
$found = false;
$items = scandir($path);
foreach($items as $item)
{
if ($item != '.' && $item != '..' && is_dir($path.'/'.$item)) {
$found=true;
break;
}
else
{
$found=false;
}
}
return $found;
}
?>
Ten post edytował busterek 18.11.2013, 12:03:36