Created
October 26, 2016 22:30
-
-
Save alfchee/0ca848c96c46899b7dd127f2d4b89f0a to your computer and use it in GitHub Desktop.
Use jQuery to sort <li> elements that posses a data-index property
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// selecting the ul and children | |
var $categories = this.$el.find('ul.sortable'), | |
$categoriesli = $categories.children('li'); | |
// sorting the list | |
$categoriesli.sort(function(a,b){ | |
var an = $(a).find('div.sort-arrows').data('index'), | |
bn = $(b).find('div.sort-arrows').data('index'); | |
if(an > bn) { | |
return 1; | |
} | |
if(an < bn) { | |
return -1; | |
} | |
return 0; | |
}); | |
// re-attaching the ordered list | |
$categoriesli.detach().appendTo($categories); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment