11import { MENU_LIST } from '../mutations'
22
3+ function getMatchMenu ( menuList , currentMenuName ) {
4+ let node = null ; // 获取的子节点
5+ let code = 'label' ; // 匹配的属性
6+ let children = 'children' ; // 包含子节点的属性
7+
8+ /**
9+ * 根据 NodeID 查找当前节点以及父节点
10+ * json 数据源
11+ * nodeId 匹配的值
12+ */
13+ let getNode = ( json , nodeName ) => {
14+ for ( let i = 0 ; i < json . length ; i ++ ) {
15+ let obj = json [ i ] ;
16+ // if (!nodeName) break;
17+ // if (!obj || !obj[code]) continue;
18+ if ( obj [ code ] == nodeName ) {
19+ node = obj ;
20+ break ;
21+ } else {
22+ if ( obj [ children ] ) {
23+ getNode ( obj [ children ] , nodeName ) ;
24+ } else {
25+ continue ;
26+ }
27+ }
28+ }
29+ return node ;
30+ }
31+
32+ return getNode ( menuList , currentMenuName ) ;
33+ } ;
34+
35+
336const state = {
437 list : [
5- { moduleName : '学校管理' , menuList : [ ] } ,
6- { moduleName : '教务管理' , menuList : [
38+ { moduleName : '学校管理' , menuList : [
739 { index : '1' , label : '学生管理' , children : [
8- { index : '1-1' , label : '学生信息' , name : 'demoList' , path : '/demo/list' } , // 注意:必须与路由router的name、path对应
9- { index : '1-2' , label : 'test' , children : [
10- { index : '1-2-1' , label : 'test_1' , name : 'test_1' , path : '/demo/test_1' }
11- ] } ]
12- }
40+ { index : '1-1' , label : '学生信息' , path : '/demo/list' } ,
41+ { index : '1-2' , label : '添加学生' , path : '/demo/add' } ]
42+ } ,
43+ { index : '2' , label : '班级管理' , path : '/demo/test_1' }
1344 ] } ,
14- { moduleName : '教职工管理' , menuList : [
15- { index : '1' , label : '学生管理' , children : [
16- { index : '1-1' , label : 'test_2' , name : 'test_2' , path : '/demo/test_2' } ]
17- }
45+ { moduleName : '教务管理' , menuList : [
46+ { index : '1' , label : '学年学期' , path : '/demo/test_2' }
1847 ] } ,
48+ { moduleName : '教职工管理' , menuList : [ ] } ,
1949 { moduleName : '行政办公' , menuList : [ ] } ,
2050 ] ,
2151 activeModuleName : '' ,
22- activeMenuIndex : '-1'
52+ activeMenuIndex : '-1' ,
53+ activeMenuName : '' ,
2354
2455}
2556
@@ -43,7 +74,16 @@ const mutations = {
4374 // 设置当前菜单的index
4475 [ MENU_LIST . SET_ACTICE_MENU_INDEX ] ( state , menuIndex ) {
4576 state . activeMenuIndex = menuIndex ;
46- } ,
77+ } ,
78+ // 设置当前菜单的name,设置index
79+ [ MENU_LIST . SET_ACTICE_MENU_NAME ] ( state , menuName ) {
80+ state . activeMenuName = menuName ;
81+
82+ // 设置当前菜单的index
83+ let menuList = getters . menuList ( ) ;
84+ let current = getMatchMenu ( menuList , state . activeMenuName ) ;
85+ state . activeMenuIndex = current . index ;
86+ } ,
4787 }
4888
4989export default {
0 commit comments