将函数附加到before.jstree

时间:2016-04-15 12:18:53

标签: javascript jquery treeview jstree

我正在尝试阻止删除jstree中的根节点。在早期版本中有before.jstree事件,你可以这样做:

.on("before.jstree", function (e, data) {
    if(data.func === "delete_node") {
        if (data.node.type == "root_node" || true)
        {
            e.stopImmediatePropagation();
            return false;
        }
    }
})

但是由于在jstree 3(https://www.jstree.com/api/)中切割了before.jstree方法,因此这种方法不再适用。有没有操作contextmenu事件的替代解决方案?

1 个答案:

答案 0 :(得分:1)

您可以使用jstree.core.check_callback属性来实现此目的。此属性在每个jstree事件触发器之前计算,并且它需要一个布尔值 - true继续调用事件处理程序并false阻止它。
您可以为此属性附加回调函数,并根据触发的事件做出决策。在您的情况下,如果触发的事件是delete_node且事件目标节点是根,则可以返回false来阻止该事件。

$('#tree').jstree({
    'core' : {
        'check_callback' : function (operation, node, node_parent, node_position, more) {
            if ((operation == "delete_node" && node.type && node.type == "root") {
                return false;
            }
            return true;
        }
    }
});
相关问题