Drag & Drop Between Wrapped Grid Components
Environment
| Product | Progress® Kendo UI for Angular Grid |
Description
I am using a custom component to wrap the Kendo UI for Angular Grid and reuse it in the same configuration on multiple instances across my Angular application. How can I implement a drag-and-drop feature between two Grids which are encapsulated in my custom wrapper component?
Solution
To implement a drag-and-drop functionality between two wrapped Grid components:
-
Expose a designated property for the wrapper component that allows differentiating the Grids.
html<kendo-grid [data]="data" [attr.data-kendo-grid-index]="index"> ... </kendo-grid> -
Wrap the two instances of the component that encapsulates the
Gridin a custom container and apply theDragTargetContainerDirectiveandDropTargetContainerDirectivedirectives.html<div #wrapper kendoDragTargetContainer kendoDropTargetContainer > <app-wrapper #first [data]="inStockData"></app-wrapper> <app-wrapper #second [data]="discontinuedData"></app-wrapper> </div> -
Handle the
Kendo UI for Angular Drag and Dropevents that the two directives expose and modify theGridcollections in the event handlers as necessary.html<div ... (onDrop)="onDrop($event)" > <app-wrapper #first [data]="inStockData"></app-wrapper> <app-wrapper #second [data]="discontinuedData"></app-wrapper> </div> -
To notify the drag-and-drop container that its content has changed, get the reference of the wrapper container and read it as
DragTargetContainerDirectiveandDropTargetContainerDirective. Then use thenotify()method of the directives, to update the view.ts@ViewChild('wrapper', { read: DragTargetContainerDirective }) dragTargetContainer; @ViewChild('wrapper', { read: DropTargetContainerDirective }) dropTargetContainer; private notifyContainers(): void { this.dragTargetContainer.notify(); this.dropTargetContainer.notify(); }
The following example demonstrates the full implementation of the suggested approach.