代码之家  ›  专栏  ›  技术社区  ›  Sudarshana Dayananda

如何将拖放功能限制为角度ui树中的同级元素?

  •  1
  • Sudarshana Dayananda  · 技术社区  · 6 年前

    我曾经 this git存储库。

    HTML格式

    <div ui-tree="treeOptions" id="tree-root" data-drag-enabled="true">
        <ol ui-tree-nodes ng-model="nodes">
            <li ng-repeat="node in nodes"  
                ui-tree-node 
                data-type="top-level" 
                ng-include="'nodes_renderer.html'"></li>
        </ol>
    </div>
    

    我想将拖放功能限制为仅同级元素。

    JS公司

    $scope.treeOptions = {
    
        accept: function (sourceNodeScope, destNodesScope, destIndex) {
    
            if (sourceNodeScope.$parent.$id === destNodesScope.$parent.$id)
                return true;
            else
                return false;
    
        }
    }
    

    我在GitHub存储库中找不到关于这个需求的更多信息。

    1 回复  |  直到 6 年前
        1
  •  1
  •   james00794    6 年前

    您的逻辑基本上是正确的,但是您使用的回调并不正确。有角度的ui树有 beforeDrop 回调,允许您返回一个承诺,并根据结果,将接受或拒绝丢弃。这个 accept 回调实际上是在每次跨越另一个节点时拖动时调用的。下面是一个简单的示例实现:

    $scope.treeOptions = {
            beforeDrop : function (e) {
              if (e.source.nodesScope.$parent.$id === e.dest.nodesScope.$parent.$id) {
                console.log("siblings, this is allowed");
                return $q.resolve();
              }
              else {
                console.log("not siblings");
                window.alert("Not siblings! Reject this drop!");
                return $q.reject();
              }
            }
         };
    

    下面是一个简单的例子: http://plnkr.co/edit/6PD6PrD3wRduOjOQzuFN