在等待ajax更新时禁用jquery可排序的方法?

发布于 2024-12-08 00:33:46 字数 375 浏览 0 评论 0原文

我有一个 jquery 可排序的,带有与更新事件相关的 ajax 回调。 ajax 回调将更新的排序顺序发送回服务器,然后刷新可排序以保证客户端和服务器同步。

我遇到的问题是用户可以在 ajax 调用完成之前启动一个新的可排序事件 - 我想防止这种情况发生。

我尝试做的是在更新事件上禁用可排序,然后在 ajax 调用返回时重新启用它。但是,除非我弄乱了顺序,否则这似乎不起作用 - 当 ajax 调用仍然处于活动状态时,我仍然可以启动新的可排序拖动。

还有其他方法可以防止这种情况吗?我当然可以设置一个全局 javascript 变量,表示“嘿,现在不行,我正在 ajax...”并引用它,但我不确定哪个可排序事件会对此进行检查,或者它将如何杀死可排序单击请求。

想法?

I have a jquery sortable with an ajax callback tied to the update event. The ajax callback sends the updated sort order back to the server and then does a refresh of the sortable to guarantee that the client and server are both in sync.

The problem I am having is that the user can start a new sortable event before the ajax call has completed - I'd like to prevent that.

What I did try doing was disabling the sortable on the update event, and then re-enabling it when the ajax call returned. However unless I messed up the sequence, this didn't seem to work - I can still start a new sortable drag while the ajax call is still active.

Is there any other way to prevent this? I can certainly set a global javascript variable that says, "hey not right now, I'm ajaxing..." and reference it, but I'm not sure what sortable event would check for this, or how it would kill the sortable click request.

Thoughts?

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

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

发布评论

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

评论(3

酸甜透明夹心 2024-12-15 00:33:47

为了更好的用户体验...

您应该取消之前的ajax请求,而不是禁用该界面,这样它就不会覆盖任何新请求。

这使界面保持响应灵敏、流畅且灵活。
它允许用户在等待保存时“改变主意”并重新排序列表。
任何旧请求都会被取消,因此不会覆盖新请求...

    //keep track of ajax request to allow cancellation of requests:
    var ajaxRequest = null;

    $('ul.sortable').sortable({
        containment: 'parent',
        update: function (event, ui) {

            //display your loading anim gif

            //get list of ids in correct order:
            var ids = $(this).sortable('toArray').toString();

            //cancel any previous ajax requests:
            if (ajaxRequest) {
                ajaxRequest.abort();
            }

            //post order to web service:
            ajaxRequest = $.ajax({
                type: 'POST',
                url: 'somewebservice.blah',
                data: ids,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (response) {
                    //saved ok:
                    ajaxRequest = null;

                    //hide your loading anim gif
                }
            });
        }
    });

For a better user experience...

Instead of disabling the interface, you should cancel the previous ajax request so that it doesn't overwrite any new requests.

This keeps the interface responsive, fluid and flexible.
It allows the user to 'change their mind' and reorder the list while waiting for it to save.
Any old requests are cancelled and therefore don't overwrite new requests...

    //keep track of ajax request to allow cancellation of requests:
    var ajaxRequest = null;

    $('ul.sortable').sortable({
        containment: 'parent',
        update: function (event, ui) {

            //display your loading anim gif

            //get list of ids in correct order:
            var ids = $(this).sortable('toArray').toString();

            //cancel any previous ajax requests:
            if (ajaxRequest) {
                ajaxRequest.abort();
            }

            //post order to web service:
            ajaxRequest = $.ajax({
                type: 'POST',
                url: 'somewebservice.blah',
                data: ids,
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (response) {
                    //saved ok:
                    ajaxRequest = null;

                    //hide your loading anim gif
                }
            });
        }
    });
意中人 2024-12-15 00:33:47

您可以简单地在整个列表上覆盖一个透明的绝对定位的 div,这将防止列表上的任何点击/拖动。

在 CSS 中将其设置为 display: none。然后,当您发起 AJAX 调用时,将其设置为 display: block,并在 AJAX 调用完成时将其切换回 display: none

You can simply overlay a transparent absolutely-positioned div over the whole list, which will prevent any clicks/drags on the list.

Set it in your CSS to display: none. Then, when you initiate an AJAX call, set it to display: block, and when your AJAX call completes, switch it back to display: none.

疧_╮線 2024-12-15 00:33:47

您是否尝试设置禁用属性?

$('#sortable').sortable('option', 'disabled', true )

然后在ajax请求之后

$('#sortable').sortable('option', 'disabled', false )

Did you try to set the disabled property ?

$('#sortable').sortable('option', 'disabled', true )

And then after the ajax request

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