Draggable

pDraggable is attached to an element to add a drag behavior. The value of the pDraggable attribute is required--it defines the scope to match draggables with droppables. By default, the whole element is draggable. We can restrict the draggable area by applying the dragHandle attribute. The value of dragHandle can be any CSS selector. In DataGrid, with available documents, we only made the panel's header draggable:

<p-dataGrid [value]="availableDocs">
<p-header>
Available Documents
</p-header>
<ng-template let-doc pTemplate="item">
<div class="ui-g-12 ui-md-4" pDraggable="docs"
dragHandle=".ui-panel-titlebar" dragEffect="move"
(onDragStart)="dragStart($event, doc)"
(onDragEnd)="dragEnd($event)">
<p-panel [header]="doc.title" [style]="{'text-align':'center'}">
<img src="/assets/data/images/docs/{{doc.extension}}.png">
</p-panel>
</div>
</ng-template>
</p-dataGrid>

The draggable element can fire three events when the dragging process begins, proceeds, and ends. These are onDragStart, onDrag, and onDragEnd, respectively. In the component class, we buffer the dragged document at the beginning and reset it at the end of the dragging process. This task is done in two callbacks: dragStart and dragEnd:

class DragDropComponent {
availableDocs: Document[];
deletedDocs: Document[];
draggedDoc: Document;

constructor(private docService: DocumentService) { }

ngOnInit() {
this.deletedDocs = [];
this.docService.getDocuments().subscribe((docs: Document[]) =>
this.availableDocs = docs);
}

dragStart(event: any, doc: Document) {
this.draggedDoc = doc;
}

dragEnd(event: any) {
this.draggedDoc = null;
}

...
}

In the shown code, we used the Document interface with the following properties:

interface Document {
id: string;
title: string;
size: number;
creator: string;
creationDate: Date;
extension: string;
}

In the demo application, we set the cursor to move when the mouse is moved over any panel's header. This trick provides a better visual feedback for the draggable area:

body .ui-panel .ui-panel-titlebar {
cursor: move;
}

We can also set the dragEffect attribute to specify the effect that is allowed for a drag operation. Possible values are none, copy, move, link, copyMove, copyLink, linkMove, and all. Refer to the official documentation to read more details at https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer/effectAllowed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.74.231