13

我想为 jstree 中的节点实现移动功能。是需要执行的移动还是拖放?此外,拥有将容器绑定到事件和事件代码的工作代码会很好。

4

3 回答 3

19

如果您不需要执行任何移动规则(不允许将某些节点移动到其他节点等),您只需要使用 dnd 插件。如果您需要执行移动规则,您可以添加 crrm 插件。

有关此示例,请参阅dnd pluign 文档的仅重新排序演示。文档很差,因此您必须使用浏览器上的开发人员工具来查看check_move回调参数的属性是什么。对于文档中的示例,m.o指的是您拖动的节点并m.r指的是您的目标节点。

当节点被移动时,您可能还需要收到通知,因此move_node.jstree在初始化树时绑定到事件:

$("#treeHost").jstree({
   // ...
}).bind("move_node.jstree", function (e, data) {
    // data.rslt.o is a list of objects that were moved
    // Inspect data using your fav dev tools to see what the properties are
});
于 2012-05-07T23:30:06.567 回答
13
$("#demo1").jstree({
....
.bind("move_node.jstree", function (e, data) {

    /*
    requires crrm plugin

    .o - the node being moved
    .r - the reference node in the move
    .ot - the origin tree instance
    .rt - the reference tree instance
    .p - the position to move to (may be a string - "last", "first", etc)
    .cp - the calculated position to move to (always a number)
    .np - the new parent
    .oc - the original node (if there was a copy)
    .cy - boolen indicating if the move was a copy
    .cr - same as np, but if a root node is created this is -1
    .op - the former parent
    .or - the node that was previously in the position of the moved node */

    var nodeType = $(data.rslt.o).attr("rel");
    var parentType = $(data.rslt.np).attr("rel");

    if (nodeType && parentType) {
        // TODO!
    }
})
});
于 2014-01-25T04:43:22.107 回答
9

上述方法不适用于最新版本的 jstree(截至今天为 3.3.7)。

博金回答的第一行仍然成立。要实施规则,您可以使用core.check_callback或可能使用types插件;该crrm插件不再存在。绑定以move_node.jstree在完成移动(放下)时执行某些操作。默认情况下,除了移动节点之外,dnd 插件还允许重新排序(在两个节点之间放置)和复制(Ctrl + 拖动)。下面的代码片段显示了如何禁用这些附加行为。

$('#treeElement').jstree({
    'core': {
        'check_callback': CheckOperation,
        ...
    },
    'plugins': ['dnd']
})
.bind("move_node.jstree", function(e, data) {
    //data.node was dragged and dropped on data.parent
});

function CheckOperation(operation, node, parent, position, more) {
    if (operation == "move_node") {
        if (more && more.dnd && more.pos !== "i") { // disallow re-ordering
            return false;
        }
        ... more rules if needed ...
        else {
            return true;
        }
    }
    else if (operation == "copy_node") {
        return false;
    }
    return true;
}
于 2019-03-05T21:03:57.473 回答