sbr*_*bru 6 jquery jquery-ui jquery-ui-sortable
我想知道是否有可能将JQuery UI可排序的默认值按字母顺序排序.如果是这样,如果我将项目添加到可排序项中,是否也可以按字母顺序对其进行实时排序?以下是我的代码:
// Adds item to sortable list
$(".addButton").click(function(e) {
e.preventDefault();
// set var item to be the string inputted by the user
var item = $("input[name='brewItem']").val();
// parses input string, splitting at commas into liArray containing substrings as elements
var liArray = item.split(", ");
// for loop to add each brew to the sortable list (length-1 because last element in array is empty string)
for (var i = 0; i < liArray.length-1; i++) {
// sets var $li to the string in the ith index of liArray
var $li = $("<li class='ui-state-default'/>").text(liArray[i]);
// adds var $li to gui
$("#sortable").append($li);
};
// refreshes the page so var $li shows up
$("#sortable").sortable("refresh");
});
Run Code Online (Sandbox Code Playgroud)
我不太确定在何处或如何实现这一点.任何帮助表示赞赏,谢谢!
PSL*_*PSL 15
You need to tweak it to make it sort as you need.
Try this:- Fiddle
Use custom sort method
function sort() {
var sortableList = $('#sortable');
var listitems = $('li', sortableList);
listitems.sort(function (a, b) {
return ($(a).text().toUpperCase() > $(b).text().toUpperCase()) ? 1 : -1;
});
sortableList.append(listitems);
}
Run Code Online (Sandbox Code Playgroud)
Call it in your sortable's create event Create and in Button Click
$("#sortable").sortable({
create: function (event, ui) {
sort();
}
});
Run Code Online (Sandbox Code Playgroud)
Or Extend jquery ui sortable widget to include your custom sorting logic.