jQuery sortable connectWith multiple lists

霸气de小男生 提交于 2019-12-10 17:33:20

问题


I have two lists with 8 list elements within each one. I would like to drag either element into either list, and get the total order of both lists together.

Currently the order is classed as two separate sortable lists:

[0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7]

However I would like it to be (obviously in the order of the elements):

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

The parameter connectWith doesn't seem to be working at all, and I cannot drag elements into other lists.

$(document).ready(function() {
    $('#list-1, #list-2').sortable({
        connectWith: '.group',
        update: function(event, ui) {
            var orders = new Array();
            $('#console').html('<b>posts[id] = pos:</b><br>');
            $('.group li').each(function(i) {
                var order = $(this).index();               
                var id = $(this).attr('data-post-id');
                orders.push(order);
            });
            console.log(orders)
        }
    });

});

I have created a jsFiddle

Could anyone offer any advice to why this isn't working?


回答1:


Your problem is with the float:left on the li items .. you need to add float:left to the containers too (ie the ul) to give them some height

Updated your jsfiddle here

fix was changing your css to

ul { list-style: none; float:left;  }

Update

To get the order try this :

$('.group li').each(function(i) {           
     var id = $(this).data('post-id');
     orders.push(parseInt(id.substr(id.indexOf('-')+1)));
});
console.log(orders)

Note: you should use .data() to store/retrieve data on a node not the attr() method

Working example here



来源:https://stackoverflow.com/questions/9703905/jquery-sortable-connectwith-multiple-lists

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