I have an existing array of elements, example:
arr=[{name:1,data:xyz},{name:2,data:abc},{name:3, data:lmn}; {name:4,data:opq}]
This items are populated according to their name on UI.
Example: 1 2 3 4 5
I have implemented Sortable of jQuery. Instead of creating a new array, I wish to update the exiting array arr
according to the new position of the elements after the user has done performing the drag & drop operation on the above elements.
User can move elements present inside the array arr
and cannot introduce any dragged object from outside the 1 2 3 4 5
elements' list, example: 2 can be moved to 5 or 1 can be moved to 3 etc etc.
Elements HTML:
<ul class= "tree-list">
<li class="li_class"><span class="span_class"> 1 </span></li>
<li class="li_class"><span class="span_class"> 2 </span></li>
<li class="li_class"><span class="span_class"> 3 </span></li>
<li class="li_class"><span class="span_class"> 4 </span></li>
<li class="li_class"><span class="span_class"> 5 </span></li>
</ul>
jQuery:
$(function() {
var data = $(".tree-list").sortable({
delay: 150,
axis: "y",
update: function(event, ui) {
var index = ui.item.index();
console.log("Moved to new position: " + index);
var children = $('ul.tree-list').children()
// What to do here
})
$( ".tree-list" ).disableSelection();
});
Array created dynamically:
someFunc(data){
let htmlString= " <ul class =\"tree-list\" "
for (let i=0; i<data.length; i++){
if(typeof(item) === "string"){
htmlString += "<li class=\" li_class \"" + item + "\">";
}
else if(typeof(item) === "object"){
htmlString += "<li>";
htmlString += "span class=\"span_class\""
}
htmlString += "</li>"
}
}
htmlString += "</ul>";
return (htmlString);
}