Jquery 可排序自动排序 [英] Jquery Sortable Auto Sort
问题描述
我们能否使用我在每个 li 中获取的 id 或 idx 对运行时可排序的 jquery 进行排序.我希望它在运行时排序
can we sort the jquery sortable at runtime using the id or idx as taken by me in each li. I want it to sort in the run time
这里是小提琴.我希望它自动排序,例如 <li id=1>
应该先于 <li id=2>
等等.帮助将不胜感激我是新手尝试学习 jquery.
here is fiddle . I want it to auto sort for eg <li id=1>
should come first than <li id=2>
and so on.. Help will be appreciated as I am Novice trying to learn jquery.
这是 HTML:
<div class="demo" style="width:444px">
<ul id="sortable">
<li itemID=3 id='3' class="ui-state-default">3<button>delete</button></li>
<li itemID=6 id='6' class="ui-state-default">6<button>delete</button></li>
<li itemID=1 id='1' class="ui-state-default">1<button>delete</button></li>
<li itemID=4 id='4' class="ui-state-default">4<button>delete</button></li>
<li itemID=9 id='9' class="ui-state-default">9<button>delete</button></li>
<li itemID=2 id='2' class="ui-state-default">2<button>delete</button></li>
<li itemID=8 id='8' class="ui-state-default">8<button>delete</button></li>
<li itemID=5 id='5' class="ui-state-default">5<button>delete</button></li>
<li itemID=11 id='11' class="ui-state-default">11<button>delete</button></li>
<li itemID=7 id='7' class="ui-state-default">7<button>delete</button></li>
<li itemID=10 id='10' class="ui-state-default">10<button>delete</button></li>
<li itemID=12 id='12' class="ui-state-default">12<button>delete</button></li>
</ul>
</div><!-- End demo -->
这是 JS:
$(function() {
$( "#sortable" ).sortable();
$(":button").click(function(){
$(this).parent().remove();
var arr=$("#sortable").sortable('toArray');
text=arr.toString();
alert(text);
});
});
这是 CSS:
#sortable { list-style-type: none; margin: 0; padding: 0; }
#sortable li { margin: 3px 3px 3px 0; padding: 1px; float: left; width: 100px; height: 90px; font-size: 4em; text-align: center; }
我还引入了一堆库和 CSS(不确定 JSFiddle 是否需要):
I also pull in a bunch of libraries and CSS (not sure if it is required on JSFiddle or not):
<link rel="stylesheet" href="http://jqueryui.com/themes/base/jquery.ui.all.css">
<script src="http://jqueryui.com/jquery-1.5.1.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.core.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.widget.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.mouse.js"></script>
<script src="http://jqueryui.com/ui/jquery.ui.sortable.js"></script>
<link rel="stylesheet" href="http://jqueryui.com/demos/demos.css">
推荐答案
看看这个
使用标准 jQuery 库和 .sort()
方法,您可以指定用于对对象数组进行排序的函数.
Using the standard jQuery library and the .sort()
method you can specify a function to use for sorting your array of objects.
$('#sort').click(function() {
var mylist = $('#sortable');
var listitems = mylist.children('li').get();
listitems.sort(function(a, b) {
var compA = parseFloat($(a).attr('id'));
var compB = parseFloat($(b).attr('id'));
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
});
$.each(listitems, function(idx, itm) {
mylist.append(itm);
});
});
一旦你对这个数组进行了排序,你可以简单地用 .each()
循环对它们进行排序
Once you got this array sorted, you can simply order them with a .each()
cycle
这篇关于Jquery 可排序自动排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!