I am trying to make simple drag and drop methods in my component and I can't seem to get this working. This is really the first time I have ever dealt with drag and drop type stuff so not sure if I am taking the right approach. Just adopting a tutorial from W3. Any suggestions? The id would be to simply get the id of the element being dragged and the id of the element dropped and append...
template:
<div id="opened" class="board-body" (ondrop)="drop($event)" (ondragover)="allowDrop($event)">
<div class="card bg-light-blue mb-2" id="drag1" draggable="true" (ondragstart)="drag($event)">
draggable card 1
</div>
</div>
<div id="responded" class="board-body" (ondrop)="drop($event)" (ondragover)="allowDrop($event)">
<div class="card bg-light-blue mb-2" id="drag2" draggable="true" (ondragstart)="drag($event)">
draggable card 2
</div>
</div>
component:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-funnel-grid',
templateUrl: './funnel-grid.component.html',
styleUrls: ['./funnel-grid.component.scss']
})
export class FunnelGridComponent implements OnInit {
constructor() { }
ngOnInit() {
}
allowDrop(e) {
e.preventDefault();
}
drag(e) {
e.dataTransfer.setData('text', e.target.id);
console.log(e.target.id);
}
drop(e) {
e.preventDefault();
const data = e.dataTransfer.getData('text');
e.target.appendChild(document.getElementById(data));
}
}
Start by importing DragDropModule into the NgModule where you want to use drag-and-drop features. You can now add the cdkDrag directive to elements to make them draggable. When outside of a cdkDropList element, draggable elements can be freely moved around the page.
To implement drag and drop list you have to include your ngFor repeated section inside a parent div to make that section draggable. This is just a basic structure of how the draggable section should look. Now we have to provide DndList directives to this structure. Basic functioning HTML for drag and drop list.
You just need to change the event names:
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