原本想在網上博客找一找解決方法,奈何百度到的結果都不盡人意,思惟邏輯不清,步驟複雜,代碼混亂,找了半天也沒找到一個滿意的,因此乾脆就本身動手寫一個css
可能樣式還不是很好看,這是我我的能力的問題,寫不出來好看的頁面,有好的建議或者好看的demo能夠聯繫博主(tangzedong.programmer@gmail)vue
<!-- 菜單樹 --> <template> <div class="menus-tree"> <el-table ref="menusTable" :row-style="showRow" :data="menusTable" v-bind="$attrs"> <el-table-column prop="title" label="菜單名稱"> <template slot-scope="scope"> <span :class="['level'+scope.row.level]"> <i v-if="scope.row.children" @click="openToggle(scope.row)" :class="[scope.row.open?'fa fa-chevron-down':'fa fa-chevron-right']"></i> </span> {{scope.row.title}} </template> </el-table-column> <el-table-column prop="icon" label="圖標"> <template slot-scope="scope"> <i :class="scope.row.icon"></i> </template> </el-table-column> <el-table-column prop="index" label="路徑"> </el-table-column> <el-table-column prop="operation" label="操做"> <template slot-scope="scope"> <el-button type="text" size="small">增長</el-button> <!-- 判斷下面是否有子菜單,有子菜單不能是有刪除按鈕 --> <el-button v-if="!scope.row.children" type="text" size="small">刪除</el-button> <el-button type="text" size="small">編輯</el-button> </template> </el-table-column> </el-table> </div> </template> <script> import Vue from 'vue'; export default { data() { return { // 菜單樹結構數據 menusTree: [{ id: '1', // 主鍵PK level: '1', // 菜單等級 parentId: '', // 父id icon: 'fa fa-book fa-2', // 菜單圖標 title: '博客管理', //菜單標題 children: [{ id: '4', level: '2', parentId: '1', title: '博客發佈', index: 'blog/edit', }, { id: '5', title: '博客列表', index: '1-2', level: '2', children: [{ id: '9', level: '3', parentId: '5', title: '三次菜單', index: 'blog/edit', }] }, { id: '6', level: '2', title: '博客編輯', index: '1-3', }] }, { id: '2', level: '1', icon: 'fa fa-address-book fa-2', title: '用戶信息', }, { id: '3', level: '1', icon: 'fa fa-list-ul fa-2', title: "系統管理", children: [{ id: '7', level: '2', title: '菜單管理', index: 'system/menu' }] }], defaultProps: { children: 'children', label: 'title' }, // 菜單表格結構數據 menusTable: [] } }, // 初始化函數,賦值,menusTree =>menusTable created() { this.menusTable = this.menusTree; }, methods: { showRow: function (row) { const show = row.row.parent ? row.row.parent._expanded && row.row.parent._show : true; row.row._show = show; return show ? "animation:treeTableShow 1s;-webkit-animation:treeTableShow 1s;" : "display:none;"; }, // 樹節點開關操做 openToggle: function (item) { // 這裏只是展開和關閉樣式的變換方法 Vue.set(item, 'open', !item.open); // 展開的時候,顯示子節點,關閉的時候隱藏子節點 // 遍歷全部的子節點,加入到menuTable中 for (let j = 0; j < this.menusTable.length; j++) { // 找到父節點的id,而後依次把子節點放到數組裏面父節點後面 if (this.menusTable[j].id !== item.id) { continue; } if (item.open) { // open => close console.log(item.children); let menusTable = this.menusTable; item.children.forEach(function (child, index) { menusTable.splice(j + index + 1, 0, child); // 添加子節點 }) } else { this.menusTable.splice(j + 1, item.children.length); // 刪除子節點 } break; } } } } </script> <style scoped> .level1, .level2, .level3 { display: inline-block; width: 20px; } .level1 { margin-left: 5px; } .level2 { margin-left: 20px; } .level3 { margin-left: 35px; } </style>
如今樓主也處於學習探討階段,有很差得地方你們能夠指正,這裏也只是簡單的樹表格的展開關閉功能,其餘功能也會持續完善git
githb地址:https://github.com/dawn-tangzedong/web-manage/tree/mastergithub
有意見或建議或問題均可以直接在下方評論web