middleground_code_v2/src/components/base/BaseMenuTree/index.vue

296 lines
7.2 KiB
Vue
Raw Normal View History

2024-03-26 11:18:19 +08:00
<!--
* @name: 弹窗配置
* @author: zhangpengcheng
* @date: 2022-08-25
-->
<template>
<!-- 权限设置弹框 -->
<!-- :style="{'margin':isCenter?'auto':''}"> -->
<div v-loading="treeLoading" flex style="margin:auto;height: 100%;">
<div class="menu-i" flex="cross:center main:center">
<div class="menu-i-t" flex="cross:top main:justify">
<el-tree :data="menuData" :check-strictly="true" :default-checked-keys="selectData"
@node-click="handleNodeClick" :expand-on-click-node="false" :default-expand-all="expandAll"
:key="new Date().getTime()" :props="treeProps" style="width: 100%;background-color:white;height: 100%;"
@check-change="checkChange" node-key="id" ref="elTree" :show-checkbox="showCheckbox">
</el-tree>
<div @click="changePcAll" class="checkText" v-if="Allshow">全选</div>
2024-03-26 11:18:19 +08:00
</div>
</div>
<div flex="cross:center main:center" style="width:100%" v-if="isSaveBtn">
<el-button style="width:40%" type="primary" @click="saveMenuUser">保存</el-button>
</div>
2024-03-26 11:18:19 +08:00
</div>
</template>
<script>
// import { queryMenuList, menuListSave } from '@/api/apis/auth'
export default {
props: {
// 是否默认展开所有节点
expandAll: {
type: Boolean,
default: true
2024-03-26 11:18:19 +08:00
},
// 是否显示保存按钮
isSaveBtn: {
type: Boolean,
default: false
2024-03-26 11:18:19 +08:00
},
// 是否显示全选按钮
Allshow: {
type: Boolean,
default: false
2024-03-26 11:18:19 +08:00
},
// 是否显示多选按钮
showCheckbox: {
type: Boolean,
default: false
2024-03-26 11:18:19 +08:00
},
// 是否居中
isCenter: {
type: Boolean,
default: true
2024-03-26 11:18:19 +08:00
},
menuData: {
type: Array,
default: () => {
return []
}
2024-03-26 11:18:19 +08:00
},
setting: {
type: Boolean,
default: false
2024-03-26 11:18:19 +08:00
},
},
data() {
return {
theme: '',
2024-03-26 11:18:19 +08:00
powerDlog: false,
// 菜单数据
// menuData: [],
2024-03-26 11:18:19 +08:00
// 回显选中ids
selectData: [],
// 树状图设置
// treeProps: {
// label: 'menuName',
// children: 'id',
// },
treeProps: {
children: 'children',
label: 'label'
},
2024-03-26 11:18:19 +08:00
// PC菜单全选
checkedAllPc: false,
// 防连点
outing: false,
treeLoading: false,
}
},
created() { },
mounted() {
2024-03-26 11:18:19 +08:00
},
computed: {
defaultTheme() {
return this.$store.state.settings.theme
2024-03-26 11:18:19 +08:00
},
},
watch: {
defaultTheme: {
handler: function (val, oldVal) {
this.theme = val
// document.getElementsByTagName('body')[0].style.setProperty('--active', val)
// let arr = document.getElementsByClassName('.el-tree-node:focus>.el-tree-node__content')
// $('.el-tree-node:focus>.el-tree-node__content').css('color',val)
},
immediate: true,
2024-03-26 11:18:19 +08:00
},
},
methods: {
2024-03-26 11:18:19 +08:00
buttonL(el) {
el.active = !el.active
this.$forceUpdate()
2024-03-26 11:18:19 +08:00
},
setData(id) {
this.selectData.push(id)
2024-03-26 11:18:19 +08:00
},
saveMenuUser() {
let allKeys = this.getKey();
this.$emit('onSaveMenu', allKeys);
2024-03-26 11:18:19 +08:00
},
// 获取选中的key值
getKey() {
return this.$refs.elTree.getCheckedKeys()
2024-03-26 11:18:19 +08:00
},
// 初始数据
initData() {
// 菜单数据
// this.menuData = [];
// 回显选中ids
this.selectData = [];
},
// 关闭弹窗
handleClose() {
this.powerDlog = false
this.initData()
2024-03-26 11:18:19 +08:00
},
// 全选、反选
changePcAll() {
this.checkedAllPc = !this.checkedAllPc;
let selectData = [];
if (this.checkedAllPc) {
selectData = this.cycleData(this.menuData)
2024-03-26 11:18:19 +08:00
}
this.selectData = selectData;
},
/**
* @description 递归获取菜单id树状 多叉树结构
* @author duanyipeng
* @createDate 2020/7/31 20:54
* @param {Array} outData 需要递归的数组
* @param {Boolean} isSelect: false返回所有id,true返回已选择id
*/
cycleData(outData, isSelect) {
let newData = [];
function cycle(data) {
if (!data || data.length == 0) {
return false
2024-03-26 11:18:19 +08:00
} else {
for (var i = 0, len = data.length; i < len; i++) {
let item = data[i]
2024-03-26 11:18:19 +08:00
if (isSelect && item.selected == 1) {
newData.push(item.id)
2024-03-26 11:18:19 +08:00
}
if (!isSelect) {
newData.push(item.id)
2024-03-26 11:18:19 +08:00
}
cycle(item.id)
2024-03-26 11:18:19 +08:00
}
}
}
cycle(outData)
return newData
2024-03-26 11:18:19 +08:00
},
getData(childIds) {
let newData = [];
function cycle(data) {
data.forEach(el => {
childIds.forEach(item => {
2024-03-26 11:18:19 +08:00
if (el.id == item) {
newData.push(el)
2024-03-26 11:18:19 +08:00
}
})
2024-03-26 11:18:19 +08:00
if (el.children != null && el.children && el.children.length) {
cycle(el.children, childIds)
2024-03-26 11:18:19 +08:00
}
})
2024-03-26 11:18:19 +08:00
}
cycle(this.menuData, childIds)
return newData
2024-03-26 11:18:19 +08:00
},
/**
* @description 节点选中状态发生变化时的回调
* @author duanyipeng
* @createDate 2020/7/31 20:53
* @param { Object } nodeDode 当前节点对象
* @param { Boolean } checked 当前节点是否选中
*/
checkChange(nodeDode, checked) {
let getHalfCheckedKeys = this.$refs.elTree.getCheckedKeys().concat(this.$refs.elTree.getHalfCheckedKeys())
2024-03-26 11:18:19 +08:00
// let getHalfCheckedKeys = this.$refs.elTree.getHalfCheckedKeys()
let childIds = getHalfCheckedKeys.length != 0 ? getHalfCheckedKeys : this.selectData
let checkdata = this.getData(childIds)
this.$emit('checkChange', childIds, checkdata)
return
2024-03-26 11:18:19 +08:00
let id = nodeDode.id;
// let childIds = this.cycleData(id)
console.log(id)
2024-03-26 11:18:19 +08:00
// 循环设置子项是否选中
childIds.forEach(item => {
this.$refs.elTree.setChecked(item, checked)
})
2024-03-26 11:18:19 +08:00
},
// 点击事件
handleNodeClick(data) {
this.$emit('handleNodeClick', data);
2024-03-26 11:18:19 +08:00
},
}
}
2024-03-26 11:18:19 +08:00
</script>
<style lang="scss">
// $activeColor:val(--activeColor, "#00aaff");
.el-tree-node__content {
height: 32px !important;
}
.el-tree-node__label {
font-size: 14px !important;
2024-03-26 11:18:19 +08:00
margin-left: 4px;
}
.el-tree-node__content>label.el-checkbox {
2024-03-26 11:18:19 +08:00
transform: scale(1.3);
}
.el-tree-node__content>.el-tree-node__expand-icon {
2024-03-26 11:18:19 +08:00
font-size: 18px;
}
.el-checkbox__label {
font-size: 16px;
}
.checkBox .el-checkbox__inner {
transform: scale(1.3);
}
</style>
<style lang="scss" scoped>
.menu-i {
width: 100%;
// margin-bottom: 24px;
&-f {
color: #52575a;
font-size: 18px;
text-align: center;
margin-bottom: 12px;
}
&-t {
width: 100%;
// height: 450px;
// border: 1px solid #d8d8d8;
overflow-y: auto;
height: 100%;
2024-03-26 11:18:19 +08:00
}
}
.checkBox {
margin-top: 6px;
margin-right: 12px;
}
.checkText {
width: 60px;
height: 30px;
line-height: 30px;
text-align: center;
color: #333;
font-size: 16px;
cursor: pointer;
transition: all 0.3s ease-in-out;
&:hover {
color: #4570fc;
2024-03-26 11:18:19 +08:00
}
}
</style>