ElementUI Tree组件相关应用整理

1. 普通tree,子节点中添加自定义属性,单击节点获取节点信息

<template>
  <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
</template>

<script>
  export default {
    name: 'Test',
    data() {
      return {
        data: [{
          "label": "一级",
          "label_en": "yiji",
          "children": [{
              "label": "AAAA",
              "children": [{
                "id": 4,
                "label": "新区县",
                "icon": "el-icon-copy-document",
                "firtype": "sde",
                "label_en": "新区县",
                "order_field": "OBJECTID",
                "coordinate": 4490,
                "color": "rgba(12.0,173.0,102.0,0.7)"
              }]
            },
            {
              "label": "BBBBB",
              "children": [{
                  "id": 5,
                  "label": "二级保护区",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "二级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(236.0,89.0,109.0,0.7)"
                },
                {
                  "id": 6,
                  "label": "二级保护区井号",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "二级保护区井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(109.0,151.0,59.0,0.7)"
                },
                {
                  "id": 7,
                  "label": "三级保护区",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "三级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(72.0,77.0,9.0,0.7)"
                },
                {
                  "id": 8,
                  "label": "三级保护区井号",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "三级保护区井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(240.0,77.0,2.0,0.7)"
                },
                {
                  "id": 9,
                  "label": "集中式水源地保护区",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "沈阳市集中式水源地保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(223.0,174.0,123.0,0.7)"
                },
                {
                  "id": 10,
                  "label": "水源井",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "水源井",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(11.0,81.0,13.0,0.7)"
                },
                {
                  "id": 11,
                  "label": "一级保护区",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "一级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(155.0,170.0,192.0,0.7)"
                },
                {
                  "id": 12,
                  "label": "一级保护区_井号",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "一级保护区_井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(197.0,26.0,63.0,0.7)"
                }
              ]
            },
            {
              "label": "CCCC",
              "children": [{
                "id": 4,
                "label": "三级",
                "icon": "el-icon-copy-document",
                "firtype": "sde",
                "label_en": "新区县",
                "order_field": "OBJECTID",
                "coordinate": 4490,
                "color": "rgba(12.0,173.0,102.0,0.7)"
              }]
            }
          ]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },
    methods: {
      handleNodeClick(data) {
        console.log(data);
      }
    }
  };
</script>

<style>
</style>

在这里插入图片描述

2. 官网上的相关拓展应用

作用代码
显示选择框show-checkbox
点击后高亮highlight-current
初次加载时展开全部default-expand-all
手风琴accordion

在这里插入图片描述

在这里插入图片描述

3. 选择框事件 @check-change=" "

<el-tree :data="data" show-checkbox highlight-current default-expand-all accordion :props="defaultProps" 
    @check-change="handleCheckChange"></el-tree>
handleCheckChange(data, checked, indeterminate) {
        console.log("当前节点:"+data);
        console.log("是否为选中状态"+checked)
      }

4. 添加自定义图标及控制图标颜色

(1). tree中添加

<span class="showname" slot-scope="{ node, data }">
    <i :class="data.icon" v-bind:style="{paddingLeft: '4px',color: data.color}"></i>
    <span style="paddingLeft: 4px" :title="node.label">{{node.label}}</span>
</span>

添加后:

<template>
  <el-tree :data="data" show-checkbox highlight-current default-expand-all :props="defaultProps"
    @check-change="handleCheckChange">
    <span class="showname" slot-scope="{ node, data }">
      <i :class="data.icon" v-bind:style="{paddingLeft: '4px',color: data.color}"></i>
      <span style="paddingLeft: 4px" :title="node.label">{{node.label}}</span>
    </span>
    </el-tree>
</template>

(2). data数据中添加

"icon": "el-icon-copy-document"

"color": "rgba(12.0,173.0,102.0,0.7)"

完整数据:

		data: [{
          "label": "一级",
          "label_en": "yiji",
          "children": [{
              "label": "AAAA",
              "children": [{
                "id": 4,
                "label": "新区县",
                "icon": "el-icon-copy-document",
                "firtype": "sde",
                "label_en": "新区县",
                "order_field": "OBJECTID",
                "coordinate": 4490,
                "color": "rgba(12.0,173.0,102.0,0.7)"
              }]
            },
            {
              "label": "BBBBB",
              "children": [{
                  "id": 5,
                  "label": "二级保护区",
                  "icon": "el-icon-share",
                  "firtype": "sde",
                  "label_en": "二级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(236.0,89.0,109.0,0.7)"
                },
                {
                  "id": 6,
                  "label": "二级保护区井号",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "二级保护区井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(109.0,151.0,59.0,0.7)"
                },
                {
                  "id": 7,
                  "label": "三级保护区",
                  "icon": "el-icon-share",
                  "firtype": "sde",
                  "label_en": "三级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(72.0,77.0,9.0,0.7)"
                },
                {
                  "id": 8,
                  "label": "三级保护区井号",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "三级保护区井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(240.0,77.0,2.0,0.7)"
                },
                {
                  "id": 9,
                  "label": "集中式水源地保护区",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "沈阳市集中式水源地保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(223.0,174.0,123.0,0.7)"
                },
                {
                  "id": 10,
                  "label": "水源井",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "水源井",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(11.0,81.0,13.0,0.7)"
                },
                {
                  "id": 11,
                  "label": "一级保护区",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "一级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(155.0,170.0,192.0,0.7)"
                },
                {
                  "id": 12,
                  "label": "一级保护区_井号",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "一级保护区_井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(197.0,26.0,63.0,0.7)"
                }
              ]
            },
            {
              "label": "CCCC",
              "children": [{
                "id": 4,
                "label": "三级",
                "icon": "el-icon-copy-document",
                "firtype": "sde",
                "label_en": "新区县",
                "order_field": "OBJECTID",
                "coordinate": 4490,
                "color": "rgba(12.0,173.0,102.0,0.7)"
              }]
            }
          ]
        }]

在这里插入图片描述

5. 右击事件,添加右键菜单 @node-contextmenu=" "

  1. 导入 npm install @xunlei/vue-context-menu --save
  2. tree标签中添加右击事件 @node-contextmenu="rightClick"
<el-tree :data="data"  @node-contextmenu="rightClick" show-checkbox node-key="id" ref="tree" highlight-current :props="defaultProps" :filter-node-method="filterNode" default-expand-all style="background-color: #fafafa;margin-left: 10px;" >
  1. tree标签下添加
<el-tree></el-tree>
<vue-context-menu :target="contextMenuTarget" :show="contextMenuVisible" class="right-menu"
        @update:show="(show) => contextMenuVisible = show">
    <a href="javascript:;" @click="createDatabaseOrTable">新建</a>
    <a href="javascript:;" @click="deleteDatabaseOrTable">删除</a>
</vue-context-menu>
  1. 引入VueContextMenu
import { component as VueContextMenu } from '@xunlei/vue-context-menu';

  export default {
    name: 'Test',
    components: {
      'vue-context-menu': VueContextMenu,
    },
    data() {
    	return{
			contextMenuTarget: null,
        	contextMenuVisible: false, // 让菜单显示
		}
    },
}
  1. 添加自定义样式
.right-menu {
    font-size: 14px;
    position: fixed;
    background: #fff;
    border: solid 1px rgba(0, 0, 0, .2);
    border-radius: 3px;
    z-index: 999;
    display: none;
  }
  .right-menu a {
    width: 150px;
    height: 28px;
    line-height: 28px;
    text-align: center;
    display: block;
    color: #1a1a1a;
    padding: 2px;
  }
  .right-menu a:hover {
    background: #bbb;
    color: #fff;
  }
  .right-menu {
    border: 1px solid #eee;
    box-shadow: 0 0.5em 1em 0 rgba(0,0,0,.1);
    border-radius: 1px;
  }
  a {
    text-decoration: none;
  }

完整代码:

<style>
  .right-menu {
      font-size: 14px;
      position: fixed;
      background: #fff;
      border: solid 1px rgba(0, 0, 0, .2);
      border-radius: 3px;
      z-index: 999;
      display: none;
    }
    .right-menu a {
      width: 150px;
      height: 28px;
      line-height: 28px;
      text-align: center;
      display: block;
      color: #1a1a1a;
      padding: 2px;
    }
    .right-menu a:hover {
      background: #bbb;
      color: #fff;
    }
    .right-menu {
      border: 1px solid #eee;
      box-shadow: 0 0.5em 1em 0 rgba(0,0,0,.1);
      border-radius: 1px;
    }
    a {
      text-decoration: none;
    }
</style>

<template>
  <div>
    <el-tree :data="data" show-checkbox highlight-current default-expand-all :props="defaultProps" @check-change="handleCheckChange"
      @node-contextmenu="rightClick">
      <span class="showname" slot-scope="{ node, data }">
        <i :class="data.icon" v-bind:style="{paddingLeft: '4px',color: data.color}"></i>
        <span style="paddingLeft: 4px" :title="node.label">{{node.label}}</span>
      </span>
    </el-tree>
    <vue-context-menu :target="contextMenuTarget" :show="contextMenuVisible" class="right-menu" @update:show="(show) => contextMenuVisible = show">
      <a href="javascript:;" @click="createDatabaseOrTable">新建</a>
      <a href="javascript:;" @click="deleteDatabaseOrTable">删除</a>
    </vue-context-menu>
  </div>
</template>

<script>
  import {
    component as VueContextMenu
  } from '@xunlei/vue-context-menu';

  export default {
    name: 'Test',
    components: {
      'vue-context-menu': VueContextMenu,
    },
    data() {
      return {
        contextMenuTarget: null,
        contextMenuVisible: false, // 让菜单显示
        data: [{
          "label": "一级",
          "label_en": "yiji",
          "children": [{
              "label": "AAAA",
              "children": [{
                "id": 4,
                "label": "新区县",
                "icon": "el-icon-copy-document",
                "firtype": "sde",
                "label_en": "新区县",
                "order_field": "OBJECTID",
                "coordinate": 4490,
                "color": "rgba(12.0,173.0,102.0,0.7)"
              }]
            },
            {
              "label": "BBBBB",
              "children": [{
                  "id": 5,
                  "label": "二级保护区",
                  "icon": "el-icon-share",
                  "firtype": "sde",
                  "label_en": "二级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(236.0,89.0,109.0,0.7)"
                },
                {
                  "id": 6,
                  "label": "二级保护区井号",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "二级保护区井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(109.0,151.0,59.0,0.7)"
                },
                {
                  "id": 7,
                  "label": "三级保护区",
                  "icon": "el-icon-share",
                  "firtype": "sde",
                  "label_en": "三级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(72.0,77.0,9.0,0.7)"
                },
                {
                  "id": 8,
                  "label": "三级保护区井号",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "三级保护区井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(240.0,77.0,2.0,0.7)"
                },
                {
                  "id": 9,
                  "label": "集中式水源地保护区",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "沈阳市集中式水源地保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(223.0,174.0,123.0,0.7)"
                },
                {
                  "id": 10,
                  "label": "水源井",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "水源井",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(11.0,81.0,13.0,0.7)"
                },
                {
                  "id": 11,
                  "label": "一级保护区",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "一级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(155.0,170.0,192.0,0.7)"
                },
                {
                  "id": 12,
                  "label": "一级保护区_井号",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "一级保护区_井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(197.0,26.0,63.0,0.7)"
                }
              ]
            },
            {
              "label": "CCCC",
              "children": [{
                "id": 4,
                "label": "三级",
                "icon": "el-icon-copy-document",
                "firtype": "sde",
                "label_en": "新区县",
                "order_field": "OBJECTID",
                "coordinate": 4490,
                "color": "rgba(12.0,173.0,102.0,0.7)"
              }]
            }
          ]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },
    methods: {
      handleNodeClick(data) {
        console.log(data);
      },
      handleCheckChange(data, checked, indeterminate) {
        console.log("当前节点:" + data);
        console.log("是否为选中状态" + checked)
      },
      rightClick(e, data, node) {
        this.treeNodeData = data; // 存当前数据
        this.treeNode = node; // 存当前节点信息
        console.log('rightClick', this.treeNodeData, this.treeNode);
        this.contextMenuVisible = true; // 让菜单显示
        console.log('0', e, '1', e.screenX, '2', e.screenY);
        const ele = document.querySelector('.right-menu');
        ele.style.position = 'fixed';
        ele.style.top = `${e.clientY}px`;
        ele.style.left = `${e.clientX + 10}px`; //根据鼠标点击位置设置菜单出现
      },
      createDatabaseOrTable() {
        this.contextMenuVisible = false;
        console.log('createDatabaseOrTable', this.treeNodeData, this.treeNode);
      },
      deleteDatabaseOrTable() {
        this.contextMenuVisible = false;
        console.log('deleteDatabaseOrTable', this.treeNodeData, this.treeNode);
      },
    }
  };
</script>

实现效果:
在这里插入图片描述

6.右键菜单,自定义菜单内容,并修改tree高亮样式

效果:

在这里插入图片描述

完整代码:

<style>
  * {
    padding: 0;
    margin: 0;
    border-bottom: 0;
  }

  #dataPage {
    margin: 0 0;
    /* text-align: center; */
    color: #2c3e50;
  }

  .right-menu {
    font-size: 14px;
    position: fixed;
    background: #fff;
    border: solid 1px rgba(0, 0, 0, .2);
    border-radius: 3px;
    z-index: 999;
    display: none;
  }

  .right-menu a {
    width: 150px;
    height: 40px;
    line-height: 28px;
    text-align: center;
    display: block;
    color: #1a1a1a;
    padding: 2px;
  }

  .right-menu a:hover {
    background: #55aaff;
    color: #fff;
  }

  .right-menu {
    border: 1px solid #eee;
    box-shadow: 0 0.5em 1em 0 rgba(0, 0, 0, .1);
    border-radius: 1px;
  }

  a {
    text-decoration: none;
  }
   /* 修改选中高亮颜色 */
  .el-tree-node:focus>.el-tree-node__content {
    background-color: #55aaff !important;
    color: #fff;
  }

  .rightClickStyle {
    height: 40px;
    /* background-color: lavender; */
    display: flex;
    line-height: 40px;
    text-align: center;
    align-items: center;
    justify-content: center;
    color: #606266;
  }

  .shaixuanClass {
    height: 40px;
    text-align: center;
    line-height: 40px;
  }

  .rightClickStyle h4:hover {
    background: #55aaff;
    color: #fff;
  }

  .shaixuanClass :hover {
    background: #55aaff;
    color: #fff;
  }
</style>

<template>
  <div id="dataPage">
    <el-tree id="el-tree" :data="data" show-checkbox highlight-current default-expand-all :props="defaultProps"
      @check-change="handleCheckChange" @node-contextmenu="rightClick">
      <span class="showname" slot-scope="{ node, data }">
        <i :class="data.icon" v-bind:style="{paddingLeft: '4px',color: data.color}"></i>
        <span style="paddingLeft: 4px" :title="node.label">{{node.label}}</span>
      </span>
    </el-tree>
    <vue-context-menu :target="contextMenuTarget" :show="contextMenuVisible" class="right-menu" @update:show="(show) => contextMenuVisible = show">
      <a>
        <div class="shaixuanClass">
          <el-dropdown>
            <span class="el-dropdown-link" style="display: flex;align-items: center;justify-content: center;">
              <h4></h4>
              <h4>&nbsp;&nbsp;&nbsp;&nbsp;</h4>
              <h4></h4>
              <h4>&nbsp;&nbsp;&nbsp;&nbsp;</h4><i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item v-if="nowSelectLayerFieldList" @click.native="selectBiaozhu()"></el-dropdown-item>
                <el-dropdown-item v-if="nowSelectLayerFieldList" v-for="fieldlist in nowSelectLayerFieldList"
                  @click.native="selectBiaozhu(fieldlist.field_en)">{{fieldlist.field_cn == ""?fieldlist.field_en:fieldlist.field_cn}}</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </a>
      <a>
        <div class="rightClickStyle">
          <h4>图层颜色</h4>
          <h4>&nbsp;&nbsp;</h4>
          <el-color-picker size="mini" show-alpha v-model="nowShowLayerColor" @change="changeLayerColor" @active-change="changeActiveLayerColor"></el-color-picker>
        </div>
      </a>
      <a>
        <div class="shaixuanClass">
          <el-dropdown @command="selectoption">
            <span class="el-dropdown-link" style="display: flex;align-items: center;justify-content: center;">
              <h4></h4>
              <h4>&nbsp;&nbsp;&nbsp;&nbsp;</h4>
              <h4></h4>
              <h4>&nbsp;&nbsp;&nbsp;&nbsp;</h4><i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="Circle">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</el-dropdown-item>
                <el-dropdown-item command="box">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</el-dropdown-item>
                <el-dropdown-item command="Polygon">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </a>
    </vue-context-menu>
  </div>
</template>

<script>
  import {
    component as VueContextMenu
  } from '@xunlei/vue-context-menu';

  export default {
    name: 'Test',
    components: {
      'vue-context-menu': VueContextMenu,
    },
    data() {
      return {
        contextMenuTarget: null,
        contextMenuVisible: false, // 让菜单显示
        nowSelectLayerFieldList: [],
        nowShowLayerColor: 'rgba(224, 29, 22, 1)',
        data: [{
          "label": "一级",
          "label_en": "yiji",
          "children": [{
              "label": "AAAA",
              "children": [{
                "id": 4,
                "label": "新区县",
                "icon": "el-icon-copy-document",
                "firtype": "sde",
                "label_en": "新区县",
                "order_field": "OBJECTID",
                "coordinate": 4490,
                "color": "rgba(12.0,173.0,102.0,0.7)"
              }]
            },
            {
              "label": "BBBBB",
              "children": [{
                  "id": 5,
                  "label": "二级保护区",
                  "icon": "el-icon-share",
                  "firtype": "sde",
                  "label_en": "二级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(236.0,89.0,109.0,0.7)"
                },
                {
                  "id": 6,
                  "label": "二级保护区井号",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "二级保护区井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(109.0,151.0,59.0,0.7)"
                },
                {
                  "id": 7,
                  "label": "三级保护区",
                  "icon": "el-icon-share",
                  "firtype": "sde",
                  "label_en": "三级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(72.0,77.0,9.0,0.7)"
                },
                {
                  "id": 8,
                  "label": "三级保护区井号",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "三级保护区井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(240.0,77.0,2.0,0.7)"
                },
                {
                  "id": 9,
                  "label": "集中式水源地保护区",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "沈阳市集中式水源地保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(223.0,174.0,123.0,0.7)"
                },
                {
                  "id": 10,
                  "label": "水源井",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "水源井",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(11.0,81.0,13.0,0.7)"
                },
                {
                  "id": 11,
                  "label": "一级保护区",
                  "icon": "el-icon-copy-document",
                  "firtype": "sde",
                  "label_en": "一级保护区",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(155.0,170.0,192.0,0.7)"
                },
                {
                  "id": 12,
                  "label": "一级保护区_井号",
                  "icon": "el-icon-more",
                  "firtype": "sde",
                  "label_en": "一级保护区_井号",
                  "order_field": "OBJECTID",
                  "coordinate": 4490,
                  "color": "rgba(197.0,26.0,63.0,0.7)"
                }
              ]
            },
            {
              "label": "CCCC",
              "children": [{
                "id": 4,
                "label": "三级",
                "icon": "el-icon-copy-document",
                "firtype": "sde",
                "label_en": "新区县",
                "order_field": "OBJECTID",
                "coordinate": 4490,
                "color": "rgba(12.0,173.0,102.0,0.7)"
              }]
            }
          ]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },
    methods: {
      handleNodeClick(data) {
        console.log(data);
      },
      handleCheckChange(data, checked, indeterminate) {
        console.log("当前节点:" + data);
        console.log("是否为选中状态" + checked)
      },
      rightClick(e, data, node) {
        this.treeNodeData = data; // 存当前数据
        this.treeNode = node; // 存当前节点信息
        console.log('rightClick', this.treeNodeData, this.treeNode);
        this.contextMenuVisible = true; // 让菜单显示
        console.log('0', e, '1', e.screenX, '2', e.screenY);
        const ele = document.querySelector('.right-menu');
        ele.style.position = 'fixed';
        ele.style.top = `${e.clientY}px`;
        ele.style.left = `${e.clientX + 10}px`; //根据鼠标点击位置设置菜单出现
      },
      createDatabaseOrTable() {
        this.contextMenuVisible = false;
        console.log('createDatabaseOrTable', this.treeNodeData, this.treeNode);
      },
      deleteDatabaseOrTable() {
        this.contextMenuVisible = false;
        console.log('deleteDatabaseOrTable', this.treeNodeData, this.treeNode);
      },
      changeLayerColor(e) {
        console.log(e)
      },
      changeActiveLayerColor() {

      },
      selectoption(po) {
        console.log(po)
      }
    }
  };
</script>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: elementui tree组件的回显可以通过以下步骤实现: 1. 在tree组件中设置v-model属性,绑定一个数组,用于存储选中的节点数据。 2. 在mounted生命周期中,通过接口请求获取已选中的节点数据,将数据添加到v-model绑定的数组中。 3. 在tree组件中设置default-checked-keys属性,将已选中的节点的key值传入,实现回显。 4. 当用户进行节点选择操作时,需要将选中的节点数据同步到v-model绑定的数组中,以便在下次回显时能够正确显示已选中的节点。 以上是elementui tree组件回显的基本实现方式,具体实现还需要根据具体业务需求进行调整。 ### 回答2: elementui tree组件是一个常用的树形结构组件,常用于展示页面导航、分类、目录等方式。在开发过程中,经常需要将一些数据以树形结构的方式进行展示,而elementui提供了tree组件来实现这种需求。如果想要实现回显的效果,需要对tree组件进行一些操作。 首先,需要创建一个tree组件,用来展示需要回显的数据。在组件的data属性中定义一个树形结构数组,来保存需要展示的节点信息。同时,定义一个checkedKeys属性,用来保存已选中的节点信息。这样,当tree组件渲染时,会将已选中的节点标记为选中状态,并展示在页面上。 接下来,需要在mounted生命周期方法中,获取需要回显的数据,并将其转化为树形结构数组。然后,将该数组赋值给tree组件的data属性,从而让tree组件展示该数据。 最后,需要将已选中的节点信息设置到checkedKeys属性中,以实现回显效果。可以通过遍历数据数组来获取已选中的节点信息,并将其存储到checkedKeys属性中,从而让tree组件自动选中这些节点。 总之,要实现elementui tree组件的回显效果,需要首先创建一个tree组件,然后在mounted生命周期方法中获取需要回显的数据,并将其转化为树形结构数组。最后,将已选中的节点信息设置到checkedKeys属性中,以实现回显效果。这样就可以在应用中使用elementui tree组件来展示完整的导航、分类、目录等信息,同时满足用户对于回显效果的需求。 ### 回答3: elementui是一个非常实用的UI框架,它提供了很多现成的组件,其中tree组件是一个很常用的组件。在使用tree组件时,我们经常需要根据后端返回的数据来回显tree数据。那么具体如何进行回显呢? 首先,我们需要知道tree组件的数据结构。elementuitree组件数据结构采用树形结构,每个节点都包含一个label字段和一个children字段,children字段是一个数组,可以包含多个层级。例如: ```js { label: '父节点1', children: [ { label: '子节点1' }, { label: '子节点2' } ] } ``` 当我们需要回显树状结构时,我们需要根据后端返回的数据来设置tree组件的数据源(即props中的data)。假设后端返回的数据结构为: ```js [ { id: 1, name: '父节点1', children: [ { id: 2, name: '子节点1' }, { id: 3, name: '子节点2' } ] } ] ``` 我们可以通过递归的方式来构建tree组件的数据源,具体步骤如下: 1. 定义递归函数:我们先定义一个递归函数,用于递归构建树形结构。函数的参数包括数据源(data)和当前节点的父节点(parent)。parent默认值为null,表示当前节点是根节点。函数返回值为一个数组,表示当前节点的所有子节点。 ```js function buildTreeData(data, parent = null) { let treeData = [] data.forEach(item => { let node = { label: item.name, id: item.id, parent: parent } if (item.children && item.children.length > 0) { node.children = buildTreeData(item.children, item.id) } treeData.push(node) }) return treeData } ``` 2. 调用递归函数:在页面中,我们获取后端返回的数据,然后调用递归函数来构建tree组件的数据源。构建完成后,将treeData赋值给tree组件的data属性即可。 ```js <template> <el-tree :data="treeData" /> </template> <script> export default { data() { return { treeData: [] } }, mounted() { // 模拟后端返回的数据 let data = [ { id: 1, name: '父节点1', children: [ { id: 2, name: '子节点1' }, { id: 3, name: '子节点2' } ] } ] // 构建tree组件的数据源 this.treeData = buildTreeData(data) }, methods: { // 定义递归函数 buildTreeData(data, parent = null) { let treeData = [] data.forEach(item => { let node = { label: item.name, id: item.id, parent: parent } if (item.children && item.children.length > 0) { node.children = buildTreeData(item.children, item.id) } treeData.push(node) }) return treeData } } } </script> ``` 通过以上步骤,就可以很方便地完成tree组件的回显。当然,实际使用时还需要根据具体情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值