ngDraggable简单使用及实现拖拽排序

Posted gerry2019

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ngDraggable简单使用及实现拖拽排序相关的知识,希望对你有一定的参考价值。

  ngDraggable.js是一款比较简单实用的angularJS拖拽插件,借助于封装好的一些自定义指令,能够快速的进行一些拖拽应用开发。首先先介绍一些基本的概念;

  • ng-drop:是否允许放入拖拽元素
  • ng-drop-success($data, $event):拖拽元素放入的回调;$data:放入元素数据;$event拖拽事件对象
  • ng-drag:元素是否允许拖拽
  • ng-drag-success($data, $event):$data:拖拽元素数据,$event拖拽元素事件对象
  • ng-drag-data:拖拽数据

  官方链接:https://github.com/fatlinesofcode/ngDraggable

  在拖拽进行元素排序时,只需动态更改下循环数组元素顺序;具体事件绑定drop绑定在父元素,drag绑定在子元素即可,具体实现代码如下:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 
 4 <head>
 5     <meta charset="UTF-8">
 6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 7     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 8     <style>
 9         .drag-container {
10             display: flex;
11             justify-content: flex-start;
12             align-items: center;
13         }
14         
15         .drag-container .drag-box {
16             display: flex;
17             align-items: center;
18             justify-content: center;
19             width: 200px;
20             height: 200px;
21             margin: 20px;
22             color: #fff;
23             font-size: 20px;
24             font-weight: bold;
25         }
26         
27         .drag-container .drag-box .drag-item {
28             display: flex;
29             align-items: center;
30             justify-content: center;
31             width: 150px;
32             height: 150px;
33         }
34     </style>
35     <title>ng-dragger</title>
36 </head>
37 
38 <body ng-app="app" ng-controller="draggerController">
39     <div class="drag-container">
40         <div ng-repeat="item in dragArr" class="drag-box" ng-drop="true" ng-drop-success="onDropComplete($index, $data, $event)">
41             <div class="drag-item" ng-drag="true" ng-drag-data="{{item}}" ng-style="{background: item.color}">{{item.text}}</div>
42         </div>
43     </div>
44 
45     <script src="https://cdn.bootcss.com/angular.js/1.3.1/angular.js"></script>
46     <script src="https://cdn.bootcss.com/ngDraggable/0.1.11/ngDraggable.js"></script>
47     <script>
48         var myApp = angular.module(‘app‘, [‘ngDraggable‘]);
49         myApp.controller(‘draggerController‘, function($scope) {
50             $scope.dragArr = [{
51                 text: ‘方块1‘,
52                 color: ‘green‘,
53                 key: ‘arr1‘
54             }, {
55                 text: ‘方块2‘,
56                 color: ‘blue‘,
57                 key: ‘arr1‘
58             }, {
59                 text: ‘方块3‘,
60                 color: ‘#888‘,
61                 key: ‘arr1‘
62             }, {
63                 text: ‘方块4‘,
64                 color: ‘#888‘,
65                 key: ‘arr2‘
66             }, {
67                 text: ‘方块5‘,
68                 color: ‘#888‘,
69                 key: ‘arr2‘
70             }, {
71                 text: ‘方块6‘,
72                 color: ‘#888‘,
73                 key: ‘arr2‘
74             }];
75 
76             $scope.onDropComplete = function(index, obj, $event) {
77                 var otherObj = $scope.dragArr[index];
78                 var otherIndex = $scope.dragArr.findIndex(vv => Object.is(vv.text, obj.text));
79                 if (otherObj.key !== obj.key) return;
80                 $scope.dragArr[index] = obj;
81                 $scope.dragArr[otherIndex] = otherObj;
82             }
83 
84         })
85     </script>
86 </body>
87 
88 </html>

  效果图如下:技术图片

 

以上是关于ngDraggable简单使用及实现拖拽排序的主要内容,如果未能解决你的问题,请参考以下文章

element ui table中使用拖拽排序组件sortablejs及注意事项

怎么简单实现菜单拖拽排序的功能

怎么简单实现菜单拖拽排序的功能

怎么简单实现菜单拖拽排序的功能

ztree使用系列四(ztree实现同级拖拽排序并将排序结果保存数据库)

基于H5实现的react拖拽排序组件