SortableJS 保持不可拖动项目固定

发布于 2025-01-12 21:30:38 字数 1293 浏览 4 评论 0原文

我有一个使用 SortableJS 创建的列表,并且有一些无法拖放的项目。下面是它的外观示例:

<ul id="user_selections">
    <li class="no_drag">Fixed</li>
    <li>Item 1</li>
    <li class="no_drag">Fixed</li>
    <li>Item 2</li>
    <li class="no_drag">Fixed</li>
    <li>Item 3</li>
    <li class="no_drag">Fixed</li>
</ul>

no_drag 类是固定的项目。

现在,当我将第 3 项移至第 1 项上方时,结构应如下所示:

<ul id="user_selections">
    <li class="no_drag">Fixed</li>
    <li>Item 3</li>
    <li class="no_drag">Fixed</li>
    <li>Item 1</li>
    <li class="no_drag">Fixed</li>
    <li>Item 2</li>
    <li class="no_drag">Fixed</li>
</ul>

任何人都可以帮助我如何实现这种行为。

这是我尝试过的JS代码:

var el = document.getElementById('user_selections');
if (el) {
    const sortable = Sortable.create(el, {
        filter: '.no_drag',
        onMove(evt, oe) {
            return evt.related.className.indexOf('no_drag') === -1;
        },
        preventOnFilter: false,
        onEnd: function(/**Event*/evt) {
            // Some ajax call to save the new indexes
        }
    });
}

I have a list created using SortableJS and has a few items which cannot be dragged and dropped. Here is an example of how it looks:

<ul id="user_selections">
    <li class="no_drag">Fixed</li>
    <li>Item 1</li>
    <li class="no_drag">Fixed</li>
    <li>Item 2</li>
    <li class="no_drag">Fixed</li>
    <li>Item 3</li>
    <li class="no_drag">Fixed</li>
</ul>

Class no_drag are the items that are fixed.

Now when I move Items 3 above Item 1 the structure should be like this:

<ul id="user_selections">
    <li class="no_drag">Fixed</li>
    <li>Item 3</li>
    <li class="no_drag">Fixed</li>
    <li>Item 1</li>
    <li class="no_drag">Fixed</li>
    <li>Item 2</li>
    <li class="no_drag">Fixed</li>
</ul>

Can anyone please help me how to achieve this kind of behaviour.

Here is the JS code I have tried:

var el = document.getElementById('user_selections');
if (el) {
    const sortable = Sortable.create(el, {
        filter: '.no_drag',
        onMove(evt, oe) {
            return evt.related.className.indexOf('no_drag') === -1;
        },
        preventOnFilter: false,
        onEnd: function(/**Event*/evt) {
            // Some ajax call to save the new indexes
        }
    });
}

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

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

发布评论

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

评论(1

记忆で 2025-01-19 21:30:38

我找到了一个非常适合第一个和最后一个元素的解决方案。
在选项中添加此内容:

onMove: function(e) {
            if(e.related.classList.contains('no_drag')) {
                return false;
            }
        },

如果放置目标是您不想移动的东西,它将返回 false,即它将被忽略为放置目标。

中间的元素也将被忽略,但您仍然可以定位它们之后(或之前)的元素,因此它们将被移动。

I have found a solution that works perfectly for first and last elements.
Add this in options:

onMove: function(e) {
            if(e.related.classList.contains('no_drag')) {
                return false;
            }
        },

If drop target is something you don't want moved it will return false, i.e. it will be ignored as drop target.

Elements in the middle will also be ignored, but you can still target elements after (or before) them, so they will be moved.

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