Draggable Sorted List with Javascript
course from Brad Traversy
Features:
drag and drop HTML API
Array.prototype.sort()
closest() getAttribute() data-index
HTML
draggable list is a unsorted listthe button is for checking if the list after manipulating is sorted correctlly
CSS
when list item add class named over, the draggable should have a colored backgroundJS
select elements draggable list and check buttonthe data is a richestPeople array string with richest first ordered
listItem is to store the custom list array
map the data and get the random index to sort the list and then create all list items
Array.prototype.sort() sort by the callback function MDN
and then add the events drag and drop
Element.closest(): MDN
The
closest()
method traverses the Element
and its parents (heading toward the document root) until it finds a node that matches the provided selector string. Will return itself or the matching ancestor. If no such element exists, it returns null
.dragOver run when drag active
when drag enter into another list, add the classname over for the background color,when drag leave, remove the over classname.
when dragdrop happends, get the current data-index and swap the start index and end index,also remove the over classname
swap the two list items with index and then render in DOM
swap the two list items with index and then render in DOM