ElementUI tree 非异步(增加,删除,上移,下移)

<template>
   <div class="border" style="overflow:auto;height:545px">
    <div class="table_title mb10">菜单树</div>
    <el-tree
      ref="tree"
      :key="key"
      :data="menuTreeData"
      node-key="id"
      :render-content="renderContent"
      :expand-on-click-node="false"
      :default-expanded-keys="defaultExpand"
      @node-click = 'getnodeinfo'
      :props="defaultProps"
    ></el-tree>
  </div>
</template>

<script>
export default {
  name: "menuTree",
  data() {
    return {
      key: 0,
      defaultExpand: [],
      menuTreeData: [],
      defaultProps: {
        children: 'children',
        label: 'title'
      }
    }
  },
  methods: {
    // 增加节点
    append(e,store,node,data) {
      e.stopPropagation() // 阻止冒泡
      var rootid = ''		
		  var addMenu = {
				"title" : "未命名菜单",
				"parentid" : data.id,
				"appid" : data.appid,
        		"rootid" : data.rootid,
			}
      this.loading = true
      // 此处调用添加节点的接口
      QueryPost("menuManage/insert", "传参").then(res => {
        this.loading = false
        const newChild = res.body.data.menuJson
        if (!data.children) {
          this.$set(data, 'children', [])
        }
        data.children.push(newChild)
        // 将新添加的节点信息传递给父组件
        this.$emit('listenToChildEvent',newChild)
      })
    },
    // 节点删除
    nodeDelete(e,node, data) {
      e.stopPropagation() // 阻止冒泡
      const parent = node.parent
      const children = parent.data.children || parent.data
      const index = children.findIndex(d => d.id === data.id)
      if(data.hasOwnProperty('children') && data.children.length > 0){
        this.$message({type: 'warning',message: '存在子节点无法删除!'})
        return
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {confirmButtonText: '确定',cancelButtonText: '取消',type: 'warning',center: true}).then(() => {
        this.loading = true
        // 点击确定后调用删除的接口
        QueryPost("menuManage/delete", " 传参 ").then(res => {
          this.loading = false
          children.splice(index, 1)
          this.$message({type: 'success',message: '删除成功!'})
        }).catch(() => {
          this.$message({type: 'info',message: '已取消删除'})
        })
      })
    },
    // 节点上移
    nodeUp(e,node, data) {
      e.stopPropagation() // 阻止冒泡
      const parent = node.parent
      const children = parent.data.children || parent.data
      const cIndex = children.findIndex(d => d.id === data.id)
      if (cIndex > 0) {
        var targetid = children[cIndex-1].id
      }else {
        this.$message({type: 'warning',message: '已置顶,无法上移!'})
      }
      this.loading = true
      QueryPost("menuManage/move","传参").then(res => {
        this.loading = false
        if ((parent.level == 0 && cIndex !== 0) || (parent.level !== 0 && cIndex !== 0)) {
          const tempChildrenNodex1 = children[cIndex - 1]
          const tempChildrenNodex2 = children[cIndex]
          this.$set(children, cIndex - 1, tempChildrenNodex2)
          this.$set(children, cIndex, tempChildrenNodex1)
          this.defaultExpand[0] = data.id
        }
        this.key++
      })
    },
    // 节点下移
    nodeDown(e,store, node, data) {
      e.stopPropagation() // 阻止冒泡
      const parent = node.parent
      const children = parent.data.children || parent.data
      const cIndex = children.findIndex(d => d.id === data.id)
      const cLength = children.length - 1; // 最边上的节点
      const allLevel = store.data.length - 1; // 树的深度
      if (cIndex < cLength) {
        var targetid = children[cIndex+1].id
      }else {
        this.$message({type: 'warning',message: '已置底,无法下移!'})
      }
      this.loading = false
      QueryPost("menuManage/move", "传参").then(res => {
        this.loading = false
        if ((parent.level === allLevel && cIndex !== cLength) || (parent.level !== allLevel && cIndex !== cLength)) {
          // 父节点相同
          const tempChildrenNodex1 = children[cIndex + 1]
          const tempChildrenNodex2 = children[cIndex];
          this.$set(children, cIndex + 1, tempChildrenNodex2)
          this.$set(children, cIndex, tempChildrenNodex1)
          this.defaultExpand[0] = data.id
        }
        this.key++
      })
    },
    // jsx渲染
    renderContent(h, { node, data, store }) {
      return (
        <span class="custom-tree-node" style="padding-right:15px">
          <span>{data.title}</span>
          <div class="btnbox">
            <i class="el-icon-plus mr10" on-click={(e) => this.append(e,store, node, data)} />
            <i class="el-icon-delete mr10" on-click={(e) => this.nodeDelete(e,node, data)} />
            <i class="el-icon-upload2 mr10" on-click={(e) => this.nodeUp(e,node, data)} />
            <i class="el-icon-download" on-click={(e) => this.nodeDown(e,store, node, data)} />
          </div>
        </span>
      )
    },
    // 向父组件传递节点数据
    getnodeinfo (data) {
      QueryPost("menuManage/queryByMenuId", "传参").then(res => {
        var data = this.lowerKey(res.body.data.data)
        this.$emit('listenToChildEvent',data)
      })
    },
    // json 数据转换为tree格式
    transDate(list,idstr,pidstr,children){  
      var result = [],temp = {};  
      for(var i = 0; i < list.length; i++){  
        temp[list[i][idstr]]=list[i]
      }  
      for(var j=0; j<list.length; j++){  
        var tempVp = temp[list[j][pidstr]];
        if(tempVp){ 
          if(!tempVp[children]) tempVp[children] = [];
          tempVp[children].push(list[j]);
        }else{  
          result.push(list[j]);
        }  
      }  
      return result;
    },
    // json key 转小写
    lowerKey(jsonObj) {
      for (var key in jsonObj) {
        jsonObj[key.toLowerCase()] = jsonObj[key];
        delete(jsonObj[key]);
      }
      return jsonObj;
    },
    // 初始化菜单方法
    init() {
      var datacenter = new commonParams()
      QueryPost("menuManage/query", "传参").then(res => {
        this.menuTreeData = this.transDate(res.body.data.menuJson,'id','parentid','children')
      })
    }
  },
  mounted() {
    // 初始化菜单数据
    this.init()
  }
}
</script>
以下是一个基于 Element UI Tree 的限制只能在同级上移下移的示例代码: ```html <template> <el-tree :data="treeData" :default-expanded-keys="defaultExpandedKeys" :props="treeProps" @node-drop="handleNodeDrop" /> </template> <script> export default { data() { return { treeData: [ { id: 1, label: 'Node 1', children: [ { id: 2, label: 'Node 1-1', }, { id: 3, label: 'Node 1-2', }, ], }, { id: 4, label: 'Node 2', children: [ { id: 5, label: 'Node 2-1', }, { id: 6, label: 'Node 2-2', }, ], }, ], defaultExpandedKeys: [1, 4], treeProps: { label: 'label', children: 'children', }, }; }, methods: { handleNodeDrop(event) { const { node, dropNode, dropType } = event; // 确保仅同级节点可以上移下移 if (node.parent !== dropNode.parent) { event.preventDefault(); return; } // 处理上移 if (dropType === 'before') { const dropNodeIndex = node.parent.children.indexOf(dropNode); const nodeIndex = node.parent.children.indexOf(node); if (nodeIndex < dropNodeIndex) { this.moveNode(node, dropNodeIndex - 1); } else { this.moveNode(node, dropNodeIndex); } } // 处理下移 if (dropType === 'after') { const dropNodeIndex = node.parent.children.indexOf(dropNode); const nodeIndex = node.parent.children.indexOf(node); if (nodeIndex < dropNodeIndex) { this.moveNode(node, dropNodeIndex); } else { this.moveNode(node, dropNodeIndex + 1); } } }, moveNode(node, index) { node.parent.children.splice(node.parent.children.indexOf(node), 1); node.parent.children.splice(index, 0, node); }, }, }; </script> ``` 在上面的示例代码,我们使用了 `@node-drop` 事件来监听拖拽事件,并在方法 `handleNodeDrop` 对节点的拖拽行为进行限制。具体来说,我们在 `handleNodeDrop` 方法添加了以下逻辑: 1. 确保仅同级节点可以上移下移。 2. 处理上移:根据拖拽节点的位置和目标节点的位置,计算出要移动的节点的新位置,然后调用 `moveNode` 方法移动节点。 3. 处理下移:根据拖拽节点的位置和目标节点的位置,计算出要移动的节点的新位置,然后调用 `moveNode` 方法移动节点。 在 `moveNode` 方法,我们使用 `splice` 方法来移动节点,并且使用 `indexOf` 方法来获取节点在其父节点的子节点列表的位置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值