废话不多说,直接上代码,代码写在bootstraptreetable初始化结束后,只支持修改父节点,不支持修改排序
//拖拽代码开始
//长按flag
var clickFlag = false;
//拖动flag
var dragFlag = false;
//拖动的节点
var dragElem;
//选中的节点
var selectedElem;
//点击时赋值
$("tr").mousedown(function (e) {
//根节点不允许拖动
if ($(this).find("[name='id']").text() !== "0") {
dragElem = this;
clickFlag = true;
}
})
//松开鼠标开始处理事件
$("tr").mouseup(function () {
clickFlag = false;
dragFlag = false;
//当松开时存在拖拽元素及选中元素时开始一系列操作
if (dragElem && selectedElem) {
var categoryName = $(dragElem).find("[name='categoryName']").text();
var targetCategoryName = $(selectedElem).find("[name='categoryName']").text();
var pId = $(dragElem).find("[name='parentId']").text();
var selectedId = $(dragElem).find("[name='id']").text();
var targetId = $(selectedElem).find("[name='id']").text();
if (pId === targetId) {
//如果当前父ID跟选中节点ID相同,视为无效操作
$(dragElem).css("position", "unset");
$(dragElem).css("background", "unset");
$(selectedElem).css("color", "#676a6c");
dragElem = undefined;
selectedElem = undefined;
} else {
layer.confirm("是否把【"+categoryName+"】切换至【"+targetCategoryName+"】分类下面",function () {
layer.msg('处理中', {
icon: 16
,shade: 0.01
});
$.post('yoururl', {
id:selectedId,
oldParentId: pId,
parentId: targetId
}, function (data) {
layer.closeAll();
dragElem = undefined;
selectedElem = undefined;
});
//更新分类的父ID
//然后刷新列表
},function () {
//取消时重置
$(dragElem).css("position", "unset");
$(dragElem).css("background", "unset");
$(selectedElem).css("color", "#676a6c");
dragElem = undefined;
selectedElem = undefined;
});
}
}
})
$("tr").mouseout(function() {
if (clickFlag) {
$(dragElem).css("background", "#f8ac59");
$(dragElem).css("position", "fixed");
dragFlag = true;
}
});
$('tr').hover(function () {
if (dragFlag&&this!==dragElem){
$("tr").css("color", "#676a6c");
$(this).css("color","red")
selectedElem = this;
}
})
$('tr').mousemove(function(e) {
if (dragFlag){
$(dragElem).offset({
top:e.clientY,
left:e.clientX
});
}
});