diff --git a/src/views/modules/sys/role-add-or-update.vue b/src/views/modules/sys/role-add-or-update.vue
index a92e22d..c1d57fb 100644
--- a/src/views/modules/sys/role-add-or-update.vue
+++ b/src/views/modules/sys/role-add-or-update.vue
@@ -1,30 +1,56 @@
-
@@ -38,6 +64,7 @@ export default {
data() {
return {
visible: false,
+ filterText: '',
menuList: [],
menuListTreeProps: {
label: 'name',
@@ -73,19 +100,93 @@ export default {
tempKey: -666666 // 临时key, 用于解决tree半选中状态项不能传给后台接口问题. # 待优化
}
},
-
+ watch: {
+ filterText(val) {
+ this.$refs.menuListTree.filter(val);
+ }
+ },
methods: {
-
+ filterNode(value, data) {
+ if (!value) return true;
+ let matchButton = data.buttonList && data.buttonList.some(btn => btn.name.indexOf(value) !== -1);
+ return data.name.indexOf(value) !== -1 || matchButton;
+ },
+ processMenuTree(treeList) {
+ for (let i = 0; i < treeList.length; i++) {
+ let node = treeList[i]
+ this.$set(node, 'buttonList', [])
+ this.$set(node, 'checkedButtons', [])
+ if (node.children && node.children.length > 0) {
+ let normalChildren = []
+ for (let child of node.children) {
+ if (child.specificType === 'button' || child.type === 2) {
+ node.buttonList.push(child)
+ } else {
+ normalChildren.push(child)
+ }
+ }
+ node.children = normalChildren
+ this.processMenuTree(node.children)
+ }
+ }
+ },
+ populateCheckedButtons(treeList, checkedIds, treeCheckedKeys) {
+ for (let node of treeList) {
+ if (checkedIds.includes(Number(node.menuId))) {
+ if (!node.children || node.children.length === 0) {
+ treeCheckedKeys.push(Number(node.menuId))
+ }
+ }
+ if (node.buttonList && node.buttonList.length > 0) {
+ node.checkedButtons = node.buttonList
+ .filter(btn => checkedIds.includes(Number(btn.menuId)))
+ .map(btn => Number(btn.menuId))
+ }
+ if (node.children) {
+ this.populateCheckedButtons(node.children, checkedIds, treeCheckedKeys)
+ }
+ }
+ },
+ handleTreeCheck(data, checkedInfo) {
+ let isChecked = checkedInfo.checkedKeys.includes(data.menuId)
+ this.toggleButtons(data, isChecked)
+ },
+ toggleButtons(node, isChecked) {
+ if (isChecked) {
+ node.checkedButtons = node.buttonList ? node.buttonList.map(b => Number(b.menuId)) : []
+ } else {
+ node.checkedButtons = []
+ }
+ if (node.children) {
+ node.children.forEach(child => this.toggleButtons(child, isChecked))
+ }
+ },
+ handleButtonChange(data) {
+ if (data.checkedButtons.length > 0) {
+ this.$refs.menuListTree.setChecked(data.menuId, true, false)
+ }
+ },
+ getAllCheckedButtons(treeList, result) {
+ for (let node of treeList) {
+ if (node.checkedButtons && node.checkedButtons.length > 0) {
+ result.push(...node.checkedButtons)
+ }
+ if (node.children) {
+ this.getAllCheckedButtons(node.children, result)
+ }
+ }
+ },
init (row) {
let id = row ? row.roleId : 0
- // this.getFunctionButtonList()
this.dataForm.id = id || 0
this.$http({
url: this.$http.adornUrl('/sys/menu/list'),
method: 'get',
params: this.$http.adornParams()
}).then(({data}) => {
- this.menuList = treeDataTranslate(data, 'menuId')
+ let treeData = treeDataTranslate(data, 'menuId')
+ this.processMenuTree(treeData)
+ this.menuList = treeData
}).then(() => {
this.visible = true
this.$nextTick(() => {
@@ -107,10 +208,10 @@ export default {
data.role.menuIdList.splice(idx, data.role.menuIdList.length - idx)
}
let x1 = data.role.menuIdList.map(Number)
+ let treeCheckedKeys = []
+ this.populateCheckedButtons(this.menuList, x1, treeCheckedKeys)
this.$nextTick(() => {
- for (let x1Element of x1) {
- this.$refs.menuListTree.setChecked(x1Element, true, false)
- }
+ this.$refs.menuListTree.setCheckedKeys(treeCheckedKeys)
})
}
})
@@ -122,6 +223,14 @@ export default {
dataFormSubmit () {
this.$refs['dataForm'].validate((valid) => {
if (valid) {
+ let buttonIds = []
+ this.getAllCheckedButtons(this.menuList, buttonIds)
+ let treeKeys = this.$refs.menuListTree.getCheckedKeys()
+ let halfKeys = this.$refs.menuListTree.getHalfCheckedKeys()
+ let finalMenuIds = [...new Set([...treeKeys, ...halfKeys, ...buttonIds])]
+ if (this.tempKey) {
+ finalMenuIds.push(this.tempKey)
+ }
this.$http({
url: this.$http.adornUrl(`/sys/role/${!this.dataForm.id ? 'save' : 'update'}`),
method: 'post',
@@ -129,7 +238,7 @@ export default {
'roleId': this.dataForm.id || undefined,
'roleName': this.dataForm.roleName,
'remark': this.dataForm.remark,
- 'menuIdList': [].concat(this.$refs.menuListTree.getCheckedKeys(), [this.tempKey], this.$refs.menuListTree.getHalfCheckedKeys())
+ 'menuIdList': finalMenuIds
})
}).then(({data}) => {
if (data && data.code === 0) {
@@ -165,8 +274,29 @@ export default {