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

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

效果

代码实现

页面

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)
      }
    },
   
    
  },
}
相关推荐
susu1083018911几秒前
vue3 css的样式如果background没有,如何覆盖有background的样式
前端·css
Ocean☾2 分钟前
前端基础-html-注册界面
前端·算法·html
Rattenking2 分钟前
React 源码学习01 ---- React.Children.map 的实现与应用
javascript·学习·react.js
Dragon Wu4 分钟前
前端 Canvas 绘画 总结
前端
CodeToGym9 分钟前
Webpack性能优化指南:从构建到部署的全方位策略
前端·webpack·性能优化
~甲壳虫10 分钟前
说说webpack中常见的Loader?解决了什么问题?
前端·webpack·node.js
~甲壳虫14 分钟前
说说webpack proxy工作原理?为什么能解决跨域
前端·webpack·node.js
Cwhat15 分钟前
前端性能优化2
前端
熊的猫1 小时前
JS 中的类型 & 类型判断 & 类型转换
前端·javascript·vue.js·chrome·react.js·前端框架·node.js
瑶琴AI前端1 小时前
uniapp组件实现省市区三级联动选择
java·前端·uni-app