2012-09-05 18 views
6

Me gustaría comenzar a usar Dynatree en mi página, sin embargo, probablemente necesite buscar mi árbol por nombre. ¿Sabes quizás cómo hacer esto?JQuery Dynatree - nodo de búsqueda por nombre

+0

remitir este http://www.designchemical.com/blog/index.php/jquery/live-text- search-function-using-jquery/ Dynatree es un simple UL> LI – Rahul

Respuesta

1

Actualmente no existe una función de búsqueda, pero se puede usar algo como esto (no probado)

var match = null; 
tree.visit(function(node){ 
    if(node.data.title === "foo"){ 
     match = node; 
     return false; // stop traversal (if we are only interested in first match) 
    } 
}); 
alert("Found " + match); 
+0

Agregue una búsqueda probada y funcional a su próxima versión :) – Elisabeth

+0

Tiene un error en su código. debes verificar si se encontró una coincidencia. si no hay coincidencia en el árbol, está mostrando 'Found null' – AaA

15

necesitaba tener nodos no sólo a juego, sino también los caminos enteros a estos nodos. Escribí esta funcionalidad y funciona para mí.

Modificaciones para la biblioteca:

var clear = true; 

DynaTreeNode.prototype.search = function(pattern){ 

    if(pattern.length < 1 && !clear){ 
     clear = true; 
     this.visit(function(node){ 
      node.expand(true); 
      node.li.hidden = false; 
      node.expand(false); 
     }); 
    } else if (pattern.length >= 1) { 
     clear = false; 
     this.visit(function(node){ 
      node.expand(true); 
      node.li.hidden = false; 
     }); 

     for (var i = 0; i < this.childList.length; i++){ 
      var hide = {hide: false}; 
      this.childList[i]._searchNode(pattern, hide); 
     } 
    } 
}, 

DynaTreeNode.prototype._searchNode = function(pattern, hide){ 

    if (this.childList){ 
     // parent node 

     var hideNode = true; 
     for(var i = 0; i < this.childList.length; i++){ 
      var hideChild = {hide: false}; 
      this.childList[i]._searchNode(pattern, hideChild); 
      hideNode = hideNode && hideChild.hide; 
     } 
     if(hideNode && !this._isRightWithPattern(pattern)){ 
      this._hideNode(); 
      hide.hide = true; 
     } else { 
      hide.hide = false; 
     } 

    } else { 
     // leaf 
     if (!this._isRightWithPattern(pattern)){ 
      this._hideNode(); 
      hide.hide = true; 
     } else { 
      hide.hide = false; 
     } 
    } 
}, 

DynaTreeNode.prototype._isRightWithPattern = function(pattern){ 
    if((this.data.title.toLowerCase()).indexOf(pattern.toLowerCase()) >= 0){ 
     return true; 
    } 
    return false; 
}, 

DynaTreeNode.prototype._hideNode = function(){ 
    if(this.li) { 
     this.li.hidden = true; 
    } 
} 

Uso:

$("tree").dynatree("getRoot").search(pattern); 
+0

¡Funcionó como un amuleto! ¡Gracias! Por cierto, creo que el uso debería ser '$ (" # tree ")' - a tu código le falta el '#'. – SNag

+3

Estoy tratando de implementar esto, pero no estoy seguro de cómo hacer esto. Por favor, aclara por mí! No sé dónde puse este código. ¿Y qué es el patrón? el texto para buscar? –

+0

Exactamente lo que necesito, una pena que puedo votar una sola vez :) – mibutec

0

lo he hecho de esta manera

<style> 
span.occurance a.dynatree-title{background-color:#3AFF22;} 
</style> 


DynaTreeNode.prototype.find = function (needle) { 
    needle = (needle || ''); 
    if (needle.length >= 1) { 
     var occurs = []; 
     this.visit(function (node) { 
      $(node.span).removeClass('occurance'); //remove pervious findings 
      if (node.data.title.indexOf(needle) != -1) { 
       occurs.push(node); 
       node._expandPath(); 
      } 
     }); 
     for (indx in occurs) { // mark findings 
      $(occurs[indx].span).addClass('occurance'); 
     } 
    } else { 
     $('span.dynatree-node.occurance').removeClass('occurance'); 
    } 
}, 
DynaTreeNode.prototype._expandPath = function() { 
    var path = [], 
     node = this; 
    while (node = node.getParent()) { 
     path.push(node); 
    } 
    for (indx in path) { 
     path[indx].expand(true) 
    } 
} 

uso:

[your selector].dynatree("getRoot").find('needle'); 
0

Gracias a @ Mar10 hice una pequeña función, simple para buscar un nodo con el título:

// If searchFrom is null, root is used 
function seachFolderNodeWithName(name, searchFrom) { 
    if (name == null) { 
     return undefined; 
    } 

    if (searchFrom == null) { 
     searchFrom = jQuery('#tree').dynatree("getRoot"); 
    } 

    var match = undefined; 

    searchFrom.visit(function (node) { 
     if (node.data.title === name) { 
      match = node; 
      return false; // Break if found 
     } 
    }); 
    return match; 
}; 
Cuestiones relacionadas