1. 程式人生 > >Freemarker如何生成樹形導航選單(遞迴)

Freemarker如何生成樹形導航選單(遞迴)

很多導航選單是樹形的,即一級一級往下分,這樣的結構固然需要用遞迴來處理。 

對於Freemarker 來說,巨集就相當於函式,其定義了簽名及引數列表。 

所以在freemarker頁面應這樣寫:

<#macro menuTree menus> 
  <#if menus?? && menus?size gt 0> 
   <#list menus as menu> 
    <tr> 
     <td> 
      <input type="checkbox" name="ids" value="${menu.id}" /> 
     </td> 
     <td> 
   <span title="${menu.name!}" style="margin-left: ${menu.leaf * 30}px;[#if menu.leaf == 0] color: #000000;[/#if]"> 
   ${menu.menuName!} 
   </span> 
     </td> 
     <td> 
     ${menu.url!} 
     </td> 
     <td> 
     ${menu.permissionText!} 
     </td> 
     <td> 
     ${menu.sortNo!} 
     </td> 
     <td> 
      <a href="edit.jhtml?id=${menu.id}">[編輯]</a> 
     </td> 
    </tr> 
   <#if menu.menuBeans?? && menu.menuBeans?size gt 0> 
    <@menuTree menus = menu.menuBeans/> 
   </#if> 
   </#list> 
  </#if> 
 </#macro> 
 <!-- 呼叫巨集 生成遞迴樹 -->
<@menuTree menus = dto /> 

後臺資料:

[ 
 { 
  "id": 1, 
  "enable": true, 
  "remark": null, 
  "createBy": 1, 
  "createTime": 1503546411000, 
  "updateBy": 1, 
  "updateTime": 1503546415000, 
  "keyword": null, 
  "menuName": "系統管理", 
  "menuType": 1, 
  "parentId": null, 
  "iconcls": null, 
  "url": null, 
  "expand": null, 
  "sortNo": null, 
  "permission": null, 
  "moduleId": 1, 
  "parentName": null, 
  "leaf": 0, 
  "typeName": null, 
  "permissionText": null, 
  "menuBeans": [ 
   { 
    "id": 7, 
    "enable": true, 
    "remark": null, 
    "createBy": 1, 
    "createTime": 1503546411000, 
    "updateBy": 1, 
    "updateTime": 1503546415000, 
    "keyword": null, 
    "menuName": "模組管理", 
    "menuType": 1, 
    "parentId": 1, 
    "iconcls": null, 
    "url": "/module/list", 
    "expand": null, 
    "sortNo": null, 
    "permission": null, 
    "moduleId": 1, 
    "parentName": null, 
    "leaf": 1, 
    "typeName": null, 
    "permissionText": null, 
    "menuBeans": null
   }, 
   { 
    "id": 3, 
    "enable": true, 
    "remark": null, 
    "createBy": 1, 
    "createTime": 1503546411000, 
    "updateBy": 1, 
    "updateTime": 1503546415000, 
    "keyword": null, 
    "menuName": "選單管理", 
    "menuType": 1, 
    "parentId": 1, 
    "iconcls": null, 
    "url": "/menu/list", 
    "expand": null, 
    "sortNo": null, 
    "permission": "admin:add", 
    "moduleId": 1, 
    "parentName": null, 
    "leaf": 1, 
    "typeName": null, 
    "permissionText": null, 
    "menuBeans": null
   } 
  ] 
 }, 
 { 
  "id": 2, 
  "enable": true, 
  "remark": null, 
  "createBy": 1, 
  "createTime": 1503546411000, 
  "updateBy": 1, 
  "updateTime": 1503546415000, 
  "keyword": null, 
  "menuName": "使用者管理", 
  "menuType": 1, 
  "parentId": null, 
  "iconcls": null, 
  "url": null, 
  "expand": null, 
  "sortNo": null, 
  "permission": null, 
  "moduleId": 1, 
  "parentName": null, 
  "leaf": 0, 
  "typeName": null, 
  "permissionText": null, 
  "menuBeans": [ 
   { 
    "id": 9, 
    "enable": true, 
    "remark": null, 
    "createBy": 1, 
    "createTime": 1503546411000, 
    "updateBy": 1, 
    "updateTime": 1503546415000, 
    "keyword": null, 
    "menuName": "供應商管理", 
    "menuType": 1, 
    "parentId": 2, 
    "iconcls": null, 
    "url": null, 
    "expand": null, 
    "sortNo": null, 
    "permission": null, 
    "moduleId": 1, 
    "parentName": null, 
    "leaf": 1, 
    "typeName": null, 
    "permissionText": null, 
    "menuBeans": null
   }, 
   { 
    "id": 8, 
    "enable": true, 
    "remark": null, 
    "createBy": 1, 
    "createTime": 1503546411000, 
    "updateBy": 1, 
    "updateTime": 1503546415000, 
    "keyword": null, 
    "menuName": "部門管理", 
    "menuType": 1, 
    "parentId": 2, 
    "iconcls": null, 
    "url": null, 
    "expand": null, 
    "sortNo": null, 
    "permission": null, 
    "moduleId": 1, 
    "parentName": null, 
    "leaf": 1, 
    "typeName": null, 
    "permissionText": null, 
    "menuBeans": null
   }, 
   { 
    "id": 6, 
    "enable": true, 
    "remark": null, 
    "createBy": 1, 
    "createTime": 1503546411000, 
    "updateBy": 1, 
    "updateTime": 1503546415000, 
    "keyword": null, 
    "menuName": "許可權管理", 
    "menuType": 1, 
    "parentId": 2, 
    "iconcls": null, 
    "url": "/user/list-6", 
    "expand": null, 
    "sortNo": null, 
    "permission": null, 
    "moduleId": 1, 
    "parentName": null, 
    "leaf": 1, 
    "typeName": null, 
    "permissionText": null, 
    "menuBeans": null
   }, 
   { 
    "id": 5, 
    "enable": true, 
    "remark": null, 
    "createBy": 1, 
    "createTime": 1503546411000, 
    "updateBy": 1, 
    "updateTime": 1503546415000, 
    "keyword": null, 
    "menuName": "角色管理", 
    "menuType": 1, 
    "parentId": 2, 
    "iconcls": null, 
    "url": "/user/list-5", 
    "expand": null, 
    "sortNo": null, 
    "permission": null, 
    "moduleId": 1, 
    "parentName": null, 
    "leaf": 1, 
    "typeName": null, 
    "permissionText": null, 
    "menuBeans": null
   }, 
   { 
    "id": 4, 
    "enable": true, 
    "remark": null, 
    "createBy": 1, 
    "createTime": 1503546411000, 
    "updateBy": 1, 
    "updateTime": 1503546415000, 
    "keyword": null, 
    "menuName": "分銷商管理", 
    "menuType": 1, 
    "parentId": 2, 
    "iconcls": null, 
    "url": "/user/add-4", 
    "expand": null, 
    "sortNo": null, 
    "permission": null, 
    "moduleId": 1, 
    "parentName": null, 
    "leaf": 1, 
    "typeName": null, 
    "permissionText": null, 
    "menuBeans": null
   }, 
   { 
    "id": 10, 
    "enable": true, 
    "remark": null, 
    "createBy": 1, 
    "createTime": 1503546411000, 
    "updateBy": 1, 
    "updateTime": 1503546415000, 
    "keyword": null, 
    "menuName": "員工管理", 
    "menuType": 1, 
    "parentId": 2, 
    "iconcls": null, 
    "url": null, 
    "expand": null, 
    "sortNo": null, 
    "permission": null, 
    "moduleId": 1, 
    "parentName": null, 
    "leaf": 1, 
    "typeName": null, 
    "permissionText": null, 
    "menuBeans": null
   } 
  ] 
 } 
]