4

I'm trying to create a drag and drop from one list to one of two others. I've been trying to use the draggable and droppable, but this sortable demo looks more like what I want: http://jqueryui.com/sortable/#connect-lists

I'm trying to do it with draggable/droppable because of my strict requirements:

  1. I have several days printed on the page

  2. Each day has 3 lists: inbound, pending and processed

  3. Items from inbound can be dragged to either the pending or processed lists

  4. Those items will be appended to the bottom of the applied list

  5. Items on the pending and processed lists cannot be dragged, except those which originated on the inbound list

Because of these my thoughts were to have the individual inbound list items be draggable and appended to the end of the applied list.

Here is the JS Code I'm using:

$(document).ready(function() {
    $('li.drag').draggable({
        revert: "invalid",
        revertDuration: 100
    });
    $('.pending-list').droppable({
        accept: ".inbound li",
        drop: function(event, ui) {
            $(this).appendChild($(ui));
        }
    });
    $('.processed-list').droppable({
        accept: ".inbound li",
        drop: function(event, ui) {
            $(this).append($(ui));
        }
    });
});

Here is my fiddle, but as you can see (1) it's not applying to the bottom of the list and (2) I cannot move it again after: http://jsfiddle.net/wGWTS/1/

Bing
  • 3,071
  • 6
  • 42
  • 81

1 Answers1

1

EDIT: changed the code slightly since the inbound last was acting funny when it had no members.

I think you want to be using a combination of the connectWith and items options of sortable. Part of sortable's functionality is to accept items from other lists, via connectWith, so you don't need to make it droppable. By using the items property, you can choose which members of the list should be sortable, so to enforce condition #5 you want only items that came from the inbound list to be sortable. This is done by giving all inbound list items the class of "inbound".

Here is an example with inbound, pending, and outbound lists. Items in inbound can be moved between all 3 lists, and items originating in pending or outbound cannot be moved at all. See here for a fiddle: http://jsfiddle.net/ULmrD/4/

HTML

<div id=inbound class=sort>
    <ul>
        <li><b>Inbound</b></li>
        <li class=inbound>Inbound Item 1</li>
        <li class=inbound>Inbound Item 2</li>
        <li class=inbound>Inbound Item 3</li>
    </ul>
</div>
<div class=sort id=pending>
    <b>Pending</b>
    <ul>
        <li>Pending Item 1</li>
        <li>Pending Item 2</li>
    </ul>
</div>

<div class=sort id=outbound>
    <b>Outbound</b>
    <ul>
        <li>Outbound Item 1</li>
        <li>Outbound Item 2</li>
    </ul>
</div>

CSS

div.sort{
    border:1px solid black;
    display:inline;
    width:175px;
    float:left;
}
ul{
    height:200px;
    width:175px;
}

jQuery

$(document).ready(function() {
    $('div.sort li').disableSelection();

    $('#pending ul').sortable({
        revert: 'invalid',
        connectWith: "#inbound ul, #outbound ul",
        items: "li.inbound"
    });

    $('#outbound ul').sortable({
        revert: 'invalid',
        connectWith: "#inbound ul, #pending ul",
        items: "li.inbound"
    });

    $('#inbound ul').sortable({
        revert: 'invalid',
        connectWith: "#pending ul, #outbound ul",
        items: "li.inbound"
    });

});
chiliNUT
  • 18,989
  • 14
  • 66
  • 106
  • I've got a little follow-up: when I drop the item into that list I'd like to to reorder within the new list (I have a `data-order` field to sort on which every `li` has). Any clue how to do that, too? Thanks! – Bing Dec 01 '13 at 21:39
  • I was thinking about that when I was constructing the example. From what I can tell, if you want those items to still sort with the other items, then they will also still be draggable, which isn't desired. I'm there is a way around that but it's not trivial. You might want to look at this SO http://stackoverflow.com/questions/1134976/how-may-i-sort-a-list-alphabetically-using-jquery and look at the second answer to try and construct your own comparator to sort by the `data-order` – chiliNUT Dec 01 '13 at 22:56
  • This is so much helpful for drag-drop newbie like me. Thanks a lot! – Nam G VU Jan 24 '18 at 03:32
  • By the way, what it means by *revert:'invalid'* is explained here https://www.tutorialspoint.com/jqueryui/jqueryui_sortable.htm#option_revert i.e. adding animation when sorted – Nam G VU Jan 24 '18 at 03:58