简体   繁体   English

JQuery UI,将Sortable与Droppable相结合

[英]JQuery UI, Combining Sortable with Droppable

I am trying to combine JQuery UI sortable with droppable to create multiple pages for dropping and sorting things. 我正在尝试将JQuery UI 可排序droppable相结合,以创建多个页面来删除和排序事物。 I have setup a blog entry with a stand-along demo here: 我在这里设置了一个带有独立演示的博客条目:

http://whit.info/blog/2009/06/06/jquery-ui-combining-sortable-with-droppable/ http://whit.info/blog/2009/06/06/jquery-ui-combining-sortable-with-droppable/

and here is a jsFiddle: 这是一个jsFiddle:

http://jsfiddle.net/VUuyx/ http://jsfiddle.net/VUuyx/

Note that you can drag to sort the boxes, even into other columns. 请注意,您可以拖动以对框进行排序,甚至可以对其他列进行排序。 You can also click the page buttons to switch pages. 您也可以单击页面按钮切换页面。 My problem lies in combining these two features: 我的问题在于结合这两个特征:

By using droppable , I've allowed the user to drag a box to a page button, the page will then switch, and the user can finish dragging it onto the newly revealed page. 通过使用droppable ,我允许用户将一个框拖到页面按钮,然后页面将切换,用户可以完成将其拖动到新显示的页面上。 The problem is that when the page switches, the first column which appears under the dragged box doesn't have it's over event fire. 问题是当页面切换时,显示在拖动框下面的第一列没有它超过事件触发。 You have to drag to another column, and then back to the first column to get the placeholder to appear. 您必须拖动到另一列,然后返回到第一列以显示占位符。

I'm not sure, but I think I need to somehow clear the events, or fire them manually. 我不确定,但我想我需要以某种方式清除事件,或手动触发它们。 The problem seems to stem from the fact that the dragged box is over the column when it is made visible. 问题似乎源于这样一个事实,即拖动的盒子在可见时位于柱子上方。

Can you assist with this esoteric dilemma? 你能帮助解决这个深奥的困境吗?

Thanks! 谢谢!

Update: 更新:

So I have been considering possible work arounds for this. 所以我一直在考虑可能的工作。 Michal suggested firing the refresh method, which indeed doesn't solve the problem, but made me think about event issues. Michal建议启动刷新方法,这确实无法解决问题,但让我思考事件问题。

It seems that when you mouse away and then back again, the proper events fire. 似乎当你离开然后再回来时,适当的事件就会发生。 Perhaps if I can manually fire the mouseout event for the first column, the reset will allow the mouseover event to fire properly. 也许如果我可以手动触发第一列的mouseout事件,重置将允许mouseover事件正确触发。

I tried this: 我试过这个:

$(".column:first").trigger('mouseout'); 

But I don't think that is the same as sortable 's out event. 但我不认为这与可排序的事件是一样的。 Perhaps I should fire that event? 也许我应该解雇那个事件?

Maybe I'm misunderstanding the problem, but I don't think it has anything to do with the "page switching". 也许我误解了这个问题,但我不认为它与“页面切换”有任何关系。 If you turn on both pages at the same time and try to drag "Box 1" to a position above "Box 4", you'll see that it doesn't trigger that "Box 4"'s Sortable has received the Draggable until you go below "Box 4". 如果您同时打开两个页面并尝试将“Box 1”拖动到“Box 4”上方的位置,您将看到它不会触发“Box 4”的Sortable已收到Draggable直到你去“Box 4” 下面 This doesn't solve your problem, but perhaps will help you look in a better area for the solution. 这并不能解决您的问题,但可能会帮助您寻找更好的解决方案。

See http://jsfiddle.net/nkBNP/7/ for a JSFiddle that demonstrates what I mean. 请参阅http://jsfiddle.net/nkBNP/7/以获取演示我的意思的JSFiddle。

At least for the top down drop in I think the solution is somewhere in setting the box class to draggable and link it to the sortable object. 至少对于自上而下的下降,我认为解决方案是将box类设置为draggable并将其链接到可排序对象。

$(".box").draggable({
    tolerance: 'point',
    connectToSortable: '.column',
    snap:false,
    helper : 'clone',
});

This example creates a duplicate of the box but it does allow me to drag box 1 up to page 2 and slowly drag it down above box 5 and get placed into the top spot. 此示例创建了框的副本,但它允许我将框1拖到第2页,然后慢慢将其拖到框5上方并放入顶部位置。 It is very sensitive though. 但它非常敏感。 You may need to adjust the grids and snap to get it to work consistently for the casual user. 您可能需要调整网格并捕捉以使其为临时用户一致地工作。

I can certainly imagine that a sortable object wouldn't expect something to come down from the top, because it expects things to be sorted from within the container. 我当然可以想象一个可排序的对象不会期望从顶部下来的东西,因为它期望从容器内对事物进行排序。 Droppable on the other hand expects things to enter from any direction. 另一方面,Droppable期望从任何方向进入。

Sortable has an undocumented option refreshPositions which automatically updates the position of your droppables when they are sortable as well. Sortable有一个未记录的选项refreshPositions ,当它们可以排序时,它会自动更新droppables的位置。 This prevents the placeholder from being droppable instead of the actual droppable. 这可以防止占位符被删除而不是实际的可放置。

Yeah adding $('.column').sortable("refresh"); 是的,添加$('。column')。sortable(“refresh”); to end of the show_page(id) function worked for me (only tested in firefox minefield): show_page(id)函数的结束对我有用(仅在firefox minefield中测试过):

function show_page(id) {
    $('.page').removeClass('page_active');
    $('#page_'+id).addClass('page_active');
    $('.column').sortable("refresh");
}

Adding @edtechdev's answer to tolerance: 'intersect produced something I thought might satisfy you, Whit: 添加@edtechdev对tolerance: 'intersect的回答tolerance: 'intersect产生了我认为可能满足你的东西,Whit:

$(".column").sortable({
    connectWith: '.column',
    opacity: 0.4,
    tolerance: 'intersect', //<--changed
    placeholder: 'place_holder',
    helper: function(event, el) {
        var myclone = el.clone();
        $('body').append(myclone);
        return myclone;
    },
}).disableSelection();

// ...

function show_page(id) {
    $('.page').removeClass('page_active');
    $('#page_'+id).addClass('page_active');
    $('#page_'+id).find('.column:first').sortable('refresh'); //<- added
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM