Angular + jquery ui sortable

时间:2018-01-17 18:22:28

标签: angularjs jquery-ui angularjs-directive jquery-ui-sortable jquery-ui-autocomplete

请看这个小提琴https://jsfiddle.net/Lj0swckj/3/

我已经为sortable创建了指令 对于父div可以排序正常工作 对于孩子们我也想要同样的东西,也包含遏制作为其父母 在给定示例中,child1,child2,child3可以交换位置,包含将是parent1。

我尝试了很多但找不到任何东西

注意:还可以为孩子1,孩子2 ... (这里在这个例子中我添加了两个级别的数据,但最多可以有四个级别)

所以我想要一个解决方案,其中sortable可以用于这种类型的递归模板。

任何帮助都将不胜感激。

angular.module("myApp", []).
controller("myController", ['$scope', function($scope) {
    $scope.fields = [{
    		"name": "parent1",
        "level": 1,
        "children": [{
         		"name": "child1",
            "level": 2
        }, {
        		"name": "child2",
            "level": 2
        }, {
        		"name": "child3",
            "level": 2
        }]
    }, {
    		"name": "parent2",
        "level": 1
    }, {
    		"name": "parent3",
        "level": 1
    }];
    
    $scope.draggableItemsOption = {
    	"moverElement": ".moverBtn",
			"containment": ".draggable-field"
    }
}]).directive("myDragDrop", ['$timeout', function ($timeout) {

		return {
			restrict:'A',
			scope: {
				myDragDrop:"="
			},
			link: function ($scope, element, attrs) {
				var options = $scope.myDragDrop;
				var $container = element;
			
				
				var sortableOptions = {
        	 /* handle: options.moverElement, */
           containment: options.containment,
           delay: 0
				 };


				if($container.sortable){
					$container.sortable(sortableOptions);
				}
				
       }
       }
					
	}]);
.element {
   border: 1px solid #333;
   padding: 10px 10px;
   position: relative;
}

.element-class {
  margin-left: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.20/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
    
    <div class="draggable-field" my-drag-drop="draggableItemsOption">
        <div ng-repeat="data in fields">
            <div class="element" ng-class="{'element-class': data.level > 1}"> 
              {{data.name}}
              <div ng-repeat="childData in data.children">
                  <div class="element" ng-class="{'element-class': data.level > 1}"> 
                    {{childData.name}}
                   </div>
              </div>
           </div>
   			</div>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

嗯!终于有了解决方案。 实际上它非常简单

这里看看这个小提琴https://jsfiddle.net/Lj0swckj/6/  我所做的是创造了儿童元素

&#13;
&#13;
angular.module("myApp", []).
controller("myController", ['$scope', function($scope) {
    $scope.fields = [{
    		"name": "parent1",
        "level": 1,
        "children": [{
         		"name": "child1",
            "level": 2
        }, {
        		"name": "child2",
            "level": 2
        }, {
        		"name": "child3",
            "level": 2,
            "children": [{
            		"name": "grandChild1",
                "level": 3
            }, {
            		"name": "grandChild2",
                "level": 3
            }]
        }]
    }, {
    		"name": "parent2",
        "level": 1,
        "children": [{
        	"name": "child4",
          "level":2
        }]
    }, {
    		"name": "parent3",
        "level": 1
    }];
    
    $scope.draggableItemsOption = {
    	"moverElement": ".moverBtn",
			"containment": ".draggable-field"
    }
    
    $scope.childDraggableItemsOption = {
    		"containment": "parent"
    }
}]).directive("myDragDrop", ['$timeout', function ($timeout) {

		return {
			restrict:'A',
			scope: {
				myDragDrop:"="
			},
			link: function ($scope, element, attrs) {

				var options = $scope.myDragDrop;
				var $container = element;
			
				
				var sortableOptions = {
        	 /* handle: options.moverElement, */
           containment: options.containment,
           delay: 0
				 };


				if($container.sortable){
					$container.sortable(sortableOptions);
				}
				
       }
       }
					
	}]);
&#13;
.element {
   border: 1px solid #333;
   padding: 10px 10px;
   position: relative;
}

.element-class {
  margin-left: 20px;
}

.moverBtn {
  position: absolute;
  top: 2px;
  right: 10px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-autocomplete/1.0.7/jquery.auto-complete.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.7/angular.min.js"></script>

<div ng-app="myApp" ng-controller="myController">
    <script type="text/ng-template"  id="tree_item_renderer.html">
    <div class="child-draggable-field" my-drag-drop="childDraggableItemsOption">
    		<div ng-repeat="data in data.children" ng-class="{'element-class': data.level > 1}">
   			 	<div class="element"> {{data.name}} ------ {{$parent.$parent.data.children.length}}</div>
          <div ng-include="'tree_item_renderer.html'">
              </div>
    			</div>
    </div>
</script>
    <div class="draggable-field" my-drag-drop="draggableItemsOption">
        <div ng-repeat="data in fields">
            <div class="element" ng-class="{'element-class': data.level > 1}"> 
              {{data.name}}
              <div ng-include="'tree_item_renderer.html'">
              </div>
           </div>
   			</div>
    </div>
</div>
&#13;
&#13;
&#13;