1.由栈方法
- 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());
- }
- }
- }
- }
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.递归法
- 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;
- }
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折叠树
- // 树折叠
- $(".categoryName").click( function() {
- var level = $(this).parent().attr("level");
- var isHide;
- $(this).parent().nextAll("tr").each(function(){
- var thisLevel = $(this).attr("level");
- if(thisLevel <= level) {
- return false;
- }
- if(isHide == null) {
- if($(this).is(":hidden")){
- isHide = true;
- } else {
- isHide = false;
- }
- }
- if( isHide) {
- $(this).show();
- } else {
- $(this).hide();
- }
- });
- });