views:

517

answers:

3

Hi,

I cannot find out how to obtain destination element with jQuery UI sortable.

    $("#pages").sortable({
        opacity: 0.6,
        update: function(event, ui) {
            var first = ui.item; // First element to swap
            var second = ???? // Second element to swap
            swapOnServer(first, second);
        }
    });

All the options I've tried point to the element being dragged, but not the one it is swapped with: ui.item[0], event.srcElement, event.toElement.

Additionally, this points to the LIST (OL) element.

Saying second I mean following:

Original order is:

| 0 | 1 | 2 | 3 |

We drag element 1 and drop it in position 3. Which will end up with:

| 0 | 3 | 2 | 1 |

So the first element is 1 and the second is 3 (WRONG! See below).

UPDATE: I have realised that I got it wrong. The new order in this case will be.

| 0 | 2 | 3 | 1 |

As a result my question does not really makes sense. Thanks everybody for the help. I'll mark vote and mark an answer.

So the question is how to obtain the second element here?


THE CURRENT WORKAROUND (as there is no term as swapping in sortable) is below. It uses temporary array with orders.

    var prevPagesOrder = [];
    $("#pages").sortable({
        start: function(event, ui) {
            prevPagesOrder = $(this).sortable('toArray');
        },
        update: function(event, ui) {
            var currentOrder = $(this).sortable('toArray');
            var first = ui.item[0].id;
            var second = currentOrder[prevPagesOrder.indexOf(first)];
            swapOnServer(first, second);
        }
    });

Thanks,
Dmitriy.

+2  A: 

Try using the serialize function which gives you a hash of the list of items in order.

If you just need the item that the new item go dropped before you can do this:

$("#pages").sortable({
    opacity: 0.6,
    update: function(event, ui) {
        var first = ui.item; // First element to swap
        var second = ui.item.prev();
        swapOnServer(first, second);
    }
});

second will be null if its at the start of the list.

PetersenDidIt
There's not much I can do with the whole list. I only need 2 items that has been swapped so I can post the change to server. I don't need to post to server the whole list swapping only 2 items.
Dmytrii Nagirniak
I think Dmitriy is referring to jQuery UI Sortable, you're referring to Interface element's sortable.
Ben
Updated my answer to show you how to grab the item before the dropped item.
PetersenDidIt
`ui.item.prev()` is **WRONG**. It will return previous element. I need to obtain the second element being swapped. There is no anything that says it will be previous one.
Dmytrii Nagirniak
there is no such thing as being "swapped" you are dropping something in the middle of a list. You can either send back the whole list in its new form, or send back the item that is before and/or after it in its new place. You aren't swapping anything.
PetersenDidIt
@petersendidit, thanks a lot for the help. I've just realised that i got it wrong. There is really no such thing as 'swapping' and I have to use the whole list. Updated the question to reflect that and voted your answer.
Dmytrii Nagirniak
+1  A: 

There's not really a "second" item per se. You have an item, and you are simply placing it in another location. The items around it adjust their positions accordingly. If you want to get an array of all the items, you can use the toArray method.

Raul Agrait
+1  A: 

take a look at the "Swapable" jQuery plugin:

http://plugins.jquery.com/project/Swapable

It similar to "Sortable", but only two elements of the selected group are affected: dragged element and dropped one which are swapped. All other elements stay at their current positions. This plugin is built based on existing "Sortable" jQuery plugin and inherits all sortable options.

vadimk
I didn't know about this plug-in. Thanks a lot.
Dmytrii Nagirniak
you are welcome :)
vadimk