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
Respuesta
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);
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);
¡Funcionó como un amuleto! ¡Gracias! Por cierto, creo que el uso debería ser '$ (" # tree ")' - a tu código le falta el '#'. – SNag
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? –
Exactamente lo que necesito, una pena que puedo votar una sola vez :) – mibutec
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');
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;
};
- 1. dynatree - ¿Cómo desplazarse al nodo activo?
- 2. Dynatree: ¿Dónde puedo almacenar información adicional en cada nodo?
- 3. jquery selectores: búsqueda de un elemento secundario del nodo raíz
- 4. Búsqueda de nodos por su nombre en XmlDocument
- 5. cómo recargar/actualizar/reiniciar DynaTree?
- 6. ¿Cómo obtener el nombre de nodo primario del nodo actual?
- 7. jQuery obtener nodo DOM?
- 8. jQuery obtener elementos por nombre
- 9. variables de shell de búsqueda por nombre, indirectamente
- 10. Obtener nombre de nodo con minidom
- 11. jQuery función de llamada por su nombre
- 12. Búsqueda de SQL Server para una columna por nombre
- 13. Cómo hacer que los hipervínculos en el plugin dynaTree jQuery puedan hacer clic?
- 14. nodo de títere nombre de host
- 15. Búsqueda incremental de JQuery
- 16. primer nombre de nodo secundario en xslt
- 17. Obtener nodo hijo de otro nodo, teniendo en cuenta el nombre del nodo
- 18. nodo ancestro más cercano en jQuery
- 19. Cómo cambiar el nombre del nodo XML
- 20. cómo obtener elementos por nombre usando jquery?
- 21. XSL por nodo
- 22. Búsqueda recursiva de un nodo en un árbol no binario
- 23. Magento - cambiar Ordenar el nombre por el nombre en el resultado de la búsqueda
- 24. XmlSerializer reemplazar xsi: type al nodo nombre
- 25. Nodo dividido de Rangy (JS/jQuery)
- 26. PHP DOM XPath - búsqueda de nombre de la clase
- 27. búsqueda de nombre de ruta en Linux?
- 28. Buscar nodo TreeView por valor
- 29. herencia privada: nombre del error de búsqueda
- 30. Consulta de XPath para nodo XML con dos puntos en el nombre de nodo
remitir este http://www.designchemical.com/blog/index.php/jquery/live-text- search-function-using-jquery/ Dynatree es un simple UL> LI – Rahul