jstree中如何回滚无法移动的节点

2024-02-18

我试图弄清楚如何仅回滚未成功移动的文件夹节点。下面的代码是我正在尝试做的事情的示例。当您选择了几个文件夹并将它们移动到另一个文件夹中时,就会出现问题。如果其中一个目录无法移动,我希望能够将其回滚到其原始父目录。 很遗憾$.jstree.rollback(data.rlbk);将所有选择的文件夹回滚到之前的位置。

$("#tree").jstree({...}).bind("move_node.jstree", function (e, data) {
    // process all selected nodes directory
    data.rslt.o.each(function (i) {
         // Send request.
         var move = $.parseJSON($.ajax({
             url: "./jstree.php",
             type: 'post',
             async: false,
             data: {
                 operation:  "move_dir",
                 ....
             }
         }).responseText);
         // When everything's ok, the reponseText will be {success: true}
         // In all other cases it won't exist at all.
         if(move.success == undefined){
             // Here I want to rollback the CURRENT failed node.
             // $.jstree.rollback(data.rlbk); will rollback all 
             // of the directories that have been moved.
         }
    }
});

有办法做到这一点吗?


我以前看过使用 jstree,但没有在我的代码中使用它。因此,代码可能不正确,但概念应该正确。

根据您的代码,您似乎正在服务器端执行移动操作,并且希望更新树以反映结果。

根据 jsTree 文档,您似乎无法提交节点更新并回滚到上次提交。

您可以回滚树(所有更改)并随后执行移动,而不是仅回滚您不需要的更改。

为了更好地理解下面的代码,您可能需要阅读它(或创建副本),而不需要在“if”语句的条件中设置或引用“wasTriggeredByCode”的行。

$("#tree").jstree({...}).bind("move_node.jstree", function (e, data) {

    var jsTree = $(this);
    var successes = [];

    // Becomes true when function was triggered by code that updates jsTree to
    //  reflect nodes that were successfully moved on the server
    var wasTriggeredByCode = false;

    // process all selected nodes directory
    data.rslt.o.each(function (i) {

         // I'm not certain that this is how the node is referenced
         var node = $(this);

         wasTriggeredByCode = (wasTriggeredByCode || node.data('redoing'));

         // Don't perform server changes when event was triggered from code
         if (wasTriggeredByCode) {
             return;
         }

         // Send request.
         var move = $.parseJSON($.ajax({
             url: "./jstree.php",
             type: 'post',
             async: false,
             data: {
                 operation:  "move_dir",
                 ....
             }
         }).responseText);

         if(move.success){
             successes.push(node);
         }
    });

    // Don't continue when event was triggered from code
    if (wasTriggeredByCode) {
         return;
    }

    // Roll back the tree here
    jsTree.rollback(data.rlbk);

    // Move the nodes
    for (var i=0; i < successes.length; i++) {
        var node = successes[i];

        // According to the documentation this will trigger the move event,
        //  which will result in infinite recursion. To avoid this you'll need
        //  to set a flag or indicate that you're redoing the move.
        node.data('redoing', true);

        jsTree.move_node(node, ...);

        // Remove the flag so that additional moves aren't ignored
        node.removeData('redoing');
    }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jstree中如何回滚无法移动的节点 的相关文章