Drag and DroppDraggable and pDroppable directives apply drag-drop behaviors to any element.

Drag&Drop - 图1

Documentation

Import

  1. import {DragDropModule} from 'primeng/dragdrop';
  2.  

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.

  1. <div pDraggable="dd">Draggable Div</div>
  2. <div pDroppable="dd">Droppable Div</div>
  3.  

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.

  1. <div pDraggable="pnl" dragHandle=".ui-panel-titlebar">
  2. <p-panel header="Drag Header">
  3. The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
  4. His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
  5. 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,
  6. kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
  7. </p-panel>
  8. </div>
  9.  

Draggable

Attributes

NameTypeDefaultDescription
dragEffectstringnullDefines the cursor style, valid values are none, copy, move, link, copyMove, copyLink, linkMove and all.
dragHandlestringnullSelector to define the drag handle, by default anywhere on the target element is a drag handle to start dragging.
pDraggableDisabledbooleanfalseWhether the element is draggable, useful for conditional cases.

Events

NameParametersDescription
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

NameTypeDefaultDescription
dropEffectstringnullDefines the cursor style on drag over, valid values are copy, relocate, link and move.
pDroppableDisabledbooleanfalseWhether the element is droppable, useful for conditional cases.

Events

NameParametersDescription
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

View on GitHub

  1. <h3 class="first">Drag Only</h3>
  2. <div pDraggable="pnl" dragHandle=".ui-panel-titlebar">
  3. <p-panel header="Drag Header">
  4. The story begins as Don Vito Corleone, the head of a New York Mafia family, oversees his daughter's wedding.
  5. His beloved son Michael has just come home from the war, but does not intend to become part of his father's business.
  6. 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,
  7. kind and benevolent to those who give respect, but given to ruthless violence whenever anything stands against the good of the family.
  8. </p-panel>
  9. </div>
  10. <h3>Drag and Drop to Table</h3>
  11. <div class="ui-g">
  12. <div class="ui-g-12 ui-md-6 ui-g-nopad drag-column">
  13. <ul style="margin:0;padding:0">
  14. <li *ngFor="let car of availableCars" class="ui-helper-clearfix" pDraggable="cars"
  15. (onDragStart)="dragStart($event,car)" (onDragEnd)="dragEnd($event)">
  16. <i class="fa fa-arrows fa-2x" style="float:right;margin-top:8px"></i>
  17. <img src="assets/showcase/images/demo/car/{{car.brand}}.png" style="float:left" draggable="false">
  18. <div style="margin:8px 0 0 8px;float:left">{{car.vin}} - {{car.year}}</div>
  19. </li>
  20. </ul>
  21. </div>
  22. <div class="ui-g-12 ui-md-6 drop-column" pDroppable="cars"
  23. (onDrop)="drop($event)" [ngClass]="{'ui-highlight-car':draggedCar}">
  24. <p-table [value]="selectedCars">
  25. <ng-template pTemplate="header">
  26. <tr>
  27. <th>Vin</th>
  28. <th>Year</th>
  29. <th>Brand</th>
  30. <th>Color</th>
  31. </tr>
  32. </ng-template>
  33. <ng-template pTemplate="body" let-car>
  34. <tr>
  35. <td>{{car.vin}}</td>
  36. <td>{{car.year}}</td>
  37. <td>{{car.brand}}</td>
  38. <td>{{car.color}}</td>
  39. </tr>
  40. </ng-template>
  41. </p-table>
  42. </div>
  43. </div>
  44.  
  1. export class DragDropDemo {
  2. availableCars: Car[];
  3. selectedCars: Car[];
  4. draggedCar: Car;
  5. constructor(private carService: CarService) { }
  6. ngOnInit() {
  7. this.selectedCars = [];
  8. this.carService.getCarsSmall().then(cars => this.availableCars = cars);
  9. }
  10. dragStart(event,car: Car) {
  11. this.draggedCar = car;
  12. }
  13. drop(event) {
  14. if(this.draggedCar) {
  15. let draggedCarIndex = this.findIndex(this.draggedCar);
  16. this.selectedCars = [...this.selectedCars, this.draggedCar];
  17. this.availableCars = this.availableCars.filter((val,i) => i!=draggedCarIndex);
  18. this.draggedCar = null;
  19. }
  20. }
  21. dragEnd(event) {
  22. this.draggedCar = null;
  23. }
  24. findIndex(car: Car) {
  25. let index = -1;
  26. for(let i = 0; i < this.availableCars.length; i++) {
  27. if(car.vin === this.availableCars[i].vin) {
  28. index = i;
  29. break;
  30. }
  31. }
  32. return index;
  33. }
  34. }
  35.