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=" "
- 导入
npm install @xunlei/vue-context-menu --save
- 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;" >
- 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>
- 引入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, // 让菜单显示
}
},
}
- 添加自定义样式
.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> </h4>
<h4>注</h4>
<h4> </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> </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> </h4>
<h4>选</h4>
<h4> </h4><i class="el-icon-arrow-down el-icon--right"></i>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item command="Circle"> 圆 形 </el-dropdown-item>
<el-dropdown-item command="box"> 方 形 </el-dropdown-item>
<el-dropdown-item command="Polygon"> 多 边 形 </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>