EasyUI Forum
April 18, 2024, 03:36:03 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News:
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: [SOLVED] Find Tree Node by Name  (Read 8083 times)
don
Newbie
*
Posts: 28


View Profile
« on: July 10, 2018, 09:56:08 AM »

I'm using a simple list (ul) for the nodes of my tree. Can someone tell me how to search or find a specific node using only the list item's text? Ultimately I want to check/uncheck a checkbox node using code. So for example, if I had a checkbox tree that listed colors, I could (using code) check nodes 'red' and 'yellow'.  Huh
« Last Edit: July 11, 2018, 09:22:25 AM by don » Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #1 on: July 11, 2018, 01:28:33 AM »

The code below shows how to find a node by 'text' and check it.
Code:
var t = $('#tt');
var mynode = null;
var roots = t.tree('getRoots');
$.easyui.forEach(roots, true, function(node){
    if (node.text == 'Java'){
        mynode = node;
        return false;
    }
});
if (mynode){
    var el = $('#'+mynode.domId);
    t.tree('check', el[0])
}
Logged
don
Newbie
*
Posts: 28


View Profile
« Reply #2 on: July 11, 2018, 07:01:16 AM »

[SOLVED] Thanks stworthy! For others and future reference, here is a function that returns a tree node's element using the node's text:
Code:
/**
*Retrieve tree node element using node list item <li> text
 *
 * @param {easyUI.Tree} tree easyUI tree object
 * @param {string} nodeText list item <li> text of desired node
 *
 * @return {element} the tree node <li> element
 */
function getNodeByText(tree, nodeText) {
var mynode = null;
var roots = tree.tree('getRoots');
$.easyui.forEach(roots, true, function (node) {
if (node.text == nodeText) {
mynode = node;
return false;
}
});
return $('#' + mynode.domId);
};
Logged
officecode
Jr. Member
**
Posts: 69


View Profile Email
« Reply #3 on: October 17, 2018, 08:30:24 PM »

This feature is great.
Can the official consider adding a findNode method to the tree component? Just like the findItem method in menu, let everyone find the tree node based on the text content.
Because the getNode and getData methods in the tree are too inconvenient to use!
For reference only, thank you!
Logged
stworthy
Administrator
Hero Member
*****
Posts: 3581


View Profile Email
« Reply #4 on: October 18, 2018, 06:07:48 PM »

The 'find' method will be enhanced in next version.
Code examples:
Code:
var node1 = $('#tt').tree('find', 122);  // find by id
var node2 = $('#tt').tree('find', {text:'Java'});  // find by text
var node3 = $('#tt').tree('find', function(node){
  if (node.id > 100){
    return true;
  }
});  // find by custom function
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Valid XHTML 1.0! Valid CSS!