使 jQuery 可排序项目可放置到其对等项

发布于 2024-09-05 04:37:01 字数 156 浏览 8 评论 0原文

我有一个可排序的(任务)列表。它有助于确定任务的优先级,因此我想保留此功能。现在我想向功能添加子任务:我希望用户能够将一个任务拖到另一个任务并将其放到那里以将其变成子任务。

将 .draggable() 和 .droppable() 应用于已经可排序的项目没有任何效果。我能做什么?

I have a sortable list (of tasks). It helps prioritize tasks, so I want to keep this functionality. Now I want to add subtasks to the functionality: I want to enable users to drag one task over to another task and drop it there to turn it into a subtask.

Applying .draggable() and .droppable() to items that are already sortable has no effect. What could I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

娇纵 2024-09-12 04:37:01

我整理了一个如何执行此操作的演示...但这可能不是最好的方法。以下是我发现的一些问题:

  • 因为此代码使用占位符来确定要将列表移动到的位置,所以如果您从顶部接近它,则只能将一个项目放入另一个项目中。我确实得到了一个工作版本,您可以在基本项目内的任何位置放置一个项目,但代码太混乱和麻烦。
  • 有时,当另一个列表中的项目被放入一个项目中时,它会被卡住。我不知道为什么,但是当列表组移动到另一个列表时它会变得不卡住。

我确信有一种更好的方法,可以计算列表项的交集(就像可排序脚本一样)。但这是一种快速而肮脏的方法。

$(function() {
    var phTop, container, indx;
    $("#sortable1, #sortable2").sortable({
        connectWith: '.connectedSortable',
        beforeStop: function(e,ui){
            phTop = ui.placeholder.position().top;
            container = ui.placeholder.parent();
            indx = ui.placeholder.index();
        },
        stop: function(e,ui){
            var list = container.find('> li').eq(indx);
            // 15 is a pixel tolerance between the two items (dragging in from the top)
            if ( Math.abs( phTop - ui.position.top ) < 15 ) {
                // prevent list depth > 1
                if (ui.item.find('li').length > 0) { return; }
                // add ul inside of li to make subgroup
                if (!list.find('ul').length) { list.append('<ul></ul>'); }
                ui.item.appendTo( list.find('> ul') );
            }
            container.find('li > ul:empty').remove(); // remove empty subgroups
        }
    }).disableSelection();
});

I put together a demo of how to do this... but it may not be the best method. Here are some problems I've discovered:

  • Because this code uses the placeholder to figure out where you are moving the list, you can only drop an item inside another item if you approach it from the top. I did get a working version where you could drop an item anywhere inside the base item, but the code was just too messy and cumbersome.
  • Sometimes when an item from the other list is dropped in an item, it becomes stuck. I'm not sure why, but it becomes unstuck when the list group is moved to the other list.

I'm sure there is a better method, one that calculates the intersection of list items (just like the sortable script does). But this is a quick and dirty method.

$(function() {
    var phTop, container, indx;
    $("#sortable1, #sortable2").sortable({
        connectWith: '.connectedSortable',
        beforeStop: function(e,ui){
            phTop = ui.placeholder.position().top;
            container = ui.placeholder.parent();
            indx = ui.placeholder.index();
        },
        stop: function(e,ui){
            var list = container.find('> li').eq(indx);
            // 15 is a pixel tolerance between the two items (dragging in from the top)
            if ( Math.abs( phTop - ui.position.top ) < 15 ) {
                // prevent list depth > 1
                if (ui.item.find('li').length > 0) { return; }
                // add ul inside of li to make subgroup
                if (!list.find('ul').length) { list.append('<ul></ul>'); }
                ui.item.appendTo( list.find('> ul') );
            }
            container.find('li > ul:empty').remove(); // remove empty subgroups
        }
    }).disableSelection();
});
故事↓在人 2024-09-12 04:37:01

在我发布问题后,我没有耐心,我决定完全忽略 UI.sortable,从可拖动和可放置构建所需的功能,并使用特殊的 div 作为间隔符,这些间隔符会在拖动时膨胀以方便在任务之间插入。

这在某种程度上是有效的,只不过它的代码要多得多,而且比可排序更容易紧张和容易出错,即使将refreshPositions选项设置为true也是如此。尽管如此,可能还有其他正当理由想要规避 UI.sortable。

在非常简短的人造代码中: $(.taskitem).draggable

revert: invalid

start: 动画间隔符高度从 0 到 5

$(.spacer).droppable

over:动画高度从 5 到 50

out:动画高度回到 5

drop:在 spacer 之后插入可拖动< /code>

找到与draggable索引相同的垫片并沿其移动

After I posted my question I had no patience, and I decided to ignore UI.sortable altogether, building the required functionality from draggable and droppable and using special divs as spacers that would swell up on dragover to facilitate dropping in between tasks.

That worked to some degree, except it's all much more code and it's a lot more jittery and bugprone than sortable, even with the refreshPositions option set to true. Still, there might be other valid reasons to want to circumvent UI.sortable.

In very brief faux code: $(.taskitem).draggable

revert: invalid

start: animate height of spacers from 0 to 5

$(.spacer).droppable

over: animate height from 5 to 50

out: animate height back to 5

drop: insert draggable after spacer

find spacer with same index as draggable and move it along

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文