dudeonthehorse
dudeonthehorse

Reputation: 57

how to deny delete/rename/move root(or other) node(s) in jsTree?

for example:

[{
"data": "reference",
"attr": {
    "id": "0"
},
"state": "open",
"children": [
    [{
        "data": "one",
        "attr": {
            "id": "1"
        },
        "state": "closed"
    }, {
        "data": "two",
        "attr": {
            "id": "2"
        }
    }]
]
}, {
"data": "recycle bin",
"attr": {
    "id": "bin"
},
"state": "closed",
"children": []
}]

i need to deny delete/move/rename "reference" & "recycle bin" nodes with "dnd", "crrm" and "context menu" plugins

Upvotes: 1

Views: 2776

Answers (2)

temuri
temuri

Reputation: 2807

You can capture node delete event and check for node's metadata:

.bind('delete_node.jstree', function (e, data) {
    // Check medatada, assuming that root's parent_id is NULL:
    if (data.rslt.obj.attr('parent_id') == null) {
       alert('Root folder is here to stay.');
       e.stopImmediatePropagation();
       return false;
    }
})

Upvotes: 1

Zheileman
Zheileman

Reputation: 2559

For avoiding moves using the crrm plugin you could do:

"crrm": {
   "move": {
      "check_move": function(m) { return (m.o[0].id !== "0" && m.o[0].id !== "bin"); }
   }
}

In summary, you need to return TRUE for allowing the move, or FALSE otherwise. So you check that the node's ID being moved is not the reference one, or the recycle bin one.

Please take a look at the jsTree documentation for accomplishing the other tasks, as everything you need is there. Don't be lazy :-)

Upvotes: 2

Related Questions