`

java生成目录结构

    博客分类:
  • java
阅读更多

1.由栈方法

Java代码 复制代码 收藏代码
  1. class MenuSort {   
  2.         private List<SysConstants> list = null;   
  3.         final String TOP_CODE = "005001036";   
  4.   
  5.         public MenuSort(List<SysConstants> list) {   
  6.             this.list = list;   
  7.         }   
  8.   
  9.         /*  
  10.          * 排序  
  11.          */  
  12.         public List <SysConstants> sort() {   
  13.             if (list != null && !list.isEmpty()) {   
  14.                 List <SysConstants> values = new ArrayList<SysConstants>(list.size());   
  15.                 Map<String, List<SysConstants>> bucket = in();                 
  16.                 out(values, bucket, TOP_CODE);   
  17.                 return values;   
  18.             }   
  19.             return null;   
  20.         }   
  21.   
  22.         /*  
  23.          * 入桶  
  24.          */  
  25.         private Map<String, List<SysConstants>> in() {   
  26.             Map<String, List<SysConstants>> bucket = new LinkedHashMap<String, List<SysConstants>>(list.size());   
  27.             for (SysConstants obj : list) {   
  28.                 String parentCode = obj.getLev().equals(1) ? TOP_CODE : obj.getParentCode();   
  29.                 List<SysConstants> parentList = bucket.get(parentCode);   
  30.                 if (parentList != null) {   
  31.                     parentList.add(obj);   
  32.                 } else {   
  33.                     parentList = new ArrayList<SysConstants>();   
  34.                     parentList.add(obj);   
  35.                     bucket.put(parentCode, parentList);   
  36.                 }   
  37.             }   
  38.             return bucket;   
  39.         }   
  40.   
  41.         /*  
  42.          * 出桶  
  43.          */  
  44.         private void out(List <SysConstants> values, Map<String, List<SysConstants>> bucket, String node) {   
  45.             List<SysConstants> nodes = bucket.get(node);   
  46.             if(nodes != null && nodes.size() > 0){   
  47.                 for (SysConstants obj : nodes) {   
  48.                     values.add(obj);   
  49.                     out(values, bucket, obj.getCode());   
  50.                 }   
  51.             }   
  52.         }   
  53.     }  
class MenuSort {
		private List<SysConstants> list = null;
		final String TOP_CODE = "005001036";

		public MenuSort(List<SysConstants> list) {
			this.list = list;
		}

		/*
		 * 排序
		 */
		public List <SysConstants> sort() {
			if (list != null && !list.isEmpty()) {
				List <SysConstants> values = new ArrayList<SysConstants>(list.size());
				Map<String, List<SysConstants>> bucket = in();				
				out(values, bucket, TOP_CODE);
				return values;
			}
			return null;
		}

		/*
		 * 入桶
		 */
		private Map<String, List<SysConstants>> in() {
			Map<String, List<SysConstants>> bucket = new LinkedHashMap<String, List<SysConstants>>(list.size());
			for (SysConstants obj : list) {
				String parentCode = obj.getLev().equals(1) ? TOP_CODE : obj.getParentCode();
				List<SysConstants> parentList = bucket.get(parentCode);
				if (parentList != null) {
					parentList.add(obj);
				} else {
					parentList = new ArrayList<SysConstants>();
					parentList.add(obj);
					bucket.put(parentCode, parentList);
				}
			}
			return bucket;
		}

		/*
		 * 出桶
		 */
		private void out(List <SysConstants> values, Map<String, List<SysConstants>> bucket, String node) {
			List<SysConstants> nodes = bucket.get(node);
			if(nodes != null && nodes.size() > 0){
				for (SysConstants obj : nodes) {
					values.add(obj);
					out(values, bucket, obj.getCode());
				}
			}
		}
	}



2.递归法

Java代码 复制代码 收藏代码
  1. private List<ProductCategory> recursivProductCategoryTreeList(List<ProductCategory> allProductCategoryList, ProductCategory p, List<ProductCategory> temp) {   
  2.         if (temp == null) {   
  3.             temp = new ArrayList<ProductCategory>();   
  4.         }   
  5.         for (ProductCategory productCategory : allProductCategoryList) {   
  6.             ProductCategory parent = productCategory.getParent();   
  7.             if ((p == null && parent == null) || (productCategory != null && parent == p)) {   
  8.                 temp.add(productCategory);   
  9.                 if (productCategory.getChildren() != null && productCategory.getChildren().size() > 0) {   
  10.                     recursivProductCategoryTreeList(allProductCategoryList, productCategory, temp);   
  11.                 }   
  12.             }   
  13.         }   
  14.         return temp;   
  15. }  
private List<ProductCategory> recursivProductCategoryTreeList(List<ProductCategory> allProductCategoryList, ProductCategory p, List<ProductCategory> temp) {
		if (temp == null) {
			temp = new ArrayList<ProductCategory>();
		}
		for (ProductCategory productCategory : allProductCategoryList) {
			ProductCategory parent = productCategory.getParent();
			if ((p == null && parent == null) || (productCategory != null && parent == p)) {
				temp.add(productCategory);
				if (productCategory.getChildren() != null && productCategory.getChildren().size() > 0) {
					recursivProductCategoryTreeList(allProductCategoryList, productCategory, temp);
				}
			}
		}
		return temp;
}


js折叠树

Java代码 复制代码 收藏代码
  1. // 树折叠   
  2.     $(".categoryName").click( function() {   
  3.         var level = $(this).parent().attr("level");   
  4.         var isHide;   
  5.         $(this).parent().nextAll("tr").each(function(){   
  6.             var thisLevel = $(this).attr("level");   
  7.             if(thisLevel <=  level) {   
  8.                 return false;   
  9.             }   
  10.             if(isHide == null) {   
  11.                 if($(this).is(":hidden")){   
  12.                     isHide = true;   
  13.                 } else {   
  14.                     isHide = false;   
  15.                 }   
  16.             }   
  17.             if( isHide) {   
  18.                 $(this).show();   
  19.             } else {   
  20.                 $(this).hide();   
  21.             }   
  22.         });   
  23.     });  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics