我找到了解決辦法,也許不漂亮,但它的工作:
var isValidPlacement = true;
$('#input-table tbody').sortable({
placeholder: {
element: function (currentItem) {
return $('<tr id="placeholder"/>')[0];
},
update: function (container, p) {
return;
}
},
change: function (event, ui) {
var placeholder = document.getElementById('placeholder');
var currentLevel = $(placeholder).data('level');
var prev = $(placeholder).prev();
var next = $(placeholder).next();
var prevLevel = prev ? prev.data('level') : 0;
var nextLevel = next ? next.data('level') : 0;
// If prevLevel > nextLevel
// then whe are at the end of a container
// so we can put an element of any level between those ones
// Else if prevLevel < newtLevel
// then we are just at the begging of a container
// so we can only put an element of level == prevLevel + 1 == nextLevel
// Finally if prevLevel == nextLevel
// then we are in the middle of a container
// so we can only put an element of level == prevLevel == nextLevel
isValidPlacement = (
prevLevel > nextLevel
&& prevLevel >= currentLevel
&& currentLevel >= nextLevel
||
prevLevel <= nextLevel
&& currentLevel === nextLevel);
placeholder.style.visibility = isValidPlacement ? "" : "hidden";
},
stop: function (event, ui) {
if (!isValidPlacement) {
$(this).sortable("cancel");
}
}
}).disableSelection();
其中 '水平' 是hierarchi水平(這裏:類=> 1,家庭=> 2 ...)