1. 程式人生 > >javaweb遞迴呼叫封裝樹型結構選單樹(無限層級)

javaweb遞迴呼叫封裝樹型結構選單樹(無限層級)

1.先放一張最終結果圖片,如果你認為這主是你要的結果,那請把接著往下看程式碼 

2.程式碼為封裝成了一個工具類,有需要的小夥伴直接control+c吧

package com.sunkee.business.admin.common.utils;

import com.sunkee.business.admin.common.domain.XTreeGrid;
import com.sunkee.business.admin.common.model.ShiroPermissions;
import com.sunkee.business.make.utils.StringUtil;
import net.sf.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

/**
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 * @developers  LONGZHIQIANG
 * @createtime  2018/11/2 10:36.
 * @describe    用於封裝樹型結構樹(無限層級)
 * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
 */
public class XTreeKit {

    public static List<XTreeGrid> formatTree(List<ShiroPermissions> permissions) {
        List<XTreeGrid> menuList = new ArrayList<XTreeGrid>();
        for (ShiroPermissions ps:permissions) {
            if(ps.getPermParentCode().equals("0")){ //遍歷所有一級節點,並找出所有一級節點下的所有子節點
                XTreeGrid grid = getXTreeGrid(ps, permissions);
                menuList.add(grid);
            }
        }
        return menuList;
    }

    /**
     * 遞迴查詢子選單
     */
    public static List<XTreeGrid> getChild(String id, List<ShiroPermissions> rootMenu) {
        List<XTreeGrid> childList = new ArrayList<>();
        for (ShiroPermissions root : rootMenu) {
            // 遍歷所有節點,將父選單編碼與傳過來的編碼進行比較、若相同則繼續檢視該節點下是否還有子節點
            if (!StringUtil.isEmpty(root.getPermParentCode())) {
                if (root.getPermParentCode().equals(id)) {
                    XTreeGrid grid = getXTreeGrid(root, rootMenu);
                    childList.add(grid);
                }
            }
        }
        return childList;
    }

    //構建 XTreeGrid(實體物件)
    public static XTreeGrid getXTreeGrid(ShiroPermissions ps, List<ShiroPermissions> permissions){
        XTreeGrid x = new XTreeGrid();
        x.setId(ps.getPermId());
        x.setCode(ps.getPermCode());
        x.setParenCode(ps.getPermParentCode());
        x.setTitle(ps.getPermName());
        x.setValue(ps.getPermPermission());
        x.setData(getChild(ps.getPermCode(),permissions));  //遞增遍歷所有子節點(無限層級)
        return x;
    }
}

3.直接呼叫

  /**
     * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
     * @developers  LONGZHIQIANG
     * @createtime  2018/10/31 17:56.
     * @describe    前往新增角色頁面
     * ☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆☆☆★☆☆☆☆
     */
    @RequiresPermissions(PER_ADD)
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(Model model) throws Exception{
        List<ShiroPermissions> permissions = shiroPermissionsService.queryPermissionsAll();
        List<XTreeGrid> grid = XTreeKit.formatTree(permissions);
        model.addAttribute("permissions",JSONArray.fromObject(grid));
        return TABLE_VIEW+"/role-add";
    }

4.java實體類

package com.sunkee.business.admin.common.domain;

import java.io.Serializable;
import java.util.List;
 
/**
 * xtree樹形表格基礎物件
 *
 */
public class XTreeGrid implements Serializable{
 
	private static final long serialVersionUID = -9189631784252440402L;
	
	public Integer id;					//節點id(記錄主健)並不是上下級的ID

	public String code;					//節點code (元素本身編碼用於上下級關連關係)

	public String parenCode;		    //節點父Code(與節點code區分上下級關係)
	
	public String title;				// 顯示的值 (必填 xTree外掛中所需屬性)
	
	public String value;				// 隱藏的值 (必填 xTree外掛中所需屬性)
	
	public Boolean checked = false;	    //預設是否選中(選填 xTree外掛中所需屬性) true為選中,false與不填都為不選中
	
	public Boolean disabled = false;    //是否可用   (選填 xTree外掛中所需屬性)   true為不可用,false與不填都為可用
	
	public Boolean expanded = false;   //是否展開    (選填 xTree外掛中所需屬性)true為展開,預設為不展開
	
	public List<XTreeGrid> data;		//孩子節點   (必填 xTree外掛中所需屬性 沒子節點該值為空集合)
 
 
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getParenCode() {
		return parenCode;
	}

	public void setParenCode(String parenCode) {
		this.parenCode = parenCode;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public Boolean getChecked() {
		return checked;
	}

	public void setChecked(Boolean checked) {
		this.checked = checked;
	}

	public Boolean getDisabled() {
		return disabled;
	}

	public void setDisabled(Boolean disabled) {
		this.disabled = disabled;
	}

	public Boolean getExpanded() {
		return expanded;
	}

	public void setExpanded(Boolean expanded) {
		this.expanded = expanded;
	}

	public List<XTreeGrid> getData() {
		return data;
	}

	public void setData(List<XTreeGrid> data) {
		this.data = data;
	}
}

以上就是所有程式碼,是不是很簡單啊