欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

AngularJS之拖拽排序(ngDraggable.js)

程序员文章站 2022-04-02 23:09:13
ngDraggable.js是一款比较简单实用的angularJS拖拽插件,借助于封装好的一些自定义指令,能够快速的进行一些拖拽应用开发。首先先介绍一些基本的概念; ng-drop:是否允许放入拖拽元素 ng-drop-success($data, $event):拖拽元素放入的回调;$data:放 ......

  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>

  效果图如下:AngularJS之拖拽排序(ngDraggable.js)