Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple drag and drop in angular ondrop, ondragover, ondragstart

Tags:

angular

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));
    }

}
like image 661
Sandra Willford Avatar asked Feb 06 '18 05:02

Sandra Willford


People also ask

How to do drag and drop in angular?

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.

How do you drag and drop in angular 6?

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.


1 Answers

You just need to change the event names:

  • (ondrop) to (drop)
  • (ondragover) to (dragover)
  • (ondragstart) to(dragstart)
like image 162
Ryan T Avatar answered Sep 21 '22 04:09

Ryan T