jQuery Sortable自动排序 [英] Jquery Sortable Auto Sort

查看:73
本文介绍了jQuery Sortable自动排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们可以在运行时使用我在每个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">

推荐答案

检查一下

http://jsfiddle.net/wmaqb/2/

使用标准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 Sortable自动排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆