ztree自动生成树状图

public String getOrgTree(Map<String, String> paramTree) {
        String orgId = paramTree.get("orgId");
        String checkedData = paramTree.get("checkedData");
        String orgRoot = paramTree.get("orgRoot");
        List<SysOrg> list=new ArrayList<SysOrg>();
        
        SysOrgExample example=new SysOrgExample();
        SysOrgExample.Criteria criteria=example.createCriteria();
        example.setOrderByClause("ORG_ORDER");
        
        SysOrg org = sysOrgMapper.selectByPrimaryKey(BpsConst.DYBANK_ORG_ID);
        String rootName = "";
        if(org !=null){
            rootName = org.getOrgShrtNm();
        }
        
        if ("orgRoot".equals(orgRoot)) {
            list = getAllOrgByparentId(orgId);
            return JSONUtil.getTreeOrg(list, orgRoot, 0, checkedData, paramTree.get("orgName"),orgId).toString();
        }else {
            list=sysOrgMapper.selectWholeByExample(example);
            return JSONUtil.getTree(list,"orgRoot",0,checkedData,rootName).toString();
        }
    }
    
    public List<SysOrg> getAllOrgByparentId(String parentId){
        List<SysOrg> sysOrgList = new ArrayList<SysOrg>();
        
        SysOrgExample example=new SysOrgExample();
        SysOrgExample.Criteria criteria=example.createCriteria();
        example.setOrderByClause("ORG_ORDER");
        criteria.andFOrgIdEqualTo(parentId);
        
        List<SysOrg> orgList = sysOrgMapper.selectWholeByOrgId(example);
        for (SysOrg sysOrg:orgList) {
            sysOrgList.add(sysOrg);
            List<SysOrg> orgListL = getAllOrgByparentId(sysOrg.getOrgId());
            if(orgListL==null){
                continue;
            }
            for (SysOrg sysOrgL :orgListL) {
                sysOrgList.add(sysOrgL);
            }
        }
        return sysOrgList;
    }
 public static JSONArray getTreeOrg(List<SysOrg> list,String fatherId,int level,String checkedData,String rootName,String orgId){
        JSONArray jsonArray=new JSONArray();
        if(fatherId.equals("orgRoot")){
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id", orgId);
            jsonObject.put("pId","orgRoot");
            jsonObject.put("name",rootName);//T
            jsonObject.put("open",true);
            jsonObject.put("icon","plugins/zTree/3.5/zTreeStyle/img/diy/1_open.png");
            jsonObject.put("children", getTree(list,orgId,level+1,checkedData,""));
            jsonArray.add(jsonObject);
        }else{
            for(int i=0;i<list.size();i++){
                if(list.get(i).getfOrgId().equals(fatherId)){
                    JSONObject jsonObject=new JSONObject();
                    List<String> listCheckedData = StringUtil.strToList(checkedData);
                    jsonObject.put("id", list.get(i).getOrgId());
                    jsonObject.put("pId",list.get(i).getfOrgId());
                    jsonObject.put("name",list.get(i).getOrgNm());
                    //jsonObject.put("open",true);
                    if(listCheckedData!=null && listCheckedData.contains(list.get(i).getOrgId())){
                        jsonObject.put("checked",true);
                    }
                    jsonObject.put("attribute", list.get(i));
                    jsonObject.put("children", getTree(list, list.get(i).getOrgId(),level+1,checkedData,""));
                    if(fatherId.equals(SysContant.ORG_ROOT_ID)){
                        jsonObject.put("icon", "static/images/application_side_expand.png");
                        jsonObject.put("open",true);
                    }else{
                        jsonObject.put("icon", "static/images/application.gif");
                    }
                    jsonArray.add(jsonObject);
                }
            }
        }
        return jsonArray;
    }
 public static JSONArray getTree(List<SysOrg> list,String fatherId,int level,String checkedData,String rootName){
        JSONArray jsonArray=new JSONArray();
        if(fatherId.equals("orgRoot")){
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("id", SysContant.DYBANK_ORG_ID);
            jsonObject.put("pId","orgRoot");
            jsonObject.put("name",rootName);
            jsonObject.put("open",true);
            jsonObject.put("icon","plugins/zTree/3.5/zTreeStyle/img/diy/1_open.png");
            jsonObject.put("children", getTree(list,SysContant.DYBANK_ORG_ID,level+1,checkedData,""));
            jsonArray.add(jsonObject);
        }else{
            for(int i=0;i<list.size();i++){
                if(list.get(i).getfOrgId().equals(fatherId)){
                    JSONObject jsonObject=new JSONObject();
                    List<String> listCheckedData = StringUtil.strToList(checkedData);
                    jsonObject.put("id", list.get(i).getOrgId());
                    jsonObject.put("pId",list.get(i).getfOrgId());
                    jsonObject.put("name",list.get(i).getOrgNm());
                    //jsonObject.put("open",true);
                    if(listCheckedData!=null && listCheckedData.contains(list.get(i).getOrgId())){
                        jsonObject.put("checked",true);
                    }
                    jsonObject.put("attribute", list.get(i));
                    jsonObject.put("children", getTree(list, list.get(i).getOrgId(),level+1,checkedData,""));
                    if(fatherId.equals(SysContant.ORG_ROOT_ID)){
                        jsonObject.put("icon", "static/images/application_side_expand.png");
                        jsonObject.put("open",true);
                    }else{
                        jsonObject.put("icon", "static/images/application.gif");
                    }
                    jsonArray.add(jsonObject);
                }
            }
        }
        return jsonArray;
    }
原文地址:https://www.cnblogs.com/bingrong/p/7229203.html