Holonet
Holonet

Reputation: 63

jQuery UI sortable - get items adjacent to a drop

I have sortable lists, and I need to prevent the drop in some cases, depending on what I'm "pushing aside" when dropping. Here's some sorta-pseudo code:

    $('ul#SortableList').sortable( {connectWith: 'ul#OtherList',
                               beforeStop: function(ev, ui) {
                               // Need item(s) that are being "pushed" out of the way of the item being dropped
                              if (adjacentItem == condition)
                              { // prevent the drop
                                   $(this).sortable("cancel");
                              }

I know "this" would give me the actual list itself being dropped onto, but I don't know how to get the actual items being dropped between...or even just a specific one before or after would get me on the right track.

Upvotes: 2

Views: 85

Answers (1)

Holonet
Holonet

Reputation: 63

Figured it out:

    beforeStop: function(ev, ui)
                          var previousItem = ui.placeholder.parent().children().get(ui.placeholder.index() - 2); // 2 works here, probably because the placeholder AND the dropped item are counted
                          console.log(previousItem);
                          console.log(ui.placeholder);
                          var nextItem = ui.placeholder.parent().children().get(ui.placeholder.index() + 1);
                          console.log(nextItem);
                          console.log(ui.placeholder.parent());
                          }

Upvotes: 1

Related Questions