<el-table v-if="refreshTable" v-loading="loading" :data="classifyList" row-key="classifyId" :default-expand-all="isExpandAll" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" > <el-table-column label="资源分类名称" prop="classifyName" /> <el-table-column label="操作" align="center" class-name="small-padding fixed-width"> <template slot-scope="scope"> <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['esb:classify:edit']" >修改</el-button> <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)" v-hasPermi="['esb:classify:add']" >新增</el-button> <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['esb:classify:remove']" >删除</el-button> </template> </el-table-column> </el-table>实现树形拖拽以及后台树形数据的存储
时间: 2024-03-15 11:46:49 浏览: 178
监听element-ui table滚动事件的方法
实现树形拖拽和后台树形数据的存储需要进行以下步骤:
1.在表格上添加一个拖拽事件 `@node-drag-start="handleDragStart" @node-drag-over="handleDragOver" @node-drag-enter="handleDragEnter" @node-drag-leave="handleDragLeave" @node-drag-end="handleDragEnd" @node-drop="handleDrop"`。
2.实现拖拽事件的方法,具体实现方式可以参考 Element UI 的文档。在 `handleDrop` 方法中,可以获取到拖拽的节点和目标节点,然后将拖拽的节点移动到目标节点的子节点中。
3.在后台存储树形数据时,可以使用递归的方式将树形数据转换成一个数组,然后将数组存储到数据库中。在获取树形数据时,可以将数组转换成树形数据。
下面是一个简单的代码示例,仅供参考:
```javascript
<template>
<el-table
:data="treeData"
:tree-props="treeProps"
row-key="id"
@node-drag-start="handleDragStart"
@node-drag-over="handleDragOver"
@node-drag-enter="handleDragEnter"
@node-drag-leave="handleDragLeave"
@node-drag-end="handleDragEnd"
@node-drop="handleDrop"
>
<el-table-column prop="name" label="名称"></el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
treeData: [
{
id: 1,
name: "节点1",
children: [
{
id: 2,
name: "节点2",
},
],
},
{
id: 3,
name: "节点3",
},
],
treeProps: {
children: "children",
hasChildren: (data) => data.children && data.children.length > 0,
},
};
},
methods: {
handleDragStart(draggingNode) {
console.log("handleDragStart", draggingNode);
},
handleDragOver(draggingNode, dropNode) {
console.log("handleDragOver", draggingNode, dropNode);
},
handleDragEnter(draggingNode, dropNode) {
console.log("handleDragEnter", draggingNode, dropNode);
},
handleDragLeave(draggingNode, dropNode) {
console.log("handleDragLeave", draggingNode, dropNode);
},
handleDragEnd(draggingNode) {
console.log("handleDragEnd", draggingNode);
},
handleDrop(draggingNode, dropNode, dropType) {
console.log("handleDrop", draggingNode, dropNode, dropType);
if (dropType === "inner") {
draggingNode.parent.children.splice(draggingNode.parent.children.indexOf(draggingNode), 1);
dropNode.children.push(draggingNode);
draggingNode.parent = dropNode;
} else {
const index = dropNode.parent.children.indexOf(dropNode);
draggingNode.parent.children.splice(draggingNode.parent.children.indexOf(draggingNode), 1);
dropNode.parent.children.splice(index + (dropType === "before" ? 0 : 1), 0, draggingNode);
draggingNode.parent = dropNode.parent;
}
},
},
};
</script>
```
阅读全文