<el-tree
node-key="id"
default-expand-all
:data="collectionList"
:props="defaultProps"
:expand-on-click-node="false"
:filter-node-method="filterNode"
highlight-current
ref="tree"
style="margin-top: 10px"
@node-drag-end="handleDragEnd"
@node-drop="handleDrop"
draggable
:allow-drop="allowDrop">
handleDrop
事件代码
// 拖拽事件 参数依次为:被拖拽节点对应的 Node、结束拖拽时最后进入的节点、被拖拽节点的放置位置(before、after、inner)、event
handleDrop: async function(draggingNode, dropNode, dropType, ev) {
var paramData = [];
// 当拖拽类型不为inner,说明只是同级或者跨级排序,只需要寻找目标节点的父ID,获取其对象以及所有的子节点,并为子节点设置当前对象的ID为父ID即可
// 当拖拽类型为inner,说明拖拽节点成为了目标节点的子节点,只需要获取目标节点对象即可
var data = dropType != "inner" ? dropNode.parent.data : dropNode.data;
var nodeData = dropNode.level == 1 && dropType != "inner" ? data : data.children;
// 设置父ID,当level为1说明在第一级,pid为0
nodeData.forEach(element => {
element.pid = dropNode.level == 1 ? 0 : data.id;
});
nodeData.forEach((element, i) => {
var collection = {
collectionId: element.id,
parentId: element.pid,
sort: i+1
};
paramData.push(collection);
});
this.loading = true;
updateCollection(JSON.stringify(paramData)).then(response => {
this.msgSuccess("排序成功");
this.loading = false;
});
},
后端接收前端传入的数组数据批量修改即可
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。