本文介绍了如何回滚无法在jstree中移动的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to figure out how to rollback only a folder node that wasn't successfully moved. The code below is an example of what I'm trying to do. The problem comes when you have selected a couple of folders and moved them into another folder. If one of the directories fails to be moved I want to be able to roll it back to it's original parent.Unfortunately $.jstree.rollback(data.rlbk); rollsback all of the folders that were selected to their previous locations.

$("#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.
         }
    }
});

有没有办法做到这一点?

Is there a way for this to be done?

推荐答案

我之前已经看过使用jstree,但是在我的代码中没有使用它.结果,代码可能不正确,但概念应该正确.

I've looked at using jstree before, but haven't used it in my code. As a result, the code may not be correct, but the concepts should be.

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

Based on your code, it appears that you're performing the move operation on the server side and you want the tree to be updated to reflect the results.

基于jsTree文档,似乎您无法提交节点更新并回滚到上一次提交.

Based on the jsTree documentation, it looks as though you cannot commit node updates and roll back to the last commit.

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

Instead of rolling back only the changes that you don't want, you can roll back the tree (all changes) and perform the moves afterward.

为了更好地理解下面的代码,您可能希望阅读它(或创建一个副本),而无需在"if"语句的条件中设置或引用"wasTriggeredByCode"的行.

In order to better understand the code below, you may want to read it (or create a copy) without the lines where "wasTriggeredByCode" is set or referenced in the condition for an "if" statement.

$("#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');
    }
});

这篇关于如何回滚无法在jstree中移动的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:18