Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Sortable Auto Sort

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

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.

Here's the 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 -->

Here's the JS:

$(function() {
    $( "#sortable" ).sortable();

    $(":button").click(function(){
    $(this).parent().remove();
    var arr=$("#sortable").sortable('toArray');
    text=arr.toString();
    alert(text);
    });

});

And here's the 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; }

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">
like image 418
June Avatar asked Jun 10 '11 07:06

June


1 Answers

Check this out

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

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);
    });
});

Once you got this array sorted, you can simply order them with a .each() cycle

like image 163
VAShhh Avatar answered Nov 13 '22 01:11

VAShhh