Angular Draggable - Getting Started
This tutorial guides you through the following steps:
- Add a Draggable to a page.
- Configure Draggable options.
- Handle events.
Each section in this tutorial covers a single configuration step. You can find the complete source code in the following GitHub repository:
Create Draggable
Add DevExtreme to your Angular application and use the following code to create a Draggable:
- <dx-draggable></dx-draggable>
- import { Component } from '@angular/core';
- import { type DxDraggableTypes } from 'devextreme-angular/ui/draggable';
- import { NgModule } from '@angular/core';
- import { BrowserModule } from '@angular/platform-browser';
- import { DxDraggableModule } from 'devextreme-angular';
- import { AppRoutingModule } from './app-routing.module';
- import { AppComponent } from './app.component';
- @NgModule({
- declarations: [
- AppComponent
- ],
- imports: [
- BrowserModule,
- AppRoutingModule,
- DxDraggableModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
- {
- // ...
- "styles": [
- "node_modules/devextreme/dist/css/dx.fluent.blue.light.css",
- ],
- }
Draggable does not include default visual elements. Specify custom markup as follows:
- <dx-draggable id="note-1" class="note">
- <div class="color-indicator"></div>
- <div class="text-container">
- <div class="body-text-box">Install New Router in Dev Room</div>
- <div class="detail-text-box">Amelia Harper</div>
- </div>
- </dx-draggable>
This example defines Draggable markup within a custom component, note.
This component configures the NoteInfo interface to specify note information. Use this interface within an array to define four notes. Implement the note component within a @for directive to add these notes to the app:
- <div>
- <dx-draggable
- [id]="id()"
- [group]="group()"
- [boundary]="boundary()"
- class="note"
- >
- <div class="color-indicator"></div>
- <div class="text-container">
- <div class="body-text-box">{{ task() }}</div>
- <div class="detail-text-box">{{ assignee() }}</div>
- </div>
- </dx-draggable>
- </div>
- import { Component, input, model } from '@angular/core';
- export interface NoteInfo {
- id: string;
- task: string;
- assignee: string;
- }
- @Component({
- selector: 'note',
- templateUrl: './note.component.html',
- styleUrls: ['../app/app.component.scss'],
- })
- export class NoteComponent {
- id = input<string>();
- group = input<string>('notes');
- boundary = input<string>('.board')
- task = input<string>();
- assignee = input<string>();
- // ...
- }
- <div class="boundary-text">Dragging Boundary</div>
- <div class="board">
- @for (note of notes; track note) {
- <note
- [id]="note.id"
- [task]="note.task"
- [assignee]="note.assignee"
- ></note>
- }
- </div>
- <div class="boundary-text">Dragging Boundary</div>
- import { Component } from '@angular/core';
- import { type NoteInfo } from 'src/note/note.component';
- // ...
- export class AppComponent {
- notes: NoteInfo[] = [{
- id: 'note-1',
- task: 'Install New Router in Dev Room',
- assignee: 'Amelia Harper',
- }, {
- id: 'note-2',
- task: '👨💻 Launch New Website',
- assignee: 'Brett Wade',
- }, {
- id: 'note-3',
- task: 'Prepare 2026 Marketing Plan',
- assignee: 'Robert Reagan',
- }, {
- id: 'note-4',
- task: '🖥️ Approve Personal Computer Upgrade Plan',
- assignee: 'Bart Arnaz',
- }];
- }
Configure Draggable Options
This example specifies the following Draggable properties:
- boundary: Constrains Draggable movement inside a container (
.board). - group: Specifies an interaction group (used in events such as onDragMove).
Handle Events
This example specifies the following event handlers to update the note's Z-index (bring to front while dragging):
This example also implements a (click) handler on the parent <div> element within the note component:
- <div (click)="handleClick()">
- <dx-draggable ...
- [style.z-index]="currentZIndex"
- [class.overlapped]="isOverlapped()"
- (onDragMove)="handleDragMove($event)"
- (onDragEnd)="handleDragEnd()"
- (onDragStart)="handleDragStart()"
- >
- <!-- ... -->
- </dx-draggable>
- </div>
- import { type DxDraggableTypes } from 'devextreme-angular/ui/draggable';
- // ...
- export class NoteComponent {
- // ...
- overlappedComponentId: string | null = null;
- currentZIndex: number = 0;
- updateZIndex(): void {
- const updatedZIndex = this.zIndex() + 1;
- this.currentZIndex = updatedZIndex;
- this.zIndex.update(() => updatedZIndex);
- console.log(this.currentZIndex)
- }
- handleDragStart(): void {
- this.updateZIndex();
- }
- handleDragMove(e: DxDraggableTypes.DragMoveEvent): void {
- if (e.toComponent !== e.component) {
- const toComponentId = e.toComponent.element().id;
- this.startOverlap()(toComponentId);
- this.overlappedComponentId = toComponentId;
- } else {
- this.stopOverlap()();
- this.overlappedComponentId = null;
- }
- }
- handleDragEnd(): void {
- this.stopOverlap()();
- }
- handleClick(): void {
- this.updateZIndex();
- }
- }
- <div class="boundary-text">Dragging Boundary</div>
- <div class="board">
- @for (note of notes; track note) {
- <note ...
- [isOverlapped]="overlappedId === note.id"
- [(zIndex)]="zIndex"
- [startOverlap]="startOverlap"
- [stopOverlap]="stopOverlap"
- ></note>
- }
- </div>
- <div class="boundary-text">Dragging Boundary</div>
- import { Component } from '@angular/core';
- // ...
- export class AppComponent {
- // ...
- zIndex: number = 0;
- overlappedId: string | null = null;
- startOverlap: (id: string) => void = (id) => {
- this.overlappedId = id;
- }
- stopOverlap: () => void = () => {
- this.overlappedId = null;
- }
- handleDragEnd: () => void = () => {
- this.stopOverlap();
- }
- }
- .note.overlapped {
- outline: 1px dashed var(--dx-color-primary);
- }
Refer to the following repository for the complete source code of this tutorial: