jQueryUI draggable + sortable bug (Cannot read property 'options' of undefined)

20,600

Solution 1

According to the documentation, Jquery UI Draggable Documentation, you need to set the helper parameter to "clone", for the connectWithSortable functionality to work flawlessly.

Once I did that, it stopped throwing the error.

Updated JSBin

Also a note, draggable doesn't have a 'drop' method in its documentation, so you'll probably have to include the droppable plugin if thats what youre going for.

Lastly, if you have to use clone as the helper method, you'll probably need to add some css to make it run smoother.

Cheers.

Solution 2

I also ran into this problem when building a highly dynamic app using Meteor. It turns out that if you remove the original dragging item (or didn't clone it), you would get this error. This recurred several times in different versions of jQuery, but is now finally fixed in v1.11.0:

http://bugs.jqueryui.com/ticket/6889

Share:
20,600
N.Schipper
Author by

N.Schipper

Updated on July 15, 2022

Comments

  • N.Schipper
    N.Schipper almost 2 years

    My question seems to resemble this question:

    dragging from a sortable list to a drag and drop plugin

    But since there is no answer given to that one i was wondering if anybody could / would be able to figure it out with me. The issue i am having is that i create a draggable div and append this into a div that is made sortable. When i specify any arguments like so:

    $(el).sortable({ ... arguments ... }); 
    

    It causes an error when element is dropped see below, when left empty it strangely works fine and has no issues. The error also prevents any functions to be triggered by the draggable element.

    Uncaught TypeError: Cannot read property 'options' of undefined 
    jquery-ui-1.10.3.custom.js:2204
    
    $.ui.plugin.add.stop                         jquery-ui-1.10.3.custom.js:2204
    $.extend.plugin.call                         jquery-ui-1.10.3.custom.js:284
    $.widget._trigger                            jquery-ui-1.10.3.custom.js:2017
    (anonymous function)                         jquery-ui-1.10.3.custom.js:401
    $.widget._mouseStop                          jquery-ui-1.10.3.custom.js:1702
    (anonymous function)                         jquery-ui-1.10.3.custom.js:401
    $.widget._mouseUp                            jquery-ui-1.10.3.custom.js:957
    (anonymous function)                         jquery-ui-1.10.3.custom.js:401
    $.widget._mouseUp                            jquery-ui-1.10.3.custom.js:1721
    (anonymous function)                         jquery-ui-1.10.3.custom.js:401
    $.widget._mouseDown._mouseUpDelegate         jquery-ui-1.10.3.custom.js:913
    jQuery.event.dispatch                        jquery-1.10.2.js:5095
    jQuery.event.add.elemData.handle             jquery-1.10.2.js:4766
    

    And this is the code where it goes wrong:

    $.ui.plugin.add("draggable", "cursor", {
        start: function() {
            var t = $("body"), o = $(this).data("ui-draggable").options;
            if (t.css("cursor")) {
                o._cursor = t.css("cursor");
            }
            t.css("cursor", o.cursor);
        },
        stop: function() {
            var o = $(this).data("ui-draggable").options;
            if (o._cursor) {
                $("body").css("cursor", o._cursor);
            }
        }
    });
    

    var o = $(this).data("ui-draggable").options; The $(this).data() only contains: Object {id: "c17"}

    Example code:

    $('.draggable').draggable({
      connectToSortable: '.sortable',
      drop: function(){
        console.log('Element dropped');
      }
    });
    
    $('.sortable').sortable({
      update: function(){
         console.log('sortable updated'); 
      }
    });
    

    JSBin example: http://jsbin.com/eHUKuCoL/9/edit?html,js,output Hopefully somebody is able to tell me what the issue is and what the fix for the issue is.

  • N.Schipper
    N.Schipper over 10 years
    Ah, now only have to remove the original element of the clone so it doesn't copy and yes i use currently 3 elements the draggable is renderd in all 3 2 are sortable list one is a droppable field. They are rendered in all 3 areas at start based on information contained within there models.
  • Alex F
    Alex F almost 8 years
    Your advice saved my time. Thanks!