调用
import RoleTree from './RoleTree'
<el-form-item label="功能权限" prop="funcId">
<RoleTree :type="pageType" :row="rowObj" @callBack="funcChange" :key="key" />
</el-form-item>
funcChange(row) {
this.ruleForm.funcId = row.toString()
this.$refs.ruleForm.validateField("funcId")
},
代码
<template>
<!-- 角色管理权限树组件 -->
<div class="rowTree">
<el-tree
ref="treeEdit"
style="margin-top: 10px"
:data="data"
show-checkbox
:check-strictly="true"
:destroy-on-close="true"
node-key="id"
:props="defaultProps"
@check="editClickTree"
>
<!-- -->
<!-- :default-expanded-keys="[2, 3]"
:default-checked-keys="[5]" -->
</el-tree>
</div>
</template>
<script>
import RoleApi from '@/api/account/roleAccount'
export default {
name: 'RoleTree',
data() {
return {
id: '',
data: [],
mySoleId: '',
selection: [],
funcIdData: [],
currentRow: {},
queryParams: {
page_number: 1,
page_size: 10,
name: ''
},
data: [],
total: null,
defaultProps: {
children: 'funcs',
label: 'name',
},
listLoading: false,
pageType: ''
}
},
mounted() {
this.pageType = this.$parent.$parent.$parent.type
this.id = this.$parent.$parent.$parent.row == {} ? '' : this.$parent.$parent.$parent.row.id
if (this.pageType === 'edit') {
this.loadData(this.id)
} else {
this.functionsList()
}
},
methods: {
loadData(id) {
let _this = this
RoleApi.detail({ id: id }).then((res) => {
if (res.roleId == null && res.id == 3) {
res.roleId = 3
}
if (res.roleId == null && res.id == 2) {
res.roleId = 2
}
this.functionsList(res.roleId)
this.currentRow.id = res.roleId
this.funcIdData = res.idList
this.mySoleId = res.id
setTimeout(() => {
res.idList.forEach((i, n) => {
let node = _this.$refs.treeEdit.getNode(i)
if (node) {
_this.$refs.treeEdit.setChecked(node, true)
}
})
}, 500)
})
},
functionsList(id) {
RoleApi.fun({ roleId: 1 }).then((response) => {
const arr = response.list
if (this.pageType === 'edit') {
const arr = response.list.concat()
for (let i = 0; i < arr.length; i++) {
let children = arr[i].funcs
if (children !== undefined && children !== null) {
for (let j = 0; j < children.length; j++) {
let twoChildren = children[j].funcs
if (twoChildren !== undefined && twoChildren !== null) {
for (let k = 0; k < twoChildren.length; k++) {
}
}
}
}
}
}
this.data = arr
})
},
editClickTree(data, node) {
this.funcIdData = this.checkTree(data, node, 'treeEdit')
this.$emit('callBack', this.funcIdData)
},
checkTree(data, node, treeName) {
const arr = []
let allNodes = this.$refs[treeName].getNode(data)
if (allNodes) {
allNodes.parent &&
this.setParentChecked(allNodes.parent, allNodes.checked, treeName)
}
let checkedNodes = this.$refs[treeName].getCheckedNodes()
if (checkedNodes.length > 0) {
for (let i = 0; i < checkedNodes.length; i++) {
arr.push(checkedNodes[i].id)
}
}
return arr
},
setParentChecked(parent, check, treeName) {
if (!check) {
return
}
this.$refs[treeName].setChecked(parent, check)
if (parent && parent.parent) {
this.setParentChecked(parent.parent, check, treeName)
}
}
},
created() {}
}
</script>
<style lang="scss" scoped>
.rowTree {
height: auto;
max-height: 200px;
width: 100%;
overflow: auto;
}
</style>