我使用的是阿信的 TreeView3.0

解决方案 »

  1.   

    这个是用脚本控制,给一些关键代码给你看:
    <!-- CSS -->
    /*---展开(显示)/关闭(隐藏)---*/
    .expanded {
    display:block;
    }
    .collapsed {
    display: none;
    }/*-- JavaScript --*/
    function ExpandChild(Obj,NodeID,Values)
    {
    trObj = eval("Tr" + NodeID);
    tdObj = eval("Td" + NodeID);
    if(trObj.className=="collapsed"){
    Obj.src = "Images/Tree/Collapse.gif";
    trObj.className = "expanded";
    //这下面是结点展开显示操作,根据你的具体情况而定,我是用的XMLHTTP。
    if(tdObj.innerHTML==""){
    getData(NodeID,Values);
    }
    }else{
    Obj.src = "Images/Tree/Expand.gif";
    trObj.className = "collapsed";
    }
    }
      

  2.   

    要修改TreeView的write方法,因为我默认显示时调用expand方法,把这句去掉
      

  3.   

    谢谢 fason(咖啡人生) ,我看看先,如果不能解决再请教。
      

  4.   

    var icon={
    root :'../images/CustomerMgmt.gif',
    open :'../images/treeview/open.png',
    close :'../images/treeview/close.png',
    file :'../images/treeview/file.png',
    Rplus :'../images/treeview/Rplus.gif',
    Rminus :'../images/treeview/Rminus.gif',
    join :'../images/treeview/T.png',
    joinbottom:'../images/treeview/L.png',
    plus :'../images/treeview/Tplus.png',
    plusbottom:'../images/treeview/Lplus.png',
    minus :'../images/treeview/Tminus.png',
    minusbottom:'../images/treeview/Lminus.png',
    blank :'../images/treeview/blank.png',
    line :'../images/treeview/I.png'
    };
    var Global={
    id:0,
    getId:function(){return this.id++;},
    all:[],
    selectedItem:null,
    defaultText:"treeItem",
    defaultAction:"javascript:void(0)",
    defaultTarget:"_blank"
    }
    function preLoadImage(){
    for(i in icon){
    var tem=icon[i];
    icon[i]=new Image()
    icon[i].src=tem
    }
    };preLoadImage();function treeItem(text,action,target,title,Icon)
    {
    this.id=Global.getId();
    this.level=0;
    this.text=text?text:Global.defaultText;
    this.action=action?action:Global.defaultAction;
    this.target=target?target:Global.defaultTarget;
    this.title=title?title:this.text;
    this.isLast=true;
    this.childNodes=new Array();
    this.indent=new Array();
    this.parent=null;
    var c =0; 
    if(getCookie("item"+this.id) != null) c = getCookie("item"+this.id);
    this.open=parseInt(c);
    this.load=false;
    this.setuped=false;
    this.JsItem=null;
    this.container=document.createElement("div");
    this.icon=Icon;
    Global.all[Global.all.length]=this;
    }treeItem.prototype.toString = function()
    {
    var o = this;
    var oItem = document.createElement("div");
    oItem.id = "treeItem"+this.id
    oItem.className = "treeItem";
    oItem.noWrap = true;
    oItem.onselectstart = function(){ return false;}
    oItem.oncontextmenu = function(){ return false;}
    this.JsItem = oItem;
    this.drawIndents();
    var iIcon = document.createElement("img");
    iIcon.align = "absmiddle";
    iIcon.src = this.childNodes.length>0?(this.open?(this.level>0?(this.isLast?icon.minusbottom.src:icon.minus.src):icon.Rminus.src):(this.level>0?(this.isLast?icon.plusbottom.src:icon.plus.src):icon.Rplus.src)):(this.level>0?(this.isLast?icon.joinbottom.src:icon.join.src):icon.blank.src);
    iIcon.id = "treeItem-icon-handle-" + this.id;
    iIcon.onclick = function(){ o.toggle();};
    oItem.appendChild(iIcon);
    var iIcon = document.createElement("img");
    iIcon.align = "absmiddle";
    iIcon.src = this.icon?this.icon:(this.childNodes.length>0?(this.open?icon.open.src:icon.close.src):icon.file.src);
    iIcon.id = "treeItem-icon-folder-" + this.id;
    iIcon.onclick = function(){ o.select();};
    iIcon.ondblclick = function(){ o.toggle();};
    oItem.appendChild(iIcon);
    var eText = document.createElement("span");
    var eA=document.createElement("a");
    eA.innerHTML = this.text;
    eA.target = this.target;
    eA.href = this.action;
    eA.onkeydown = function(e){ return o.KeyDown(e);}
    if(this.action == Global.defaultAction) eA.onclick = function(){ return false;}
    eText.appendChild(eA);
    eText.id = "treeItem-text-" + this.id;
    eText.className = "treeItem-unselect"
    eText.onclick = function(){ o.select(1);};
    eText.title = this.title;
    oItem.appendChild(eText);
    this.container.id = "treeItem-container-"+this.id;
    this.container.style.display = this.open?"":"none";
    oItem.appendChild(this.container);
    return oItem;
    }treeItem.prototype.root = function()
    {
    var p = this;
    while(p.parent)
    p = p.parent;
    return p;
    }treeItem.prototype.setText = function(sText)
    {
    if(this.root().setuped)
    {
    var oItem = document.getElementById("treeItem-text-" + this.id);
    oItem.firstChild.innerHTML = sText;
    }
    this.text = sText;
    }treeItem.prototype.setIndent = function(l,v)
    {
    for(var i=0;i<this.childNodes.length;i++)
    {
    this.childNodes[i].indent[l] = v;
    this.childNodes[i].setIndent(l,v);
    }
    }treeItem.prototype.drawIndents = function()
    {
    var oItem = this.JsItem;
    for(var i=0;i<this.indent.length;i++){
    var iIcon = document.createElement("img");
    iIcon.align = "absmiddle";
    iIcon.id = "treeItem-icon-" + this.id + "-" + i;
    iIcon.src = this.indent[i]?icon.blank.src:icon.line.src;
    oItem.appendChild(iIcon);
    }
    }treeItem.prototype.add = function(oItem)
    {
    oItem.parent=this;
    this.childNodes[this.childNodes.length]=oItem;
    oItem.level=this.level+1;
    oItem.indent=this.indent.concat();
    oItem.indent[oItem.indent.length]=this.isLast;
    if(this.childNodes.length>1){
    var o=this.childNodes[this.childNodes.length-2];
    o.isLast=false;
    o.setIndent(o.level,0);
    if(this.root().setuped)o.reload(1);
    }
    else if(this.root().setuped)
    this.reload(0);
    this.container.appendChild(oItem.toString());
    this.container.style.display=this.open?"":"none";
    }treeItem.prototype.loadChildren = function()
    {
    //do something
    }
      

  5.   

    treeItem.prototype.remove = function()
    {
    var tmp = this.getPreviousSibling();
    //if(tmp){ tmp.select();}
    this.removeChildren();
    var p = this.parent;
    if(!p){ return };
    if(p.childNodes.length>0){
    var o = p.childNodes[p.childNodes.length-1];
    o.isLast = true;
    o.setIndent(o.level,1);
    if(o.root().setuped)o.reload(1);
    }
    else
    p.reload();
    }treeItem.prototype.removeChildren = function ()
    {
    if(this == Global.selectedItem){ Global.selectedItem = null;}
    for(var i=this.childNodes.length-1;i>=0;i--)
    this.childNodes[i].removeChildren();
    var o = this;
    var p = this.parent;
    if (p) { p.childNodes = p.childNodes._remove(o);}
    Global.all[this.id] = null
    var oItem = document.getElementById("treeItem"+this.id);
    if (oItem) { oItem.parentNode.removeChild(oItem); }
    }treeItem.prototype.reload = function(flag)
    {
    if (flag){
    for(var j=0;j<this.childNodes.length;j++){ this.childNodes[j].reload(1);}
    for(var i=0;i<this.indent.length;i++)
    document.getElementById("treeItem-icon-" +this.id+ "-"+i).src = this.indent[i]?icon.blank.src:icon.line.src;
    }
    document.getElementById("treeItem-icon-handle-" +this.id).src = this.childNodes.length>0?(this.open?(this.level>0?(this.isLast?icon.minusbottom.src:icon.minus.src):icon.Rminus.src):(this.level>0?(this.isLast?icon.plusbottom.src:icon.plus.src):icon.Rplus.src)):(this.level>0?(this.isLast?icon.joinbottom.src:icon.join.src):icon.blank.src);
    if (!this.icon)
    document.getElementById("treeItem-icon-folder-"+this.id).src = this.childNodes.length>0?(this.open?icon.open.src:icon.close.src):icon.file.src;

    }treeItem.prototype.toggle = function()
    {
    if(this.childNodes.length>0){
    if(this.open)
    this.collapse();
    else
    this.expand();
    }
    }treeItem.prototype.expand = function()
    {
    this.open=1;
    setCookie("item"+this.id,1);
    if(!this.load){
    this.load=true;
    this.loadChildren();
    this.reload(1);
    }
    else 
    this.reload(0);
    this.container.style.display = "";
    }treeItem.prototype.collapse = function()
    {
    this.open=0;
    setCookie("item"+this.id,0);
    this.container.style.display = "none";
    this.reload(0);
    this.select(1);
    }treeItem.prototype.expandAll = function()
    {
    if(this.childNodes.length>0 && !this.open)this.expand();
    this.expandChildren();
    }treeItem.prototype.collapseAll = function()
    {
    this.collapseChildren();
    if(this.childNodes.length>0 && this.open)this.collapse();
    }treeItem.prototype.expandChildren = function()
    {
    for(var i=0;i<this.childNodes.length;i++)
    this.childNodes[i].expandAll();
    }treeItem.prototype.collapseChildren = function()
    {
    for(var i=0;i<this.childNodes.length;i++)
    this.childNodes[i].collapseAll()
    }treeItem.prototype.openURL=function()
    {
    if(this.action!=Global.defaultAction){
    window.open(this.action,this.target);
    }
    }treeItem.prototype.select=function(o)
    {
    if (Global.selectedItem) Global.selectedItem.unselect();
    var oItem = document.getElementById("treeItem-text-" + this.id);
    oItem.className = "treeItem-selected";
    oItem.firstChild.focus();
    Global.selectedItem = this;
    if(!o) this.openURL();
    }treeItem.prototype.unselect=function()
    {
    var oItem = document.getElementById("treeItem-text-" + this.id);
    oItem.className = "treeItem-unselect";
    oItem.firstChild.blur();
    Global.selectedItem = null;
    }treeItem.prototype.setup = function(oTaget)
    {
    oTaget.appendChild(this.toString());
    this.setuped = true;
    if(this.childNodes.length>0 || this.open) this.expand();
    }
      

  6.   

    这个版本应该不是3.0啊~~~
    把最后一个方法setup中的this.expand()不要就是
      

  7.   

    treeItem.prototype.setup = function(oTaget)
    {
    oTaget.appendChild(this.toString());
    this.setuped = true;
    //if(this.childNodes.length>0 || this.open) this.expand();
    }是不是我注释掉的代码啊?
    注释掉之后倒可以实现,Treeview的是否展开还是收拢与cookie有关。打开目录树后关闭页面,再打开页面,它保持原来的状态的。