The drag and drop function in Angular ListBox.
A live Angular example of jqxListBox, the jQWidgets ListBox component, with its complete source code below. It is one of 18 jqxListBox demos for Angular.
allowDrag property – Enables/disables the dragging of ListBox Items.allowDrop property – Enables/disables the dropping of ListBox Items.dragStart property – Callback function which is called when a drag operation starts.dragEnd property – Callback function which is called when a drag operation ends.height property – Sets or gets the listbox's height.renderer property – Callback function which is called when an item is rendered.source property – Sets or gets the item's source.width property – Sets or gets the listbox's width.val method – Sets or gets the selected value.To run it in your own app, install the package with npm install jqwidgets-ng, add these files to a standalone Angular CLI app, and add jqx.base.css and a theme to the styles in angular.json. Getting started with jqxListBox shows the setup step by step.
import { Component, ElementRef, viewChild } from '@angular/core';
import { jqxTextAreaComponent } from 'jqwidgets-ng/jqxtextarea';
import { CommonModule } from '@angular/common';
import { jqxListBoxModule } from 'jqwidgets-ng/jqxlistbox';
import { jqxTextAreaModule } from 'jqwidgets-ng/jqxtextarea';
import { jqxDragDropModule } from 'jqwidgets-ng/jqxdragdrop';
@Component({
imports: [CommonModule, jqxListBoxModule, jqxTextAreaModule, jqxDragDropModule],
selector: 'app-root',
templateUrl: './app.html'
})
export class App {
readonly myTextArea = viewChild.required<jqxTextAreaComponent>('myTextArea');
readonly dragStartLog = viewChild.required<ElementRef>('dragStartLog');
readonly dragEndLog = viewChild.required<ElementRef>('dragEndLog');
onDragStart(event: any): void {
this.dragStartLog().nativeElement.innerHTML = 'Drag Start: ' + event.args.label;
this.dragEndLog().nativeElement.innerHTML = '';
}
onDragEnd(event: any): void {
this.dragEndLog().nativeElement.innerHTML = 'Drag End';
if (event.args.label) {
let ev = event.args.originalEvent;
let x = ev.pageX;
let y = ev.pageY;
if (event.args.originalEvent && event.args.originalEvent.originalEvent && event.args.originalEvent.originalEvent.touches) {
let touch = event.args.originalEvent.originalEvent.changedTouches[0];
x = touch.pageX;
y = touch.pageY;
}
let textareaElement = this.myTextArea().elementRef.nativeElement.firstElementChild;
let width = textareaElement.offsetWidth;
let height = textareaElement.offsetHeight;
let right = parseInt(textareaElement.offsetLeft) + width;
let bottom = parseInt(textareaElement.offsetTop) + height;
if (x >= parseInt(textareaElement.offsetLeft) && x <= right) {
if (y >= parseInt(textareaElement.offsetTop) && y <= bottom) {
this.myTextArea().val(event.args.label);
}
}
}
}
source1: string[] =
[
'Affogato',
'Americano',
'Bicerin',
'Breve',
'Café Bombón',
'Café au lait',
'Caffé Corretto',
'Café Crema',
'Caffé Latte',
'Caffé macchiato',
'Café mélange',
'Coffee milk',
'Cafe mocha'
];
source2: string[] =
[
'Cappuccino',
'Carajillo',
'Cortado',
'Cuban espresso',
'Espresso',
'Eiskaffee',
'The Flat White',
'Frappuccino',
'Galao',
'Greek frappé coffee',
'Iced Coffee',
'Indian filter coffee',
'Instant coffee',
'Irish coffee',
'Liqueur coffee'
];
// Callbacks
dragStart = (item: any): boolean => {
if (item.label == 'Breve') {
return false;
}
return true;
};
dragEnd = (dragItem: any, dropItem: any): boolean => {
if (dragItem.label == 'Frappuccino')
return false;
return true;
};
renderer1 = (index: number, label: any, value: any): string => {
if (label == 'Breve') {
return '<span style="color: red;">' + label + '</span>';
}
return label;
};
renderer2 = (index: number, label: any, value: any): string => {
if (label == 'Frappuccino') {
return '<span style="color: red">' + label + '</span>';
}
return label;
};
// Events
dragStartA(event: any): void {
this.onDragStart(event);
};
dragStartB(event: any): void {
this.onDragStart(event);
};
dragEndA(event: any): void {
this.onDragEnd(event);
};
dragEndB(event: any): void {
this.onDragEnd(event);
};
}
<jqxListBox style="float: left;" #listBoxA
(onDragStart)="dragStartA($event)"
(onDragEnd)="dragEndA($event)"
[allowDrop]="true"
[allowDrag]="true"
[source]="source1"
[dragStart]="dragStart"
[renderer]="renderer1"
[width]="200"
[height]="250">
</jqxListBox>
<jqxListBox style="margin-left: 20px; float: left;" #listBoxB
(onDragStart)="dragStartB($event)"
(onDragEnd)="dragEndB($event)"
[allowDrop]="true"
[allowDrag]="true"
[source]="source2"
[dragEnd]="dragEnd"
[renderer]="renderer2"
[width]="200"
[height]="250">
</jqxListBox>
<div style="width: 200px; height: 200px; float: left; margin-left: 20px;">
<!--<textarea rows="5" id="textarea"></textarea>-->
<jqxTextArea #myTextArea [width]="200" [height]="100"></jqxTextArea>
</div>
<div style="font-size: 13px; font-family: Verdana; padding-top: 20px; clear: both;">
<b>Events Log:</b>
<div #dragStartLog>
</div>
<div #dragEndLog>
</div>
<br />
<b>Note:</b>
<br />
Dragging of "Breve" is disabled.
<br />
Dropping of "Frappuccino" is disabled.
</div>