import { Component } from
"@angular/core"
;
import { MessageService } from
"primeng/api"
;
interface Person {
id: String;
name: String;
age: number;
}
@Component({
selector:
"app-root"
,
templateUrl:
"./app.component.html"
,
styleUrls: [
"./app.component.css"
],
providers: [MessageService]
})
export class AppComponent {
constructor(private msg: MessageService) {}
available: Person[] = [];
selected: Person[] = [];
currentlyDragging: Person |
null
=
null
;
ngOnInit() {
this
.available = [
{
id:
"ASDF12"
,
name:
"Sandeep Jain Sir"
,
age: 38
},
{
id:
"KJHY45"
,
name:
"Shivangi Goel"
,
age: 20
},
{
id:
"LKIO34"
,
name:
"Harshit Khandelwal"
,
age: 30
},
{
id:
"LPOI21"
,
name:
"Taranjeet Singh"
,
age: 25
},
{
id:
"VANI12"
,
name:
"Vanishka Singh"
,
age: 23
}
];
}
dragStart(person: Person) {
this
.currentlyDragging = person;
this
.msg.add({
severity:
"info"
,
summary:
"Drag Started"
,
detail:
"onDragStart Event"
});
}
drag() {
this
.msg.add({
severity:
"success"
,
summary:
"Dragging..."
,
detail:
"onDrag Event"
});
}
dragEnd() {
this
.currentlyDragging =
null
;
this
.msg.add({
severity:
"error"
,
summary:
"Drag End"
,
detail:
"onDragEnd Event"
});
}
drop() {
if
(
this
.currentlyDragging) {
let currentlyDraggingIndex =
this
.findIndex(
this
.currentlyDragging);
this
.selected = [...
this
.selected,
this
.currentlyDragging];
this
.available =
this
.available.filter(
(val, i) => i != currentlyDraggingIndex
);
this
.currentlyDragging =
null
;
}
}
findIndex(person: Person) {
let index = -1;
for
(let i = 0; i <
this
.available.length; i++) {
if
(person.id ===
this
.available[i].id) {
index = i;
break
;
}
}
return
index;
}
}