<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
選單管理是一個對選單樹結構的增刪改查操作。
提供一個選單查詢介面,查詢整顆選單樹形結構。
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) => { url: '/menu/delete', // 查詢導航選單樹 export const findNavTree = (params) => { url: '/menu/findNavTree', method: 'get', params export const findMenuTree = () => { url: '/menu/findMenuTree', method: 'get'
選單管理介面是使用封裝的表格樹元件顯示選單結構,並提供增刪改查的功能。
Menu.vue
<template> <div class="container" style="width:99%;margin-top:-25px;"> <!--工具列--> <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;"> <el-form :inline="true" :model="filters" :size="size"> <el-form-item> <el-input v-model="filters.name" placeholder="名稱"></el-input> </el-form-item> <el-form-item> <kt-button label="查詢" perms="sys:menu:view" type="primary" @click="findTreeData(null)"/> </el-form-item> <el-form-item> <kt-button label="新增" perms="sys:menu:add" type="primary" @click="handleAdd"/> </el-form-item> </el-form> </div> <!--表格樹內容欄--> <el-table :data="tableTreeDdata" stripe size="mini" style="width: 100%;" v-loading="loading" element-loading-text="拼命載入中"> <el-table-column prop="id" header-align="center" align="center" width="80" label="ID"> </el-table-column> <table-tree-column prop="name" header-align="center" treeKey="id" width="150" label="名稱"> </table-tree-column> <el-table-column header-align="center" align="center" label="圖示"> <template slot-scope="scope"> <i :class="scope.row.icon || ''"></i> </template> </el-table-column> <el-table-column prop="type" header-align="center" align="center" label="型別"> <template slot-scope="scope"> <el-tag v-if="scope.row.type === 0" size="small">目錄</el-tag> <el-tag v-else-if="scope.row.type === 1" size="small" type="success">選單</el-tag> <el-tag v-else-if="scope.row.type === 2" size="small" type="info">按鈕</el-tag> </template> </el-table-column> <el-table-column prop="parentName" header-align="center" align="center" width="120" label="上級選單"> </el-table-column> <el-table-column prop="url" header-align="center" align="center" width="150" :show-overflow-tooltip="true" label="選單URL"> </el-table-column> <el-table-column prop="perms" header-align="center" align="center" width="150" :show-overflow-tooltip="true" label="授權標識"> </el-table-column> <el-table-column prop="orderNum" header-align="center" align="center" label="排序"> </el-table-column> <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作"> <template slot-scope="scope"> <kt-button label="修改" perms="sys:menu:edit" @click="handleEdit(scope.row)"/> <kt-button label="刪除" perms="sys:menu:delete" type="danger" @click="handleDelete(scope.row)"/> </template> </el-table-column> </el-table> <!-- 新增修改介面 --> <el-dialog :title="!dataForm.id ? '新增' : '修改'" width="40%" :visible.sync="dialogVisible" :close-on-click-modal="false"> <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="submitForm()" label-width="80px" :size="size" style="text-align:left;"> <el-form-item label="選單型別" prop="type"> <el-radio-group v-model="dataForm.type"> <el-radio v-for="(type, index) in menuTypeList" :label="index" :key="index">{{ type }}</el-radio> </el-radio-group> </el-form-item> <el-form-item :label="menuTypeList[dataForm.type] + '名稱'" prop="name"> <el-input v-model="dataForm.name" :placeholder="menuTypeList[dataForm.type] + '名稱'"></el-input> </el-form-item> <el-form-item label="上級選單" prop="parentName"> <popup-tree-input :data="popupTreeData" :props="popupTreeProps" :prop="dataForm.parentName==null?'根節點':dataForm.parentName" :nodeKey="''+dataForm.parentId" :currentChangeHandle="handleTreeSelectChange"> </popup-tree-input> </el-form-item> <el-form-item v-if="dataForm.type === 1" label="選單路由" prop="url"> <el-input v-model="dataForm.url" placeholder="選單路由"></el-input> </el-form-item> <el-form-item v-if="dataForm.type !== 0" label="授權標識" prop="perms"> <el-input v-model="dataForm.perms" placeholder="如: sys:user:add, sys:user:edit, sys:user:delete"></el-input> </el-form-item> <el-form-item v-if="dataForm.type !== 2" label="排序編號" prop="orderNum"> <el-input-number v-model="dataForm.orderNum" controls-position="right" :min="0" label="排序編號"></el-input-number> </el-form-item> <el-form-item v-if="dataForm.type !== 2" label="選單圖示" prop="icon"> <el-row> <el-col :span="22"> <!-- <el-popover ref="iconListPopover" placement="bottom-start" trigger="click" popper-class="mod-menu__icon-popover"> <div class="mod-menu__icon-list"> <el-button v-for="(item, index) in dataForm.iconList" :key="index" @click="iconActiveHandle(item)" :class="{ 'is-active': item === dataForm.icon }"> <icon-svg :name="item"></icon-svg> </el-button> </div> </el-popover> --> <el-input v-model="dataForm.icon" v-popover:iconListPopover :readonly="true" placeholder="選單圖示名稱(如:fa fa-home fa-lg)" class="icon-list__input"></el-input> </el-col> <el-col :span="2" class="icon-list__tips"> <fa-icon-tooltip /> </el-col> </el-row> </el-form-item> </el-form> <span slot="footer" class="dialog-footer"> <el-button :size="size" @click="dialogVisible = false">取消</el-button> <el-button :size="size" type="primary" @click="submitForm()">確定</el-button> </span> </el-dialog> </div> </template> <script> import KtButton from "@/views/Core/KtButton" import TableTreeColumn from '@/views/Core/TableTreeColumn' import PopupTreeInput from "@/components/PopupTreeInput" import FaIconTooltip from "@/components/FaIconTooltip" export default { 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<len; i++) { this.getDeleteIds(ids, row.children[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() } } </script> <style scoped> </style>
其中對錶格樹元件進行了簡單的封裝。
views/Core/TableTreeColumn.vue
<template> <el-table-column :prop="prop" v-bind="$attrs"> <template slot-scope="scope"> <span @click.prevent="toggleHandle(scope.$index, scope.row)" :style="childStyles(scope.row)"> <i :class="iconClasses(scope.row)" :style="iconStyles(scope.row)"></i> {{ scope.row[prop] }} </span> </template> </el-table-column> </template> <script> 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 < data.length; i++) { if (parentIds.indexOf(data[i][this.parentKey]) !== -1 && parentIds.indexOf(data[i][this.treeKey]) === -1) { ids.push(data.splice(i, 1)[0][this.treeKey]) i-- } } return this.removeChildNode(data, ids) } } } </script>
最終測試效果下圖所示。
後端:https://gitee.com/liuge1988/kitty
前端:https://gitee.com/liuge1988/kitty-ui.git
到此這篇關於Vue + Element UI 實現許可權管理系統之選單功能實現程式碼的文章就介紹到這了,更多相關Vue Element UI 許可權管理系統內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45