关于树形数据

树形数据后台组建

	/**
	 * 重新组建menu导航
	 * @param menuList
	 * @param menus
	 * @param pid
         * @author hous
	 */
	public List<MenuCustom> getMenuTreeLists(List<MenuCustom> menus, Integer pid) throws CustomException{
		List<MenuCustom> children = null;
		List<MenuCustom> menuList = new ArrayList<MenuCustom>();
		for (MenuCustom menu : menus) {
			if(menu.getPid() == pid){
				children = new ArrayList<MenuCustom>();
				children = getMenuTreeLists(menus, menu.getId());
				menu.setChildren(children);
				menuList.add(menu);
			}
		}
		return menuList;
	}

 前台页面显示

    <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
    <div class="collapse navbar-collapse navbar-ex1-collapse">
        <ul class="nav navbar-nav side-nav">
            <li>
                <a href="${pageContext.request.contextPath}/index"><i class="fa fa-fw fa-dashboard"></i> Dashboard</a>
            </li>
            <c:forEach items="${userInfo.menuList}" var="menu" varStatus="s">
            	<c:if test="${!(menu.children eq null)}">
            		<li>
		                <a href="javascript:;" data-toggle="collapse" data-target="#menu${s.index}"><i class="fa fa-fw fa-arrows-v"></i> ${menu.name} <i class="fa fa-fw fa-caret-down"></i></a>
		                <ul id="menu${s.index}" class="collapse">
		                	<c:forEach items="${menu.children}" var="v">
		                    <li>
		                        <a href="${pageContext.request.contextPath}/${v.url}">${v.name}</a>
		                    </li>
		                    </c:forEach>
		                </ul>
		            </li>
            	</c:if>
            	<c:if test="${menu.children eq null}">
	            	<li>
	                	<a href="${pageContext.request.contextPath}/${menu.url}"><i class="fa fa-fw fa-dashboard"></i> ${menu.name}</a>
	            	</li>
            	</c:if>
            </c:forEach>
        </ul>
    </div>
    <!-- /.navbar-collapse -->

 另一种方式树形数据展示

package com.hous;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import com.alibaba.fastjson.JSON;

public class TreeUtils {
    public static void main(String[] args) {
        List<Node> list = new ArrayList<Node>();
        Node n0 = new Node("430000", "430000", "430000", "江苏省", 1);
        
        Node n1 = new Node("430000", "430500", "430500", "南京市", 5);
        Node n2 = new Node("430500", "430521", "430521", "江宁区", 28);
        Node n3 = new Node("430500", "430527", "430527", "玄武区", 27);
        
        Node n4 = new Node("430000", "430100", "430100", "无锡市", 1);
        Node n5 = new Node("430100", "430102", "430102", "江南区", 10);
        list.add(n0);
        list.add(n1);
        list.add(n2);
        list.add(n3);
        list.add(n4);
        list.add(n5);

        Node root = null;
        Map<String, Node> nodeMap = new HashMap<String, Node>();
        for (Node node : list) {
            nodeMap.put(node.getRegId(), node);
        }
        for (Entry<String, Node> entry : nodeMap.entrySet()) {
            Node temp = entry.getValue();
            if (temp.getRegId() == "430000") {
                root = temp;
            } else {
                nodeMap.get(temp.getpRegId()).getChild().add(temp);
            }
        }
        // root.sortChildren();
        System.out.println(JSON.toJSONString(root.getChild()));

    }
}


class Node {
    private List<Node> child = new ArrayList<Node>();
    private String pRegId;
    private String regCode;
    private String regId;
    private String regName;
    private int regOrder;

    public Node(String pRegId, String regCode, String regId, String regName, int regOrder) {
        super();
        this.pRegId = pRegId;
        this.regCode = regCode;
        this.regId = regId;
        this.regName = regName;
        this.regOrder = regOrder;
    }

    public List<Node> getChild() {
        return child;
    }

    public void setChild(List<Node> child) {
        this.child = child;
    }

    public String getpRegId() {
        return pRegId;
    }

    public void setpRegId(String pRegId) {
        this.pRegId = pRegId;
    }

    public String getRegCode() {
        return regCode;
    }

    public void setRegCode(String regCode) {
        this.regCode = regCode;
    }

    public String getRegId() {
        return regId;
    }

    public void setRegId(String regId) {
        this.regId = regId;
    }

    public String getRegName() {
        return regName;
    }

    public void setRegName(String regName) {
        this.regName = regName;
    }

    public int getRegOrder() {
        return regOrder;
    }

    public void setRegOrder(int regOrder) {
        this.regOrder = regOrder;
    }

    // 孩子节点排序
    @SuppressWarnings("unchecked")
    public void sortChildren() {
        // 对本层节点进行排序
        // 可根据不同的排序属性,传入不同的比较器,这里传入ID比较器
        Collections.sort(child, new NodeIDComparator());
        // 对每个节点的下一层节点进行排序
        for (Iterator<Node> it = child.iterator(); it.hasNext();) {
            ((Node) it.next()).sortChildren();
        }
    }
}


class NodeIDComparator implements Comparator {
    // 按照节点编号比较
    public int compare(Object o1, Object o2) {
        int j1 = ((Node) o1).getRegOrder();
        int j2 = ((Node) o2).getRegOrder();
        return (j1 < j2 ? -1 : (j1 == j2 ? 0 : 1));
    }
}

猜你喜欢

转载自shuizhongyue.iteye.com/blog/2253604