HRI
HRI

Reputation: 91

how can i check if a node exists in jstree

the scenario is the following: i have two jstrees, the first tree contains nodes that i need to move some of them to the second jstree. so far i have managed to move them but with no rules. But what i need to do is: 1. to let only one node to be the root of the whole tree 2. to check if the node already exists in the tree not to allow the move, but i can't figure out how to do this. this is the code of the second tree (the one to move to):

     $('#SecondTree').jstree({
            "json_data": {
                "data": data
            },
            "themes": {
                "theme": "default",
                "dots": false,
                "icons": false
            },
            "dnd": {

            },
            "crrm": {
                "move": {
                    "always_copy": "multitree"
                  }
              },

            "plugins": ["json_data", "themes", "ui", "dnd", "crrm"]

        }).bind("move_node.jstree", function (e, data) {
            if (data.rslt.r.attr("id") == data.rslt.o.attr("id")) {
                     return false;
             }


        });
    }

thank you in advanced

Upvotes: 1

Views: 3258

Answers (1)

Zheileman
Zheileman

Reputation: 2559

Not sure if I've understood the whole of your question, but, in particular, I can help you with this part:

"[...] to check if the node already exists in the tree not to allow the move"

You must use, as you already were trying, the crrm plugin. I've just answered a pretty similar question, with an example, maybe you'll want to take a look at it as well.

In your case, as you need to check for a node's existence (or not) in another tree, you can do something like this:

"crrm": {
   "move": {
      "check_move": function(m) { 
         return (m.ot === m.rt) || !m.rt.get_container().find("li[id="+m.o[0].id+"]").length;
      }
   }
}

First we check if we are moving the node inside the same tree, and always allow this case (to reorder nodes or whatever). Change it if you don't need it.

Then we check if the node exists:
m.o is the node being moved. You will seek for it in the tree where you're dropping the node.

m.rt.get_container() will return a jQuery DOM structure, so we just need to use .find() and it will return an array of <li> elements found by ID, being empty in the case nothing was found.

If we return TRUE (we are moving around in the same tree, or the array is empty, that is, the node was not found in the second tree), the movement is allowed, otherwise it's disallowed.

I hope it helps!

Upvotes: 1

Related Questions