关于多级分类的封装

现在在公司用到了关于多级分类的菜单树,关于封装成树形结构传给前端,下面提供几个方式:

1:传统封装:每一级循环一次得到该子类

    @Override
    public List<Map<String, Object>> getCatalist(Integer sch_id) {
        //得到一级导航
        List<Map<String, Object>> list = courseDao.getCatalist(sch_id,0);
        //循环一级分类
        for(Map<String, Object> map :list){
            //获得一级分类的id值作为二级分类的父id
            Integer p_id = (Integer) map.get("p_code");
            //得到二级分类
            List<Map<String, Object>> list2 = courseDao.getCatalist(sch_id,p_id);
            if(list2 != null && list2.size()>0){
                map.put("child", list2);
            }
        
            
            
            //循环二级分类
            for(Map<String, Object> map2 :list2){
                //获得二级分类的id值作为三级分类的父id
                Integer p_id2 = (Integer) map2.get("p_code");
                //得到二级分类
                List<Map<String, Object>> list3 = courseDao.getCatalist(sch_id,p_id2);
                if(list3 != null && list.size()>0){
                    map2.put("child", list3);
                }
            }
            
            
        }
        return list;
    }

2:递归封装

由于树形菜单的表示根据p_id来建立父子关系的,首先可以查询所有的菜单信息,不区分父子

    @Override
    public List<CatagoryTree> getCatagory() {
        String sql = " SELECT cc.id,cc.p_id AS pid,cc.name AS cataname ,cc.sort_index , cc.status,cc.is_visable_nav nav FROM y_crs_catagory cc WHERE cc.sch_id=? ORDER BY sort_index";
        List<Map<String, Object>> list = jdbcTemplateBaseDao.getSelectForList(sql, "9999");
        
        List<CatagoryTree> cataDatas = new ArrayList<CatagoryTree>();
        if(list!=null){
            for(Map<String, Object> map : list){
                CatagoryTree cataTree = new CatagoryTree();
                cataTree.setId(String.valueOf(map.get("id")));
                cataTree.setName(String.valueOf(map.get("cataname")));
                cataTree.setPrientId(String.valueOf(map.get("pid")));
                cataTree.setSortIndex(String.valueOf(map.get("sort_index")));
                cataTree.setStatus(String.valueOf(map.get("status")).equals("true")?true:false);
                cataTree.setNav(String.valueOf(map.get("nav")).equals("true")?true:false);
                cataDatas.add(cataTree);
            }
            cataDatas=TreeUtils.treeCatagory(cataDatas, "0");
        }
        return cataDatas;
    }

上面是将查询的所有分类信息封装成对象的集合cataDatas传入到分类树方法treeCatagory中做处理

public static List treeCatagory(List treeDataList, String parentID) {
    List list = null;
    CatagoryTree catagoryTree = null;
    if (treeDataList != null && treeDataList.size() > 0) {
        try {
            for (int i = 0; i < treeDataList.size(); i++) {
                catagoryTree = (CatagoryTree) treeDataList.get(i);
                if (catagoryTree.getPrientId().equals(parentID)) {
                    catagoryTree.setChild(treeCatagory(treeDataList, catagoryTree.getId()));
                    
                    if (list == null) {
                        list = new ArrayList();
                    }
                    list.add(catagoryTree);
                }
            }
        } catch (Exception e) {
            // TODO: handle exception
            logger.error(catagoryTree.getName() + " 菜单,没有父节点");
            e.printStackTrace();
        }
    }

    return list;
}

采用递归方式封装成树

 

 

 

------------------------------------------------------------------------------------------------------------------------------

在这里写一个对list中的map集合进行排序的功能

sortField是map中需要排序的字段名

sortType是排序方式  asc或者desc

    public static final List<Map<String, Object>> getSortedResult(List<Map<String, Object>> list, final Object sortField, final String sortType) {
        if(null != list && list.size() != 0 && null != sortField){
            if("asc".equalsIgnoreCase(sortType) || "desc".equalsIgnoreCase(sortType)){
                Collections.sort(list,new Comparator<Map<String,Object>>() {
                    @Override
                    public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                        if("asc".equalsIgnoreCase(sortType))    
                            // 升序
                            return o1.get(sortField).toString().compareTo(o2.get(sortField).toString());
                        else 
                            // 降序
                            return o2.get(sortField).toString().compareTo(o1.get(sortField).toString());
                    }  
                });
            }
        }
        return list;
    }
}

 

posted on 2015-07-21 11:43  itliucheng  阅读(1071)  评论(0编辑  收藏  举报