I have implemented drag and drop functionality using jquery and jquery-ui within an angular project. Below is the code structure:
Index.html,
<!doctype html>
<html lang="en">
<head>
<link href="//code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="//code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<meta charset="utf-8">
<title>Drag</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<app-root></app-root>
</body>
</html>
app.component.html:
<ul id="people">
<li *ngFor="let person of people; let i = index">
<div class="draggable" id={{i}}>
<p> <b> {{ person.name }} </b> Index => {{i}}</p>
</div>
<br><br>
</li>
</ul>
app.component.ts:
import { Component } from '@angular/core';
declare var jquery:any;
declare var $ :any;
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'My application';
people: any[] = [
{
"name": "Person 1"
},
{
"name": "Person 2"
},
{
"name": "Person 3"
},
{
"name": "Person 4"
},
{
"name": "Person 5"
}
];
ngOnInit(): void {
$("#people").sortable({
update: function(e, ui) {
$("#people .draggable").each(function(i, element) {
$(element).attr("id", $(element).index("#people .draggable"));
$(element).text($(element).text().split("Index")[0] + " " + "Index: " + " => " + $(element).attr("id"));
});
}
});
}
}
While this implementation works well, I am looking to convert the code to typescript without the use of jquery and jquery-ui. I am new to angular and typescript, so I would appreciate any guidance on achieving drag and drop functionality in pure typescript and angular, avoiding jquery altogether.
I have explored libraries like angular4-drag-drop
and ng2-dragula
, but encountered issues with updating index values when reordering elements. Hence, I relied on jquery. However, I am seeking a solution that enables me to achieve the same result using angular and typescript exclusively.
I welcome any suggestions or solutions to help me accomplish this task effectively.