I want to know how to do drag and drop by using AngularJs.
This is what I have so far:
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
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
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
Modified from the angular-drag-and-drop-lists examples page
<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>
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
Using HTML 5 Drag and Drop
You can easily archive this using HTML 5 drag and drop feature along with angular directives.
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
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);"