js树通用代码

 

  js树通用代码,用的sack.js的AJAX

  

前台js处理代码

代码

function toUrl(id){
    
var tagetUrl = document.getElementById("tagetUrl").value;
    location.href
=tagetUrl+id;    
}
function clickImgNode(event){
    
var ev = event||window.event;
    
var evt = ev.target || ev.srcElement;
    
var img = evt;
    clickNode(img.mId);
}
function clickNode(id){
    
var oDiv = document.getElementById(id);
    
var status = oDiv.getAttribute("status");
    
if(status=="expended"){
        collepse(oDiv);
    }
else if(status=="collepsed"){
        expend(oDiv);
    }
else{
        doit(id);
    }
}
function collepse(oDiv){
    
var children = oDiv.childNodes;
    
var img = oDiv.childNodes[0].tagName == "IMG"?oDiv.childNodes[0]:oDiv.childNodes[1];
    
var length = children.length;
    
for(var i=0;i<length;i++){
        
var child = children[i];
        
if(child.tagName=='DIV'){
            child.style.display
="none"
        }
    }
    img.src
="/vimgs/treetable/plus.gif";
    oDiv.setAttribute(
"status","collepsed");
}
function expend(oDiv){
    
var children = oDiv.childNodes;
    
var img = oDiv.childNodes[0].tagName == "IMG"?oDiv.childNodes[0]:oDiv.childNodes[1];
    
var length = children.length;
    
for(var i=0;i<length;i++){
        
var child = children[i];
        
if(child.tagName=='DIV'){
            child.style.display
=""
        }        
    }
    img.src
="/vimgs/treetable/minus.gif";
    oDiv.setAttribute(
"status","expended");
}
function doit(id){
    
var ajax = new sack();
    
var model = document.getElementById("model").value;
    ajax.setVar(
"pid", id);
    ajax.setVar(
"model", model);
    ajax.setVar(
"action""getDocXmlChildren");
    ajax.requestURL 
= "/ServiceAction/com.velcro.base.category.servlet.CategoryTreeAction";
    ajax.method 
= "GET";
    ajax.onLoading 
= function(){showLoading(id);};
    ajax.onCompletion 
=function(){whenCompleted(ajax);};
    ajax.runAJAX();
}
function showLoading(id){
    
var oDiv = document.getElementById(id);
    
if(oDiv.childNodes[0].tagName == "IMG"){
        oDiv.childNodes[
0].src="/vimgs/task/loading.gif";
     }
else{
        oDiv.childNodes[
1].src="/vimgs/task/loading.gif";
     }
}

function whenCompleted(ajax){
  
var oXmlDom = ajax.responseXML;
  
var categorys = oXmlDom.documentElement;
  
var pid = categorys.getAttribute("id");
  
var parentDIV = document.getElementById(pid);
  parentDIV.setAttribute(
"status","expended");
  
var oFragment=document.createDocumentFragment();
  
var length = categorys.childNodes.length;
     
for(var i=0;i<length;i++){
         
var category = categorys.childNodes[i];
         
var id = category.getAttribute("id");
         
var name = category.getAttribute("name");
         
var hasChildren = category.getAttribute("hasChildren");
         
var hasPermission = category.getAttribute("hasPermission");
         
         
var childrenDIV = document.createElement("div");
         childrenDIV.id
=id;
        childrenDIV.setAttribute(
"status","none");
         
var img = document.createElement("img");
         img.align
="absmiddle";
         img.border
="0";
         img.mId
=id;
         
if(hasChildren=="true"){     
             img.src
="/vimgs/treetable/plus.gif";
             img.onclick 
= function(event){clickImgNode(event);}; 
         }
else{
             img.src
="/vimgs/treetable/blank.gif";
         }         
         childrenDIV.appendChild(img);
         
if(hasPermission=="true"){
             
var tag = document.createElement("a");
             tag.href
="javascript:toUrl('"+id+"');";
             tag.appendChild(document.createTextNode(
""+name));
             childrenDIV.appendChild(tag);
         }
else{
             
var span = document.createElement("span");
             span.appendChild(document.createTextNode(name));
             childrenDIV.appendChild(span);
         }
         
if(hasChildren=="true"){     
             
try{doit(id);}catch(e){alert(e.message)};
             childrenDIV.setAttribute(
"status","expended");
         }
         oFragment.appendChild(childrenDIV);
     }
     parentDIV.appendChild(oFragment);
    
var IMG = parentDIV.childNodes[0].tagName == "IMG"?parentDIV.childNodes[0]:parentDIV.childNodes[1];
    IMG.src
="/vimgs/treetable/minus.gif";
}

 

后台java处理代码

代码
private void getXmlChildren(HttpServletRequest request,HttpServletResponse response, User user) {
        response.setContentType(
"text/xml");
        response.setHeader(
"Cache-control""no-cache");
        Locale locale
=(Locale)request.getSession().getAttribute(Globals.LOCALE_KEY);
        String pid 
= request.getParameter("pid");
        String model 
= request.getParameter("model");
        String method 
= request.getParameter("method");
        
        Element categorys 
= new Element("categorys");
        categorys.setAttribute(
"id", pid);
        Document document 
= new Document(categorys);
        
        List
<Category> subList= null;

        List
<Category> categoryList= categoryService.getSubCategoryList(pid,model,null,null);
        String hasperm 
= categoryService.getPermitCategoryids(velcrouser, model, pid);
        subList 
= categoryService.getSubTreeList(categoryList,pid,haspermitcatids);
        
        
        
for(int i=0;i<subList.size();i++){
            Category category 
= subList.get(i);
            
if("false".equals(category.getCol3())&&category.getChildrennum()==0){
                
continue;
            }
            Element child 
= new Element("category");
            child.setAttribute(
"id",category.getId());
            child.setAttribute(
"hasPermission",category.getCol3());
            categorys.addContent(child);
        }        
        XMLOutputter outputter 
= new XMLOutputter();
        
try {
            outputter.output(document, response.getOutputStream());
        } 
catch (Exception e) {
            System.out.println(
"获取!");
        }
        
    }

 

 

 

posted @ 2010-01-29 15:23  zping  阅读(660)  评论(0编辑  收藏  举报