Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html ng-app='DemoDragAndDrop'>

<head>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js"></script>
  <meta charset=utf-8 />
  <title>Drag and Drop Con Angular</title>
</head>

<body ng-controller='DemoCtrl' ng-cloack>
  <div class="row">
    <div class="col-sm-8">
      <h5>Arrastre los elementos disponibles hacia la siguiente línea:</h5>
      <img id="{{el.id}}" class="draggable" ng-repeat="el in resultado" droppable-area src="{{el.avatar}}" draggable-thing />
      <div class="col-sm-4">
        <h5>Elementos Disponibles</h5>
        <div id="{{el.id}}" class="draggable" ng-repeat="el in disponible" droppable-area draggable="true" draggable-thing>
          <img id="{{el.id}}" src="{{el.avatar}}" />
        </div>
      </div>
    </div>
  </div>
  <hr>
  <h3>Lo interesante de esto...</h3>
  <p>
    El objetivo de agregar angular a la funcionalidad de <b>Drag and Drop</b> no es solamente que las imagenes se carguen dinámicamente a través de los arreglos <b>$scope.disponible</b> y <b>$scope.resultado</b>.
  </p>
  <p>
    Los arreglos <b>$scope.disponible</b> y <b>$scope.resultado</b> siempre reflejarán los cambios que se realicen arrastrando las imagenes de una posición a otra.
  </p>
  <p>
    Esto nos permite recuperar los cambios que el usuario haya realizado y enviarlos a otro servidor remoto para procesarlos (puede ser que se guarde la información a una base de datos).
  </p>
  <p>
    Para poder ver esto utiliza los siguientes botones (el resultado se verá mejor en la consola que en los alert):
    <input type="button" value="Ver Disponibles" ng-click='verDisponibles()' />
    <input type="button" value="Ver Resultado" ng-click='verResultado()' />
  </p>
</body>

</html>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
  font-family: Verdana;
  text-align: center;
}

body {
  background-color: #F2F2F2;
}

.draggable {
  width: 75px;
  height: 75px;
  display: inline-block;
  border: 1px solid #aaaaaa;
  padding: 5px;
  background-repeat: no-repeat;
  background-position: center;
  cursor: pointer;
  margin: 5px;
}

pre {
  font-family: "Courier New" Courier monospace;
  width: 100%;
}
              
            
!

JS

              
                'use strict';

var app = angular.module('DemoDragAndDrop', []);

app.controller('DemoCtrl', ['$scope', function($scope) {
  $scope.disponible = [{
    id: 'disponible_0',
    avatar: 'https://cdn4.iconfinder.com/data/icons/imoticons/105/imoticon_3-64.png'
  }, {
    id: 'disponible_1',
    avatar: 'https://cdn4.iconfinder.com/data/icons/imoticons/105/imoticon_6-64.png'
  }, {
    id: 'disponible_2',
    avatar: 'https://cdn4.iconfinder.com/data/icons/imoticons/105/imoticon_4-64.png'
  }];

  $scope.resultado = [{
    id: 'resultado_0',
    avatar: 'https://cdn0.iconfinder.com/data/icons/TWG_Retina_Icons/64/cancel.png'
  }, {
    id: 'resultado_1',
    avatar: 'https://cdn0.iconfinder.com/data/icons/TWG_Retina_Icons/64/cancel.png'
  }, {
    id: 'resultado_2',
    avatar: 'https://cdn0.iconfinder.com/data/icons/TWG_Retina_Icons/64/cancel.png'
  }, {
    id: 'resultado_3',
    avatar: 'https://cdn0.iconfinder.com/data/icons/TWG_Retina_Icons/64/cancel.png'
  }, {
    id: 'resultado_4',
    avatar: 'https://cdn0.iconfinder.com/data/icons/TWG_Retina_Icons/64/cancel.png'
  }, {
    id: 'resultado_5',
    avatar: 'https://cdn0.iconfinder.com/data/icons/TWG_Retina_Icons/64/cancel.png'
  }];

  $scope.mover = function(origin, dest) {
    var origin$name = origin.split('_')[0];
    var origin$index = origin.split('_')[1];

    var dest$name = dest.split('_')[0];
    var dest$index = dest.split('_')[1];

    var bkDestino = $scope[dest$name][dest$index].avatar;
    $scope.$apply(function() {
      $scope[dest$name][dest$index].avatar = $scope[origin$name][origin$index].avatar;
      $scope[origin$name][origin$index].avatar = bkDestino;
    });
  };
  
  $scope.verDisponibles = function() {
    console.log($scope.disponible);
    alert(JSON.stringify($scope.disponible));
  };
  
  $scope.verResultado = function() {
    console.log($scope.resultado);
    alert(JSON.stringify($scope.resultado));
  };
}]);

app.directive('draggableThing', function() {
  return {
    restrict: 'A',
    link: function(scope, elem, attr, ctrl) {
      elem.bind('dragstart', function(e) {
        event.dataTransfer.setData("text", event.target.id);
      });
    }
  };
});

app.directive('droppableArea', function() {
  return {
    restrict: 'A',
    link: function(scope, elem, attr, ctrl) {
      elem.bind('drop', function(e, ui) {
        e.preventDefault();
        var origin = event.dataTransfer.getData("text");
        var dest = attr['id'];
        scope.mover(origin, dest);
      });
      elem.bind('dragover', function(e) {
        e.preventDefault();
      });
    }
  };
});
              
            
!
999px

Console