jQuery sortable('disable') from start event not entirely working like expected

邮差的信 提交于 2019-12-06 06:08:43

问题


The below code does not fully disable the sortables on the start event. It will add the classes ui-sortable-disabled and ui-state-disabled to the sortable elements, but it doesn't disable the functionality - in other words, the sortables look disabled, but they still accept the dragged item and behave like they are enabled.

var assignedSortables;
var startDrag = function(event, ui) { 
    assignedSortables.each(function() {$(this).sortable('disable');});
};

var stopDrag = function(event, ui) { 
    assignedSortables.each(function() {$(this).sortable('enable');});
};

assignedSortables = $(".my-sortable-containers").sortable({
    connectWith: '.my-sortable-containers',
    start: startDrag,
    stop: stopDrag
});

The reason I want to do this is on drag start is because I might need to disable other connected sortables that already contain the item being dragged (I stripped out the logic in order to simplify). Is this a bug or is there a way around it?


回答1:


I just ran into the same issue. I was able to get the connected sortable that I wanted to disable to disable (for reals) by calling the 'refresh' method on the initiating sortable.

So, inside your start callback would be something like:

$connectedList.sortable('disable');
$(ui.sender).sortable('refresh');

I guess that internally the list grabs the set of connected and non-disabled lists before the start event is triggered and does not check to see if that list changes after start is triggered.




回答2:


I have not checked to see if the jQuery library has "fixed" this since I asked the question, what I did instead was use the mousedown and mouseup events to disable and enable

$(".myDraggableContainer").mousedown(functionToDisableTheCorrectSortables).mouseup(functionToEnableSortables);

Doing it this way does in fact disable the receiving sortables fully



来源:https://stackoverflow.com/questions/1189945/jquery-sortabledisable-from-start-event-not-entirely-working-like-expected

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