I would like to implement a drag and drop using Angular 2. I have some items:
<div class="item"></div>
which I would like to be able to drag and drop in a container:
<div class="container"></div>
I can not find any good source of information for doing this in Angular 2. I found this file: https://github.com/AngularClass/angular2-examples/blob/master/rx-draggable/directives/draggable.ts which I tried but I could not get it to work, I am also not entirely sure on how it should work.
How do I implement it?
I would recommend using Ng2-Dragula.
it is the angular2 dependency which provides drag n drop functionality to your application easily.
All you need to do is to install this dependency through npm.
npm install ng2-dragula dragula --save
add includes inside index.html and configure system as
<script src="/node_modules/ng2-dragula/bundles/ng2-dragula.js"></script>
<link href="/node_modules/ng2-dragula/src/public/css/dragula.min.css" rel='stylesheet' type='text/css'>
<script>
System.config({
paths:{
'dragula' : '../node_modules/dragula/dist/dragula.min.js'
},
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
import it inside the component where you want to use drag n drop and you are good to go.
@Component({
selector: 'sample',
directives: [Dragula],
viewProviders: [DragulaService],
template:`
<div>
<div class='wrapper'>
<div class='container' [dragula]='"first-bag"'>
<div>You can move these elements between these two containers</div>
<div>Moving them anywhere else isn't quite possible</div>
<div>There's also the possibility of moving elements around in the same container, changing their position</div>
</div>
<div class='container' [dragula]='"first-bag"'>
<div>This is the default use case. You only need to specify the containers you want to use</div>
<div>More interactive use cases lie ahead</div>
<div>Make sure to check out the <a href='https://github.com/bevacqua/dragula#readme'>documentation on GitHub!</a></div>
</div>
</div>
</div>
`
})
class Sample {}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With