欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

JAVA TREE,

来源: javaer 分享于  点击 9219 次 点评:126

JAVA TREE,


package tree;

import java.util.ArrayList;
import java.util.List;

public class Department
{
    private String id;
    private String parentId;
    private String depName;
    public int getLevel()
    {
        return level;
    }

    public void setLevel(int level)
    {
        this.level = level;
    }

    private int level;
   
    public Department(){
    
    }

    public String getId(){
     return id;
    }

    public void setId(String id){
     this.id =id;
    }

    public String getParentId(){
     return parentId;
    }

    public void setParentId(String parentId) {
     this.parentId= parentId;
    }

    public String getDepName(){
     return depName;
    }

    public void setDepName(String depName) {
     this.depName= depName;
    }
   
    public boolean equals(Object obj){
     if(this.id.equals(((Department)obj).getId()))
       return true;
     else
      return false;
    }
 
  
   public  static void test(){
    List<Department> deps =new ArrayList<Department>();
   
    Department d3 = new Department();
    d3.setId("003");
    d3.setParentId("002");
    d3.setDepName("产品部");
    deps.add(d3);
   
    Department d5 = new Department();
    d5.setId("005");
    d5.setParentId("003");
    d5.setDepName("产品部");
    deps.add(d5);
   
    Department d1 = new Department();
    d1.setId("001");
    d1.setParentId(null);
    d1.setDepName("技术部");
    deps.add(d1);
   
    Department d2 = new Department();
    d2.setId("002");
    d2.setParentId("001");
    d2.setDepName("研发部");
    deps.add(d2);
   
   
    Department d4 = new Department();
    d4.setId("004");
    d4.setParentId("001");
    d4.setDepName("研发eee部");
    deps.add(d4);
   
    List<Department> ds =getDeps(d1,deps,1);
    ds.add(0, d1);
    for(Department dep : ds)
     System.out.println(dep.getDepName()+dep.getId() + dep.getLevel());
    String json ="";
    ;
    System.out.println(Zi(deps,json));
   }
  
  
   public static List<Department>  getDeps(Department department,List<Department> allDeps,int i){
    List<Department> child=new ArrayList<Department>();
    //parent
    List<String> childx=new ArrayList<String>();
    List<Department> result =new ArrayList<Department>();
    for(Department dep :allDeps)
    {
        if(dep.getParentId()!= null &&dep.getParentId().equals(department.getId()))
        {  
           
                dep.setLevel(i);
           
            child.add(dep);
            childx.add(dep.getParentId());
        
            if(!childx.contains(dep.getParentId()))
            {
                i++;
            }
           
           
        }
    }
   
     
    result.addAll(child);
   
    for(Department dep :child)
     result.addAll(getDeps(dep,allDeps,dep.getLevel()+1));
    return result;

   }
  
  
   public static String Zi(List<Department> p, String json)
   {
       List<Department> deps =new ArrayList<Department>();
      
       for (int i = 0; i < p.size(); i++)
       {
          for(int j =1;j<p.size()-1;j++)
          {
              if (p.get(i).getParentId() ==p.get(j).getId())
              {
                  json += "{\"id\":" + p.get(i).getId() + "," + "\"pId\":"+ "\"name\":\"" + p.get(i)
                          + "\"}";
                  json += p.get(i).getId() + ",";
                  deps.add(p.get(i));
                 // Zi(p, p.get(i).getId(), json);
              }
          }
       }
      
       p.removeAll(deps);
       json = p.get(0).getId();
      
       return json;
   }
  
   public static void main(String[] args)
   {
       test();
      // System.out.println(Zi());
   }
}

 

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

 

  

this.jsoninitNodes=[
    {id:'group_a',dataObject:{match:'Group A'}},
    {id:'match_34',pid:'group_a',isLeaf:false,dataObject:{match:'France - South Africa',result:'<img src="fra.gif"/>'}},
      {id:'group_b1w',pid:'match_34',isLeaf:false,dataObject:{match:'Group B'},userObject:{jsonName:'jsonNodes_B'}},
   
    {id:'group_b',isLeaf:false,dataObject:{match:'Group B'}},
      {id:'group_b1',pid:'group_b',isLeaf:false,dataObject:{match:'Group B'},userObject:{jsonName:'jsonNodes_C'}},
     
    {id:'group_d',isLeaf:false,dataObject:{match:'Group D'},userObject:{jsonName:'jsonNodes_D'}}
  ];

this.jsonNodes_B=[
    {id:'jsonNodes_B',pid:'group_b1',dataObject:{}}
  ];

this.jsonNodes_C=[
    {id:'jsonNodes_C',pid:'group_c',dataObject:{}}
  ];
this.jsonNodes_D=[
    {id:'jsonNodes_D',pid:'group_d',dataObject:{}}
  ];
  
  

}


//ExpandNodeEvent
function fifaExpandNodeEvent(node, tree){
  tree.startLoadingNode(node);
  //you logic,you can call ajax here , when call success ,use function 'endLoadingNode(node)' to init the loading node;

  //example for my dummy data logic
  var userObject=node.userObject;
  if(userObject!=null){
   var jsonName=userObject.jsonName;
   if(jsonName!=null){
    var jsonNodes=eval('new dummyJsonData().'+jsonName);
    if(jsonNodes!=null){
     tree.loadingAddNodes(jsonNodes,node.id);
    }
   }
  }
  /////////////////////////////////

  tree.endLoadingNode(node);
    if(userObject != undefined)
    {
   var child = node.childs;
  var lastid = child[0].id;
  $('#girdtree_'+lastid).html(test());
    }
}

 

 

//ExpandNodeEvent
function fifaExpandNodeEvent(node, tree){
    if (node.isLoad == false) {
  tree.startLoadingNode(node);
  var userObject=node.userObject;
  if(userObject!=null && userObject.bindate){
          var nodename = node.id + "indata";
          var pid = node.id;
          var jsonName = [{id:nodename,pid:pid,dataObject:{},userObject:{}}];
    tree.loadingAddNodes(jsonName,node.id);
          $('#girdtree_'+nodename).html(test());

  }
  tree.endLoadingNode(node);
 }
}

 

 


 .custom_table_border_bottom
 {
   border-bottom:none;
 }
 
 .custom_table_border_left
 {
   border-left:none;
 }
 
 .custom_table_border_right
 {
   border-right:none;
 }
 
 .custom_table_border_top
 {
   border-top:none;
 }
 
 .custom_table_border
 {
   border:none;
 }
 

 在jQuery中我们借助一些特殊字符来实现查询,它们就象是编程语言里面的关键字,为方便记忆总结如下#;&,.+*~':"!^$[]()=>|/

相关文章

    暂无相关文章
相关栏目:

用户点评