树结构添加分组,向上向下添加同级,添加子级

树结构添加分组,向上向下添加同级,添加子级

效果

代码实现

页面

html 复制代码
                <el-tree :data="treeData" :props="defaultProps" :expand-on-click-node="false"
                  :filter-node-method="filterNode" :ref="'tree_' + item.name" :id="'tree' + item.name" node-key="id"
                  :current-node-key="defaultKeys" default-expand-all highlight-current @node-click="handleNodeClick">


                  <span class="custom-tree-node" slot-scope="{ node, data }">
                    <el-input :key="data.id" autofocus size="mini" class=" w-75 text-left" :id="data.id"
                      v-show="data.isEdit" v-model="form.serviceName" @keyup.enter.native="addApiGroup" @blur="addApiGroup"></el-input>
                    <el-tooltip :content="node.label" placement="right-start" transition="el-zoom-in-top" 
                      effect="light">
                      <span v-if="!data.isEdit" class="textLine1 tree-label d-flex align-items-center">
                        <img class="imgAlign-textBottom mr-1"
                          :src="data.isGroup == 1 ? require('../../../assets/images/folder.png') : require('../../../assets/images/code.png')"
                          alt="">
                        <span class=''> {{ node.label }}</span>

                      </span>
                    </el-tooltip>
                    <el-popover v-if="data.isGroup == 1" placement="bottom-start" width="100" trigger="click"
                      id="myPopover">
                      <div>
                        <el-link class="ml-0 mb-2" :underline="false" size="mini" type="info"
                          @click="handleAdd('api', node, data)">
                          <img src="../../../assets/images/code_line.png" alt="" class="mr-2" />新增接口
                        </el-link>
                        <el-popover placement="right-start" width="100" trigger="click">
                          <el-link class="d-block ml-0  mb-2" :underline="false" size="mini" type="info"
                            @click="handleAdd('group', node, data, 'up', $event)">上方添加分组</el-link>
                          <el-link class="d-block mb-2 ml-0" :underline="false" size="mini" type="info"
                            @click="handleAdd('group', node, data, 'down')">下方添加分组</el-link>
                          <el-link class="d-block ml-0" :underline="false" size="mini" type="info"
                            @click="handleAdd('group', node, data, 'child')">添加子分组</el-link>
                          <el-link class="ml-0 mb-2 addGroup" :underline="false" size="mini" type="info"
                            slot="reference">
                            <span>
                              <img src="../../../assets/images/bulletpoint.png" alt="" class="mr-2" />新增分组
                            </span>
                            <img src="../../../assets/images/chevron-right.png" alt="" class="mr-2" />
                          </el-link>
                        </el-popover>

                        <el-link class="d-block ml-0  mb-2" :underline="false" size="mini" type="info"
                          @click="handleEdit('group', node, data)"><img src="../../../assets/images/folder_line.png"
                            alt="" class="mr-2" />编辑分组</el-link>
                        <el-link class="d-block ml-0" :underline="false" size="mini" type="info"
                          @click="handleDelete(node, data)"><img src="../../../assets/images/delete.png" alt=""
                            class="mr-2" />删除</el-link>
                      </div>
                      <el-button class="ml-2" size="mini" icon="el-icon-more" type="text" slot="reference"></el-button>
                    </el-popover>
                  </span>
                </el-tree>

js

javascript 复制代码
export default {
  name: 'AppCenterDetail',
  components: { apiList },
  data() {
    return {
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'label',
      },
    }
  },
  created() {
    
    this.form.id = this.$route.query.appId
    this.appId = this.$route.query.appId
    this.getAppDetail(this.$route.query.appId)
  },
  mounted() {
  },
  methods: {
    // 节点单击事件
    handleNodeClick(data, node) {
      this.form.serviceName = data.label
      this.queryParams.parentId = data.id
      this.defaultKeys = data.id
      let tree = {
        id: 0,
        children: this.treeData
      };

    // 调接口提交
    addApiGroup() {
     
    },
    handleAdd(node, data, pageType){

      const treeDOM = this.$refs['tree_' + this.activeName][0]
      const id = Math.ceil(Math.random() * 100);
      const newData = { id: id, parentId: data ? data.id : 0, label: '', isEdit: true, isNew: true, children: [] }

      this.$set(this.form, 'isGroup', 1)
      if (pageType == 'up') {
        this.$set(this.form, 'upOrder', data.orderNum)
        this.$set(this.form, 'isUp', 0)
        this.$set(this.form, 'orderNum', data.orderNum - 1)
        this.$set(this.form, 'parentId', data ? data.parentId : 0)
        this.$set(this.form, 'upId', data.id)
        treeDOM.insertBefore(newData, node)
        // 聚焦
        setTimeout(() => {

          document.getElementById(newData.id).focus()
        }, 500);
      } else if (pageType == 'down') {
        this.$set(this.form, 'upOrder', data.orderNum)
        this.$set(this.form, 'isUp', 1)
        this.$set(this.form, 'orderNum', data.orderNum + 1)
        this.$set(this.form, 'parentId', data ? data.parentId : 0)

        this.$set(this.form, 'upId', data.id)
        treeDOM.insertAfter(newData, node)
        // 聚焦
        setTimeout(() => {

          document.getElementById(newData.id).focus()
        }, 500);
      } else if (pageType == 'child') {
        if (this.currentNodeLevel >= 4) {
          this.$modal.msgWarning('当前树最多可加四级,已超出')
        } else {
          this.$set(this.form, 'upOrder', data.orderNum)
          this.$set(this.form, 'isUp', 2)
          this.$set(this.form, 'parentId', data.id)
          treeDOM.append(newData, node)
          treeDOM.store.nodesMap[data.id].expanded = true
          // 聚焦
          setTimeout(() => {

            document.getElementById(newData.id).focus()
          }, 500);
        }

      } else {
        this.addGroupVisible = true
        this.$set(this.form, 'parentId', 0)
        this.$set(this.form, 'isUp', 2)
      }
    },
   
    
  },
}
相关推荐
m0_7482552613 分钟前
easyExcel导出大数据量EXCEL文件,前端实现进度条或者遮罩层
前端·excel
长风清留扬33 分钟前
小程序毕业设计-音乐播放器+源码(可播放)下载即用
javascript·小程序·毕业设计·课程设计·毕设·音乐播放器
web147862107231 小时前
C# .Net Web 路由相关配置
前端·c#·.net
m0_748247801 小时前
Flutter Intl包使用指南:实现国际化和本地化
前端·javascript·flutter
飞的肖1 小时前
前端使用 Element Plus架构vue3.0实现图片拖拉拽,后等比压缩,上传到Spring Boot后端
前端·spring boot·架构
漫天转悠1 小时前
VScode中配置ESlint+Prettier详细步骤(图文详情)
vscode·vue
青灯文案11 小时前
前端 HTTP 请求由 Nginx 反向代理和 API 网关到后端服务的流程
前端·nginx·http
m0_748254881 小时前
DataX3.0+DataX-Web部署分布式可视化ETL系统
前端·分布式·etl
ZJ_.1 小时前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps
GIS开发特训营1 小时前
Vue零基础教程|从前端框架到GIS开发系列课程(七)响应式系统介绍
前端·vue.js·前端框架·gis开发·webgis·三维gis