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

匿名 (未验证) 提交于 2019-12-03 08:39:56

问题:

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.

回答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.



回答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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!