菜单功能实现
菜单接口封装
菜单管理是一个对菜单树结构的增删改查操作。
提供一个菜单查询接口,查询整颗菜单树形结构。
http/modules/menu.js 添加 findMenuTree 接口。
import axios from '../axios'/*
* 菜单管理模块 */
// 保存export const save = (data) => { return axios({
url: '/menu/save',
method: 'post',
data
})
}// 删除export const batchDelete = (data) => { return axios({
url: '/menu/delete',
method: 'post',
data
})
}// 查找导航菜单树export const findNavTree = (params) => { return axios({
url: '/menu/findNavTree',
method: 'get',
params
})
}// 查找导航菜单树export const findMenuTree = () => { return axios({
url: '/menu/findMenuTree',
method: 'get'
})
}
菜单管理界面
菜单管理界面是使用封装的表格树组件显示菜单结构,并提供增删改查的功能。
Menu.vue
v-loading="loading" element-loading-text="拼命加载中">
prop="id" header-align="center" align="center" width="80" label="ID">
prop="name" header-align="center" treeKey="id" width="150" label="名称">
目录
菜单
按钮
prop="parentName" header-align="center" align="center" width="120" label="上级菜单">
prop="url" header-align="center" align="center" width="150"
:show-overflow-tooltip="true" label="菜单URL">
prop="perms" header-align="center" align="center" width="150"
:show-overflow-tooltip="true" label="授权标识">
prop="orderNum" header-align="center" align="center" label="排序">
fixed="right" header-align="center" align="center" width="150" label="操作">
label-width="80px" :size="size" style="text-align:left;">
{{ type }}
:data="popupTreeData" :props="popupTreeProps" :prop="dataForm.parentName==null?'根节点':dataForm.parentName"
:nodeKey="''+dataForm.parentId" :currentChangeHandle="handleTreeSelectChange">
取消
确定
components:{
PopupTreeInput,
KtButton,
TableTreeColumn,
FaIconTooltip
},
data() { return {
size: 'small',
loading: false,
filters: {
name: ''
},
tableTreeDdata: [],
dialogVisible: false,
menuTypeList: ['目录', '菜单', '按钮'],
dataForm: {
id: 0,
type: 1,
name: '',
parentId: 0,
parentName: '',
url: '',
perms: '',
orderNum: 0,
icon: '',
iconList: []
},
dataRule: {
name: [
{ required: true, message: '菜单名称不能为空', trigger: 'blur' }
],
parentName: [
{ required: true, message: '上级菜单不能为空', trigger: 'change' }
]
},
popupTreeData: [],
popupTreeProps: {
label: 'name',
children: 'children'
}
}
},
methods: { // 获取数据
findTreeData: function () { this.loading = true
this.$api.menu.findMenuTree().then((res) => { this.tableTreeDdata = res.data this.popupTreeData = this.getParentMenuTree(res.data) this.loading = false
})
}, // 获取上级菜单树
getParentMenuTree: function (tableTreeDdata) {
let parent = {
parentId: -1,
name: '根节点',
children: tableTreeDdata
} return [parent]
}, // 显示新增界面
handleAdd: function () { this.dialogVisible = true
this.dataForm = {
id: 0,
type: 1,
typeList: ['目录', '菜单', '按钮'],
name: '',
parentId: 0,
parentName: '',
url: '',
perms: '',
orderNum: 0,
icon: '',
iconList: []
}
}, // 显示编辑界面
handleEdit: function (row) { this.dialogVisible = true
Object.assign(this.dataForm, row);
}, // 删除 handleDelete (row) { this.$confirm('确认删除选中记录吗?', '提示', {
type: 'warning'
}).then(() => {
let params = this.getDeleteIds([], row) this.$api.menu.batchDelete(params).then( res => { this.findTreeData() this.$message({message: '删除成功', type: 'success'})
})
})
}, // 获取删除的包含子菜单的id列表 getDeleteIds (ids, row) {
ids.push({id:row.id}) if(row.children != null) { for(let i=0, len=row.children.length; i
}
} return ids
}, // 菜单树选中 handleTreeSelectChange (data, node) { this.dataForm.parentId = data.id this.dataForm.parentName = data.name
}, // 图标选中 iconActiveHandle (iconName) { this.dataForm.icon = iconName
}, // 表单提交 submitForm () { this.$refs['dataForm'].validate((valid) => { if (valid) { this.$confirm('确认提交吗?', '提示', {}).then(() => { this.editLoading = true
let params = Object.assign({}, this.dataForm) this.$api.menu.save(params).then((res) => { if(res.code == 200) { this.$message({ message: '操作成功', type: 'success' })
} else { this.$message({message: '操作失败, ' + res.msg, type: 'error'})
} this.editLoading = false
this.$refs['dataForm'].resetFields() this.dialogVisible = false
this.findTreeData()
})
})
}
})
}
},
mounted() { this.findTreeData()
}
}
其中对表格树组件进行了简单的封装。
views/Core/TableTreeColumn.vue
{{ scope.row[prop] }}
import isArray from 'lodash/isArray'
export default {
name: 'table-tree-column',
props: {
prop: {
type: String
},
treeKey: {
type: String, default: 'id'
},
parentKey: {
type: String, default: 'parentId'
},
levelKey: {
type: String, default: 'level'
},
childKey: {
type: String, default: 'children'
}
},
methods: {
childStyles (row) { return { 'padding-left': (row[this.levelKey] * 25) + 'px' }
},
iconClasses (row) { return [ !row._expanded ? 'el-icon-caret-right' : 'el-icon-caret-bottom' ]
},
iconStyles (row) { return { 'visibility': this.hasChild(row) ? 'visible' : 'hidden' }
},
hasChild (row) { return (isArray(row[this.childKey]) && row[this.childKey].length >= 1) || false
}, // 切换处理 toggleHandle (index, row) { if (this.hasChild(row)) { var data = this.$parent.store.states.data.slice(0)
data[index]._expanded = !data[index]._expanded if (data[index]._expanded) {
data = data.splice(0, index + 1).concat(row[this.childKey]).concat(data)
} else {
data = this.removeChildNode(data, row[this.treeKey])
} this.$parent.store.commit('setData', data) this.$nextTick(() => { this.$parent.doLayout()
})
}
}, // 移除子节点 removeChildNode (data, parentId) { var parentIds = isArray(parentId) ? parentId : [parentId] if (parentId.length <= 0) { return data
} var ids = [] for (var i = 0; i
ids.push(data.splice(i, 1)[0][this.treeKey])
i--
}
} return this.removeChildNode(data, ids)
}
}
}
测试效果
最终测试效果下图所示。
源码下载