Prevent drop of list item in JqueryUI sortable

前端 未结 8 2037
情歌与酒
情歌与酒 2020-12-23 22:19

I have two lists #sortable1 and #sortable 2 which are connected sortables, as shown in this example.

You can drag and drop list items from

相关标签:
8条回答
  • 2020-12-23 22:52

    Try this example

    $('#list1').sortable({
        connectWith: 'ul'
    });    
    
    $('#list2').sortable({
        connectWith: 'ul',
        receive: function(ev, ui) {
            if(ui.item.hasClass("number"))
              ui.sender.sortable("cancel");
        }
    });    
    
    0 讨论(0)
  • 2020-12-23 22:57

    Here is my 2 cents. If you don't wish to allow sorting in one of the connected list you can remove it from the containers in the instance object when start even is triggered, so the sorting won't happen at all, and if you need to restore the sorting functionality afterwards, you can recreate the sortable once again or add back removed elements in container array. It would look like this:

        start: function(e, ui){
            if(ui.item.hasClass('uniqToSortableItem')){
                var instance = $(this).sortable( "instance" );
                var newContainers = [];
                instance.containers.forEach((el)=> {
                    if(!el.element.hasClass('sortableToDisable')){
                        newContainers.push(el);
                    } 
                });
                // in case you need to save initial array just attach it to ui.helper
                ui.helper.initialContainersArray = instance.containers;
                instance.containers = newContainers;
            }
        },
        stop: function(e, ui){
            // returning containers array to previous state if necessary
            if(ui.helper.initialContainersArray){
                $(this).sortable( "instance" ).containers = ui.helper.initialContainersArray;
            }
        }
    
    0 讨论(0)
提交回复
热议问题