4

My Code in Fiddle: http://jsfiddle.net/wguan/7yMSz/

The current code allows one to drag and drop between two div boxes. But I was wondering how to auto sort by default so that when i drag between the boxes, both will automatically sort from smallest number on top to largest number at the bottom.

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>

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

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

My Actual HTML Code with PHP below (doesn't seem to work):

<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>
2
  • The answer below works fine but when I place it under my code for modification, it doesn't seem to work...please see attached for my newly updated code Commented Nov 8, 2013 at 23:42
  • Have updated the answer. Please check. Commented Nov 14, 2013 at 4:13

1 Answer 1

5

Following will sort the elements after the drop.

    $('#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;  
    }

Check the unfiddle. http://jsfiddle.net/nsjithin/7yMSz/1/

Edit : So it need to sorted whenever, the position changed, sorted or dragged/dropped. You need to put it in the update event handler.

   $('#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');
                }
            }
        }
    });

Edit 2 : Working with the <li> html structure.

$('#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;  
}

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

Sign up to request clarification or add additional context in comments.

6 Comments

The problem with this one is that the original div box is not sorting. So if I don't do any dragging and dropping, and just move the stuff in the initial box, they are not sorting. In other word how can every at everywhere be sorted.
This works, but not for my case for some reason. For my case I am using PHP to create 49 boxes. From there I sort the boxes but mine doesn't work. I've updated the code...I am not sure if Jfiddle takes php...
i am pretty sure it has to do with my .find() as when I place alert statements before and after, it never prints out the statement after.
@user1234440 It doesn't work because you are creating spans with that class in your php code, instead of divs. Try setting the class on the li element instead, and finding just by .boxes
in the loop I tried to do : echo '<li class = "boxes"><span style="text-align:center;float:left;margin:10px;" > '.$i.'</span></li>'; with your suggestion, but it still doesn't fix it, i also used .boxes as oppose to div.boxes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.