user1800581
user1800581

Reputation: 11

Remove item from jQuery-UI sortable on event receive

I want to remove an item from a jQuery-UI sortable list on the receive event. I'm trying something this:

receive: function(event, ui){
    num_of_items = num_of_items+1;                          


    if(num_of_items >= 3)
    {
        ui.item.remove();
        num_of_items = num_of_items -1;
    }
}

This renders the error:

Uncaught TypeError: Cannot read property 'options' of undefined

The item.remove() seems to work fine on any other event, such as beforeStop, stop etc.

Upvotes: 0

Views: 1072

Answers (1)

David
David

Reputation: 56

Remove an item on receive callback isn't a good idea because it can be used for other callbacks. But you can make it no more sortable. Here's my advice :

  • Initialize your sortable with the params items
  • On your receive callback, remove the class you used for the params items to make it no more sortable
  • Calling refresh on your sortable element should be a good idea at this point (maybe it's not necessary). Add a class to that element to find it easily with a selector and hide it (with jQuery also)
  • Then removing an item can be easily deferred safely and be called into a cleanup in your stop or beforeStop callback

Hope it will suit you !

Upvotes: 1

Related Questions