Jquery排序列表元素

use*_*440 4 html javascript css sorting jquery

我的小提琴代码:http://jsfiddle.net/wguan/7yMSz/

当前代码允许在两个div框之间拖放.但我想知道如何默认自动排序,以便当我在框之间拖动时,两者都会自动从顶部的最小数字到底部的最大数字排序.

HTML

<div id="boxes_holder" class="initBox">
    <div draggable="true" class="boxes" style="text-align:center;">1</div>
    <div draggable="true" class="boxes" style="text-align:center;">2</div>
    <div draggable="true" class="boxes" style="text-align:center;">3</div>
    <div draggable="true" class="boxes" style="text-align:center;">4</div>
</div>



<div id="dragBox" class = "initBox">
    <div id="dragBoxTitle" class = "">Box</div>
</div>
Run Code Online (Sandbox Code Playgroud)

CSS:

#boxes_holder{
width:100px;
height:100px;
border:0px;
display: inline-block;
float: left;
vertical-align:top;
background-color:#FBFBFB;
list-style-type: none;   
}

#dragBox{
width:100px;
height:100px;
border:0px;
display: inline-block;
float: right;
vertical-align:top;
background-color:#FBFBFB;
list-style-type: none;   
}
Run Code Online (Sandbox Code Playgroud)

使用Javascript:

$(document).ready(function () {
$('#boxes_holder, #dragBox').sortable({
    connectWith: '.initBox',


    //Whenever Dropped Item
    receive: function (event, ui) {
        if ($(this).children().length > 4) {
            if ($(this).attr('id') == 'dragBox') {

                $(ui.sender).sortable('cancel');
            }
        }
    }
});
});
Run Code Online (Sandbox Code Playgroud)

我的实际HTML代码使用PHP(似乎不起作用):

<div id = "boxes_holder" class = "initBox">
<?php
//Creates 49 BOXES elements


    $x = 49;
    for($i=1; $i<=$x; $i++){
       echo '<li><span class = "boxes" style="text-align:center;float:left;margin:10px;" > '.$i.'</span></li>';
    }

?>

</div>

<div id="dragBox" class = "initBox">
<div id="dragBoxTitle" class = "">
    Pick Your Numbers:
</div>
</div>
Run Code Online (Sandbox Code Playgroud)

Jit*_*hin 5

下面将对掉落后的元素进行排序.

    $('#boxes_holder, #dragBox').sortable({
        connectWith: '.initBox',


        //Whenever Dropped Item
        receive: function (event, ui) {
            $(this).find('div.boxes').sort(sortAlpha).appendTo(this);  
            if ($(this).children().length > 5) {
                if ($(this).attr('id') == 'dragBox') {
                    $(ui.sender).sortable('cancel');
                }
            }
        }
    });

    function sortAlpha(a,b){  
        return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1;  
    }
Run Code Online (Sandbox Code Playgroud)

检查一下.http://jsfiddle.net/nsjithin/7yMSz/1/

编辑:所以它需要在任何时候排序,位置改变,排序或拖放.您需要将它放在update事件处理程序中.

   $('#boxes_holder, #dragBox').sortable({
        connectWith: '.initBox',

        update: function( event, ui ) {
            $(this).find('div.boxes').sort(sortAlpha).appendTo(this);  
        },
        //Whenever Dropped Item
        receive: function (event, ui) {
            if ($(this).children().length > 4) {
                if ($(this).attr('id') == 'dragBox') {
                    $(ui.sender).sortable('cancel');
                }
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

编辑2:使用<li>html结构.

$('#boxes_holder, #dragBox').sortable({
    connectWith: '.initBox',

    update: function( event, ui ) {
        $(this).find('li').sort(sortAlpha).appendTo(this);  
    },
    //Whenever Dropped Item
    receive: function (event, ui) {
        if ($(this).children().length > 4) {
            if ($(this).attr('id') == 'dragBox') {
                $(ui.sender).sortable('cancel');
            }
        }
    }
});

function sortAlpha(a,b){  
    return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1;  
}
Run Code Online (Sandbox Code Playgroud)

Jsfiddle:http://jsfiddle.net/nsjithin/7yMSz/3/