zoukankan      html  css  js  c++  java
  • EasyUI_tree根据数据库数据生成树形结构JSON格式

    @Entity
    public class PubComp {
     @Id
     private String aguid; // 菜单ID
     private String pguid; // 父菜单
     private String aname;// 名称

    }

    public class TreeNode {
      private String id;          //要显示的子节点的ID 
         private String text;        //要显示的子节点的 Text 
         private String iconCls;     //节点的图标 
         private String parentId;    //父节点的ID 
         private List<TreeNode>  children;   //孩子节点的List 
        
         public TreeNode(){}
      public TreeNode(String id, String text, String iconCls,
        String parentId, List<TreeNode> children) {
       super();
       this.id = id;
       this.text = text;
       this.iconCls = iconCls;
       this.parentId = parentId;
       this.children = children;
      }
      public String getId() {
       return id;
      }
      public void setId(String id) {
       this.id = id;
      }
      public String getText() {
       return text;
      }
      public void setText(String text) {
       this.text = text;
      }
      public String getIconCls() {
       return iconCls;
      }
      public void setIconCls(String iconCls) {
       this.iconCls = iconCls;
      }
      public String getParentId() {
       return parentId;
      }
      public void setParentId(String parentId) {
       this.parentId = parentId;
      }
      public List<TreeNode> getChildren() {
       return children;
      }
      public void setChildren(List<TreeNode> children) {
       this.children = children;
      }
      
      //添加子菜单的方法 
         public void addChild(TreeNode node){ 
            if(this.children == null){ 
                children= new ArrayList<TreeNode>(); 
                children.add(node); 
            }else{ 
                children.add(node); 
            } 
                
         } 

     public String load(ModelMap modelMap){
        List<PubComp> pcList=testCaseDaoImpl.queryPubComp("select p from PubComp p ");   
             modelMap.put("list", pcList); //需要返回的数据(pcList)
            
             List<TreeNode> list = new ArrayList<TreeNode>(); 
             Map map = new HashMap<String, TreeNode>(); 
       
             try { 
       
                 //拉出数据库的数据,放入pcList中 
                 //将list2中的数据,转换成TreeNode类型,放入Map中备用 
                 for (PubComp tDict : pcList) { 
                    TreeNode node = new TreeNode(); 
                    node.setId(tDict.getAguid()); 
                    node.setText(tDict.getAname()); 
                    node.setParentId(tDict.getPguid()); 
                    map.put(tDict.getAguid(), node); 
       
                 } 
                 //遍历pcList的数据,把每个节点加入他的父节点的孩子List 
                 for (PubComp tDict : pcList) { 
                        if(tDict.getPguid() == null) 
                        { 
                           list.add((TreeNode)map.get(tDict.getAguid())); 
                        }else{ 
       
                           String pidString = tDict.getPguid(); 
                           TreeNode pnode = (TreeNode)map.get(pidString); 
                           TreeNode cnode=(TreeNode)map.get(tDict.getAguid()); 
                           pnode.addChild(cnode); 
                        } 
                 }        
                
                 //将对象转换成json(注意需要添加gson-2.2.2.jar)
                 Gson g = new GsonBuilder().create();
           String json = g.toJson(list,list.getClass());
                 System.out.println(json+"-----");
                
                
                
                
             }catch (Exception e) { 
                 e.printStackTrace(); 
             }
        

  • 相关阅读:
    WCF Server Console
    Restart IIS With Powershell
    RestartService (recursively)
    Copy Files
    Stopping and Starting Dependent Services
    多线程同步控制 ManualResetEvent AutoResetEvent MSDN
    DTD 简介
    Using Powershell to Copy Files to Remote Computers
    Starting and Stopping Services (IIS 6.0)
    java中的NAN和INFINITY
  • 原文地址:https://www.cnblogs.com/qgc88/p/3286628.html
Copyright © 2011-2022 走看看