Prevent drop of list item in JqueryUI sortable

前端 未结 8 2038
情歌与酒
情歌与酒 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:34

    You can use a combination of the beforeStop and sortable('cancel') methods to validate the item being moved. In this example, upon an item being dropped, I check if the item is valid by:

    1. Checking if the item has the class number
    2. and checking if the list item was dropped in list2

    This is slightly more hard-coded that I'd like, so alternatively what you could do is check the parent of the dropped item against this, to check if the lists are different. This means that you could potentially have an item of number in list1 and list2, but they're not interchangeable.

    jsFiddle Example

    $(function() {
        $('ul').sortable({
            connectWith: 'ul',
            beforeStop: function(ev, ui) {
                if ($(ui.item).hasClass('number') && $(ui.placeholder).parent()[0] != this) {
                    $(this).sortable('cancel');
                }
            }
        });        
    });​
    

提交回复
热议问题