如果服务器端更新不起作用,则恢复到可排序的原始位置

问题描述 投票:0回答:1

我的问题是这个问题的重复,但接受的答案包含一个不再有效的小提琴。所以我需要再问一次。

我正在使用 jquery Sortable 将元素从一个列表移动到另一个列表。在移动时,我相应地更新数据库,但如果后端更新失败,我需要将 elem 移回其原始位置。我如何得到这个?

$(".connectedSortable").on("sortreceive", function (event, ui) {
    //fetch the data to the API
    if(data.status==200){
        //fetch was successful
    }else{
        //fetch failed
        //rollback to original position                     
    }
});

我认为我正在寻找的确切代码在小提琴中,但它不再起作用了...... 请注意,我跳过了所有不需要解释问题的代码

javascript jquery jquery-ui
1个回答
0
投票

其实很简单,把cancel放到fail部分即可

$(".connectedSortable").on("sortreceive", function (event, ui) {
    //fetch the data to the API
    if(data.status==200){
        //fetch was successful
    }else{
        //fetch failed
        //rollback to original position
        $('whatever selector you started sortable with').sortable("cancel");            
    }
});

足以回滚操作。

© www.soinside.com 2019 - 2024. All rights reserved.