Canser Yanbakan
Canser Yanbakan

Reputation: 3870

Jquery Sortable Update Event can called only one time?

I'm trying to make category changes with Jquery & Php. I have no problem with it. My problem is, when the update event is called, it returning 2 results. 1 result for Dragged Parent, One result for Dropped Parent. I wanna call only dropped parent's id. Here is my script:

$("#gallery ul").sortable({
    connectWith: '.dropBox',
    opacity: 0.35,
    scroll: true, 
    scrollSensitivity: 100,
    //handle: '.move',
    helper: 'clone',
    containment:'#gallery',
    accept:'#gallery > .photo',
    revert: true,
    update: function(event, ui){
        params = 'c=' + $(this).attr('id') + '&id=' + ui.item.attr('id');

        $.ajax({
            type: 'POST',
            url: 'processData.php',
            data: params,
            error:function(){
                alert("Error!");
            },
            success:function(data){
                $("#serverResponse").html(data);
            }
        });
    }
}).disableSelection();

Can you help me guys?

Upvotes: 5

Views: 14814

Answers (4)

Anwar Chandra
Anwar Chandra

Reputation: 8648

ui.sender only exists in second callback.

$(".sortable").sortable({
    connectWith: ".sortable",
    update: function (evt, ui) {
        // just ignore the second callback
        if(ui.sender == null){
            // call ajax here
        }
    },
    receive: function (evt, ui) {
        // called after the first 'update'
        // and before the second 'update'
        // ui.sender is always exists here
    }
}).disableSelection();

Upvotes: 3

Ricardo García
Ricardo García

Reputation: 11

Just do this:

  update: function(event, ui) {
            if(ui.sender) {
             // Your actual code
            }
        },

Upvotes: 1

Safran Ali
Safran Ali

Reputation: 4497

Use update, stop and receive events, for example

$(function() {
    position_updated = false; //flag bit

    $(".sortable").sortable({
        connectWith: ".sortable",

        update: function(event, ui) {
            position_updated = !ui.sender; //if no sender, set sortWithin flag to true
        },

        stop: function(event, ui) {
            if (position_updated) {

                //code

                position_updated = false;
            }
        },

        receive: function(event, ui) {
            // code
        }

    }).disableSelection();
});

Upvotes: 9

Arnaud Leymet
Arnaud Leymet

Reputation: 6132

You should try to play with the sortable different events

  • start
  • sort
  • change
  • beforeStop
  • stop
  • update
  • receive
  • remove
  • over
  • out
  • activate
  • deactivate

I'm pretty sure one of'em will be your answer.

Source: http://jqueryui.com/demos/sortable/#event-change

Upvotes: 1

Related Questions