SpringBoot mybatis 实现多级树形菜单的示例代码
程序员文章站
2022-03-17 23:16:13
一、前言iview-admin中提供了 这么一个vue组件可以实现树形菜单,下面小编来提供一下在element-ui中的使用教程(项目见:https://github.com/lison16/v-or...
一、前言
iview-admin中提供了 这么一个vue组件可以实现树形菜单,下面小编来提供一下在element-ui中的使用教程(项目见:https://github.com/lison16/v-org-tree)
小编集成了el-dropdown下拉菜单(鼠标左击显示菜单),和右击自定义菜单,两种方式,效果图如下:
二、使用教程
(1)安装依赖
npm install clipboard npm install v-click-outside-x npm install v-org-tree
(2)引入组件
在main.js文件中引入
import treetable from 'tree-table-vue' import vorgtree from 'v-org-tree'
(3)引入部分js工具方法
在项目目录下 -> src -> directive文件夹中引入如下4个js文件
clipboard.js
import clipboard from 'clipboard' export default { bind: (el, binding) => { const clipboard = new clipboard(el, { text: () => binding.value.value }) el.__success_callback__ = binding.value.success el.__error_callback__ = binding.value.error clipboard.on('success', e => { const callback = el.__success_callback__ callback && callback(e) }) clipboard.on('error', e => { const callback = el.__error_callback__ callback && callback(e) }) el.__clipboard__ = clipboard }, update: (el, binding) => { el.__clipboard__.text = () => binding.value.value el.__success_callback__ = binding.value.success el.__error_callback__ = binding.value.error }, unbind: (el, binding) => { delete el.__success_callback__ delete el.__error_callback__ el.__clipboard__.destroy() delete el.__clipboard__ } }
draggable.js
import { on } from '@/libs/tools' export default { inserted: (el, binding, vnode) => { const triggerdom = document.queryselector(binding.value.trigger) triggerdom.style.cursor = 'move' const bodydom = document.queryselector(binding.value.body) let pagex = 0 let pagey = 0 let transformx = 0 let transformy = 0 let canmove = false const handlemousedown = e => { let transform = /\(.*\)/.exec(bodydom.style.transform) if (transform) { transform = transform[0].slice(1, transform[0].length - 1) const splitxy = transform.split('px, ') transformx = parsefloat(splitxy[0]) transformy = parsefloat(splitxy[1].split('px')[0]) } pagex = e.pagex pagey = e.pagey canmove = true } const handlemousemove = e => { const xoffset = e.pagex - pagex + transformx const yoffset = e.pagey - pagey + transformy if (canmove) bodydom.style.transform = `translate(${xoffset}px, ${yoffset}px)` } const handlemouseup = e => { canmove = false } on(triggerdom, 'mousedown', handlemousedown) on(document, 'mousemove', handlemousemove) on(document, 'mouseup', handlemouseup) }, update: (el, binding, vnode) => { if (!binding.value.recover) return const bodydom = document.queryselector(binding.value.body) bodydom.style.transform = '' } }
directives.js
import draggable from './module/draggable' import clipboard from './module/clipboard' const directives = { draggable, clipboard } export default directives
index.js
import directive from './directives' const importdirective = vue => { /** * 拖拽指令 v-draggable="options" * options = { * trigger: /这里传入作为拖拽触发器的css选择器/, * body: /这里传入需要移动容器的css选择器/, * recover: /拖动结束之后是否恢复到原来的位置/ * } */ vue.directive('draggable', directive.draggable) /** * clipboard指令 v-draggable="options" * options = { * value: /在输入框中使用v-model绑定的值/, * success: /复制成功后的回调/, * error: /复制失败后的回调/ * } */ vue.directive('clipboard', directive.clipboard) } export default importdirective
(4)正式使用v-org-tree组件
在所要使用的地方新增如下几个文件,比如我要写在user-group文件夹中
项目\src\components\org-view下面建立二个文件:
index.js
import orgview from './org-view.vue' export default orgview
org-view.vue
<template> <div ref="dragwrapper" class="org-tree-drag-wrapper" @mousedown="mousedownview" @contextmenu="handledocumentcontextmenu" > <div class="org-tree-wrapper" :style="orgtreestyle"> <v-org-tree v-if="data" :data="data" :node-render="noderender" :expand-all="true" @on-node-click="handlenodeclick" collapsable ></v-org-tree> </div> </div> </template> <script> import { on, off } from '@/directive/module/tools' export default { name: 'orgview', props: { zoomhandled: { type: number, default: 1 }, data: object, menulist: { type: array, default: function () { return [ { key: 'edit', label: '编辑公司' }, { key: 'detail', label: '查看公司' }, { key: 'add', label: '新增子公司' }, { key: 'delete', label: '删除公司' } ] } } }, data () { return { currentcontextmenuid: '', orgtreeoffsetleft: 0, orgtreeoffsettop: 0, initpagex: 0, initpagey: 0, oldmarginleft: 0, oldmargintop: 0, canmove: false } }, computed: { orgtreestyle () { return { transform: `translate(-50%, -50%) scale(${this.zoomhandled}, ${ this.zoomhandled })`, marginleft: `${this.orgtreeoffsetleft}px`, margintop: `${this.orgtreeoffsettop}px` } } }, methods: { handlenodeclick (e, data, expand) { expand() }, closemenu () { this.currentcontextmenuid = '' }, getbgcolor (data) { return this.currentcontextmenuid === data.id ? data.isroot ? '#0d7fe8' : '#5d6c7b' : '' }, noderender (h, data) { return ( <div class={[ 'custom-org-node', data.children && data.children.length ? 'has-children-label' : '' ]} on-mousedown={event => event.stoppropagation()} on-contextmenu={this.contextmenu.bind(this, data)} > {data.label} <dropdown trigger="custom" class="context-menu" visible={this.currentcontextmenuid === data.id} nativeon-click={this.handledropdownclick} on-on-click={this.handlecontextmenuclick.bind(this, data)} style={{ transform: `scale(${1 / this.zoomhandled}, ${1 / this.zoomhandled})` }} v-click-outside={this.closemenu} > <dropdown-menu slot="list"> {this.menulist.map(item => { return ( <dropdown-item name={item.key}>{item.label}</dropdown-item> ) })} </dropdown-menu> </dropdown> </div> ) }, contextmenu (data, $event) { const event = $event || window.event event.preventdefault ? event.preventdefault() : (event.returnvalue = false) this.currentcontextmenuid = data.id }, setdepartmentdata (data) { data.isroot = true this.departmentdata = data }, mousedownview (event) { this.canmove = true this.initpagex = event.pagex this.initpagey = event.pagey this.oldmarginleft = this.orgtreeoffsetleft this.oldmargintop = this.orgtreeoffsettop on(document, 'mousemove', this.mousemoveview) on(document, 'mouseup', this.mouseupview) }, mousemoveview (event) { if (!this.canmove) return const { pagex, pagey } = event this.orgtreeoffsetleft = this.oldmarginleft + pagex - this.initpagex this.orgtreeoffsettop = this.oldmargintop + pagey - this.initpagey }, mouseupview () { this.canmove = false off(document, 'mousemove', this.mousemoveview) off(document, 'mouseup', this.mouseupview) }, handledropdownclick (event) { event.stoppropagation() }, handledocumentcontextmenu () { this.canmove = false }, handlecontextmenuclick (data, key) { this.$emit('on-menu-click', { data, key }) } }, mounted () { on(document, 'contextmenu', this.handledocumentcontextmenu) }, beforedestroy () { off(document, 'contextmenu', this.handledocumentcontextmenu) } } </script> <style> </style>
项目\src\components\zoom-controller下面建立二个文件:
index.js
import zoomcontroller from './zoom-controller' export default zoomcontroller
zoom-controller.vue
<template> <div class="zoom-wrapper"> <button class="zoom-button" @click="scale('down')"> <icon type="md-remove" :size="14" color="#fff"/> </button> <span class="zoom-number">{{ value }}%</span> <button class="zoom-button" @click="scale('up')"> <icon type="md-add" :size="14" color="#fff"/> </button> </div> </template> <script> export default { name: 'zoomcontroller', props: { value: { type: number, default: 100 }, step: { type: number, default: 20 }, min: { type: number, default: 10 }, max: { type: number, default: 200 } }, methods: { scale (type) { const zoom = this.value + (type === 'down' ? -this.step : this.step) if ( (zoom < this.min && type === 'down') || (zoom > this.max && type === 'up') ) { return } this.$emit('input', zoom) } } } </script> <style lang="less"> .trans(@duration) { transition: ~"all @{duration} ease-in"; } .zoom-wrapper { .zoom-button { width: 20px; height: 20px; line-height: 10px; border-radius: 50%; background: rgba(157, 162, 172, 1); box-shadow: 0px 2px 8px 0px rgba(218, 220, 223, 0.7); border: none; cursor: pointer; outline: none; &:active { box-shadow: 0px 0px 2px 2px rgba(218, 220, 223, 0.2) inset; } .trans(0.1s); &:hover { background: #1890ff; .trans(0.1s); } } .zoom-number { color: #657180; padding: 0 8px; display: inline-block; width: 46px; text-align: center; } } </style>
项目\src\view下面建立org.vue文件
<template> <card shadow style="height: 100%;width: 100%;overflow:hidden"> <div class="department-outer"> <div class="zoom-box"> <zoom-controller v-model="zoom" :min="20" :max="200"></zoom-controller> </div> <div class="view-box"> <org-view v-if="data" :data="data" :zoom-handled="zoomhandled" :menulist="menulist" @on-menu-click="handlemenuclick" ></org-view> </div> </div> </card> </template> <script> import { orglist } from '@/api/org' import { layerdialog } from '@/libs/diaglog' import './org.less' const orgview = vue.component('orgview', function (resolve) { require(['/user/org-view'], resolve) }) const zoomcontroller = vue.component('zoomcontroller', function (resolve) { require(['/user/zoom-controller'], resolve) }) export default { name: 'org_tree_page', components: { orgview, zoomcontroller }, data () { return { data: null, zoom: 100, menulist: [ { key: 'add', label: '新增子公司' }, { key: 'edit', label: '编辑公司' }, { key: 'delete', label: '删除公司' } ] } }, computed: { zoomhandled () { return this.zoom / 100 } }, methods: { setdepartmentdata (data) { data.isroot = true return data }, handlemenuclick ({ data, key }) { switch (key) { case 'add': case 'edit': console.log(data) this.showdialog(data, key) break case 'delete': break } }, showdialog (data, key) { data.key = key const option = { id: key + 'savedialog', title: this.$i18n.t(key), width: '600px', height: '550px', url: '/api/org/' + qs.stringify(data, { arrayformat: 'brackets' }) } layerdialog(option) }, getdepartmentdata () { const entity = {} const levels = '0,1,2,3,4,5,6' entity.status = 1 getorglist(entity, 1, 20, levels).then(result => { if (result.data.code === 10000) { let len = 0 const list = result.data.data.list if (list) { len = list.length } var data = { id: 0, label: '阿里巴巴集团', level: 0 } if (len > 0) { data.children = this.formatdata(list, 1, 0) } this.data = data } }) }, formatdata (list, level, pid, pname) { const childrendata = [] for (let i = 0; i < list.length; i++) { const data = {} const item = list[i] if (level === item.level) { data.id = item.id data.label = item.name data.parentname = pname data.level = item.level data.children = this.formatdata(list, level + 1, item.id, item.name) childrendata.push(data) } } return childrendata } }, mounted () { this.getdepartmentdata() } } </script>
接口中核心代码:
list<station> stations = stationservice.listbyentity(station,levellist); list<stationvo> stationvos = new arraylist(); for(station s : stations) { stationvo vo = new stationvo(); vo.setid(s.getid()); vo.setlevel(s.getlevel()); vo.setname(s.getname()); stationvos.add(vo); }
数据库中四个字段
mybatis 配置:
返回的json如下:
{ "data" : { "page" : 1, "pagesize" : 20, "total" : 6, "pages" : 1, "list" : [ { "id" : 1, "name" : "天猫科技服务有限公司", "level" : 1, "status" : 1, }, { "id" : 2, "name" : "淘宝技术服务有限公司", "level" : 1, "status" : 1, }, { "id" : 3, "name" : "聚划算科技服务有限公司", "level" : 1, "status" : 1, }, { "id" : 4, "name" : "菜鸟金服", "level" : 2, "status" : 1, }, { "id" : 5, "name" : "黑鸟网络", "level" : 3, "status" : 1, }, { "id" : 6, "name" : "白鸟 网络", "level" : 3, "status" : 1, } ] }, "message" : "获取成功", "code" : 200 }
到此这篇关于springboot mybatis 实现多级树形菜单的示例代码的文章就介绍到这了,更多相关springboot mybatis多级树形菜单内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!