gpt4 book ai didi

javascript - jquery sortable,如何防止列表移动到其他连接列表

转载 作者:行者123 更新时间:2023-11-30 10:44:21 24 4
gpt4 key购买 nike

我有三个列表,它们都与 jquery-ui sortable 相关联,第一个列表是 Backlog works,第二个是 in-Progress,最后一个是 finished。

我想将一个项目从第一个列表移动到第二个列表,然后从第二个列表移动到第三个列表,如果一个项目从第一个列表移动到第三个列表,列表不接受元素。

现在一切都完成了,但我不知道如何防止项目开始在其他连接列表上移动,例如,如果有“a”(或...)类?

$(function(){
$(".tasks").sortable({
connectWith : ".tasks",
handle : "h2",
items : ".task",
opaciry : 0.6,
revert : true,
placeholder : "ui-state-highlight",
forcePlaceholderSize : true,
remove : function(e,obj){
$(".tasks").each(function(){
if($(this).children(".task").length == 0){
$(this).addClass("empty");
} else if($(this).hasClass("empty")) {
$(this).removeClass("empty");
}
});
},
out : function(e,obj){
$(".col").removeClass("red green");
$(".tasks").sortable("enable");
}
});
$("#p .tasks").sortable({
receive : function(e,obj){
obj.item.removeClass("tb").addClass("td");
},
over : function(e,obj){
if(obj.item.hasClass("tb")) {
$("#p").addClass("green");
} else {
$("#p").addClass("red");
$("#p .tasks").sortable("disable");
return false;
}
}
});

});

最佳答案

一种方法是结合使用“接收”回调和“取消”方法来验证任何移动。 jsFiddle

$(".tasks").sortable({
connectWith: ".tasks",
receive: function (event, ui) {
var validMove = function (a, b) {
var changeMatrix = [
[true, true, false],
[true, true, true],
[false, true, true]
];
return changeMatrix[a][b];
},
$lists, endList, startIndex, endIndex;
$lists = $(".tasks");
endList = $(ui.item).closest('.ui-sortable').get(0);

startIndex = $lists.index(ui.sender);
endIndex = $lists.index(endList);

if (!validMove(startIndex, endIndex)) {
$(ui.sender).sortable('cancel');
}
}
}).disableSelection();

在这里,我制作了一个可接受的移动矩阵和一个根据源列表和目标列表的索引进行验证的函数。由于 .sortable() 提供的作为参数的 ui 对象,源列表很容易获得,目标列表需要一些快速遍历。

关于javascript - jquery sortable,如何防止列表移动到其他连接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9282915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com