Drag and DroppDraggable and pDroppable directives apply drag-drop behaviors to any element.
Documentation
Import
import {DragDropModule} from 'primeng/dragdrop';
Getting Started
pDraggable and pDroppable are attached to a target element to add drag-drop behavior. The value of a Directive attribute is required and it defines the scope to match draggables with droppables. Droppable scope can also be an array to accept multiple droppables.
<div pDraggable="dd">Draggable Div</div>
<div pDroppable="dd">Droppable Div</div>
Drag Handle
Drag handle is used to restrict dragging unless mousedown occurs on the specified element. Panel below can only be dragged using its header.
<div pDraggable="pnl" dragHandle=".ui-panel-titlebar">
<p-panel header="Drag Header">
The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family,
kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
</p-panel>
</div>
Draggable
Attributes
Name | Type | Default | Description |
---|---|---|---|
dragEffect | string | null | Defines the cursor style, valid values are none, copy, move, link, copyMove, copyLink, linkMove and all. |
dragHandle | string | null | Selector to define the drag handle, by default anywhere on the target element is a drag handle to start dragging. |
pDraggableDisabled | boolean | false | Whether the element is draggable, useful for conditional cases. |
Events
Name | Parameters | Description |
---|---|---|
onDragStart | event: browser event | Callback to invoke when drag begins. |
onDrag | event: browser event | Callback to invoke on dragging. |
onDragEnd | event: browser event | Callback to invoke when drag ends. |
Droppable
Attributes
Name | Type | Default | Description |
---|---|---|---|
dropEffect | string | null | Defines the cursor style on drag over, valid values are copy, relocate, link and move. |
pDroppableDisabled | boolean | false | Whether the element is droppable, useful for conditional cases. |
Events
Name | Parameters | Description |
---|---|---|
onDragEnter | event: browser event | Callback to invoke when a draggable enters drop area. |
onDrop | event: browser event | Callback to invoke when a draggable is dropped onto drop area. |
onDragLeave | event: browser event | Callback to invoke when a draggable leave drop area. |
Dependencies
Native HTML5 DragDrop.
Source
<h3 class="first">Drag Only</h3>
<div pDraggable="pnl" dragHandle=".ui-panel-titlebar">
<p-panel header="Drag Header">
The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
Through Michael's life the nature of the family business becomes clear. The business of the family is just like the head of the family,
kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
</p-panel>
</div>
<h3>Drag and Drop to Table</h3>
<div class="ui-g">
<div class="ui-g-12 ui-md-6 ui-g-nopad drag-column">
<ul style="margin:0;padding:0">
<li *ngFor="let car of availableCars" class="ui-helper-clearfix" pDraggable="cars"
(onDragStart)="dragStart($event,car)" (onDragEnd)="dragEnd($event)">
<i class="fa fa-arrows fa-2x" style="float:right;margin-top:8px"></i>
<img src="assets/showcase/images/demo/car/{{car.brand}}.png" style="float:left" draggable="false">
<div style="margin:8px 0 0 8px;float:left">{{car.vin}} - {{car.year}}</div>
</li>
</ul>
</div>
<div class="ui-g-12 ui-md-6 drop-column" pDroppable="cars"
(onDrop)="drop($event)" [ngClass]="{'ui-highlight-car':draggedCar}">
<p-table [value]="selectedCars">
<ng-template pTemplate="header">
<tr>
<th>Vin</th>
<th>Year</th>
<th>Brand</th>
<th>Color</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-car>
<tr>
<td>{{car.vin}}</td>
<td>{{car.year}}</td>
<td>{{car.brand}}</td>
<td>{{car.color}}</td>
</tr>
</ng-template>
</p-table>
</div>
</div>
export class DragDropDemo {
availableCars: Car[];
selectedCars: Car[];
draggedCar: Car;
constructor(private carService: CarService) { }
ngOnInit() {
this.selectedCars = [];
this.carService.getCarsSmall().then(cars => this.availableCars = cars);
}
dragStart(event,car: Car) {
this.draggedCar = car;
}
drop(event) {
if(this.draggedCar) {
let draggedCarIndex = this.findIndex(this.draggedCar);
this.selectedCars = [...this.selectedCars, this.draggedCar];
this.availableCars = this.availableCars.filter((val,i) => i!=draggedCarIndex);
this.draggedCar = null;
}
}
dragEnd(event) {
this.draggedCar = null;
}
findIndex(car: Car) {
let index = -1;
for(let i = 0; i < this.availableCars.length; i++) {
if(car.vin === this.availableCars[i].vin) {
index = i;
break;
}
}
return index;
}
}