How to Create simple drag and Drop in angularjs

后端 未结 8 1461
孤独总比滥情好
孤独总比滥情好 2020-11-29 16:55

I want to know how to do drag and drop by using AngularJs.

This is what I have so far:



        
相关标签:
8条回答
  • 2020-11-29 17:14

    http://blog.parkji.co.uk/2013/08/11/native-drag-and-drop-in-angularjs.html This is simple method for creating native draggable angularJS elements

    0 讨论(0)
  • 2020-11-29 17:18

    Angular doesn't provide snazzy UI elements like drag and drop. That's not really Angular's purpose. However, there are a few well known directives that provide drag and drop. Here are two that I've used.

    https://github.com/angular-ui/ui-sortable

    https://github.com/codef0rmer/angular-dragdrop

    0 讨论(0)
  • 2020-11-29 17:20

    small scripts for drag and drop by angular

    (function(angular) {
      'use strict';
    angular.module('drag', []).
      directive('draggable', function($document) {
        return function(scope, element, attr) {
          var startX = 0, startY = 0, x = 0, y = 0;
          element.css({
           position: 'relative',
           border: '1px solid red',
           backgroundColor: 'lightgrey',
           cursor: 'pointer',
           display: 'block',
           width: '65px'
          });
          element.on('mousedown', function(event) {
            // Prevent default dragging of selected content
            event.preventDefault();
            startX = event.screenX - x;
            startY = event.screenY - y;
            $document.on('mousemove', mousemove);
            $document.on('mouseup', mouseup);
          });
    
          function mousemove(event) {
            y = event.screenY - startY;
            x = event.screenX - startX;
            element.css({
              top: y + 'px',
              left:  x + 'px'
            });
          }
    
          function mouseup() {
            $document.off('mousemove', mousemove);
            $document.off('mouseup', mouseup);
          }
        };
      });
    })(window.angular);
    

    source link

    0 讨论(0)
  • 2020-11-29 17:24

    Modified from the angular-drag-and-drop-lists examples page

    Markup

    <div class="row">
        <div ng-repeat="(listName, list) in models.lists" class="col-md-6">
            <ul dnd-list="list">
                <li ng-repeat="item in list" 
                    dnd-draggable="item" 
                    dnd-moved="list.splice($index, 1)" 
                    dnd-effect-allowed="move" 
                    dnd-selected="models.selected = item" 
                    ng-class="{'selected': models.selected === item}" 
                    draggable="true">{{item.label}}</li>
            </ul>
        </div>
    </div>
    

    Angular

    var app = angular.module('angular-starter', [
        'ui.router',
        'dndLists'
    ]);
    
    app.controller('MainCtrl', function($scope){
    
        $scope.models = {
            selected: null,
            lists: {"A": [], "B": []}
        };
    
        // Generate initial model
        for (var i = 1; i <= 3; ++i) {
            $scope.models.lists.A.push({label: "Item A" + i});
            $scope.models.lists.B.push({label: "Item B" + i});
        }
    
        // Model to JSON for demo purpose
        $scope.$watch('models', function(model) {
            $scope.modelAsJson = angular.toJson(model, true);
        }, true);
    });
    

    Library can be installed via bower or npm: angular-drag-and-drop-lists

    0 讨论(0)
  • 2020-11-29 17:26

    Using HTML 5 Drag and Drop
    You can easily archive this using HTML 5 drag and drop feature along with angular directives.

    1. Enable drag by setting draggable = true.
    2. Add directives for parent container and child items.
    3. Override drag and drop functions - 'ondragstart' for parent and 'ondrop' for child.

    Find the example below in which list is array of items.
    HTML code:

        <div class="item_content" ng-repeat="item in list" draggrble-container>
            <div class="item" draggable-item draggable="true">{{item}}</div>
        </div>
    

    Javascript:

        module.directive("draggableItem",function(){
         return {
          link:function(scope,elem,attr){
            elem[0].ondragstart = function(event){
                scope.$parent.selectedItem = scope.item;
            };
          }
         };
        });
    
    
        module.directive("draggrbleContainer",function(){
         return {
            link:function(scope,elem,attr){
                elem[0].ondrop = function(event){
                    event.preventDefault();
                    let selectedIndex = scope.list.indexOf(scope.$parent.selectedItem);
                    let newPosition = scope.list.indexOf(scope.item);
                    scope.$parent.list.splice(selectedIndex,1);
                    scope.$parent.list.splice(newPosition,0,scope.$parent.selectedItem);
                    scope.$apply();
                };
                elem[0].ondragover = function(event){
                    event.preventDefault();
    
                };
            }
         };
        });
    

    Find the complete code here https://github.com/raghavendrarai/SimpleDragAndDrop

    0 讨论(0)
  • 2020-11-29 17:27

    adapt-strap has very light weight module for this. here is the fiddle. Here are some attributes that are supported. There are more.

    ad-drag="true"
    ad-drag-data="car"
    ad-drag-begin="onDragStart($data, $dragElement, $event);"
    ad-drag-end="onDataEnd($data, $dragElement, $event);"
    
    0 讨论(0)
提交回复
热议问题