动态载入数据的无刷新TreeView控件(3)

    今天完成了对菜单输出部分的重构实现,就是怎么样把我们的JS类变成能看到的实实在在的树。这个步骤虽然不是很难,不过很多细微的东西还是值得注意的。具体输出HTML代码的结构可以参看(1)中的图示,而我们的重点是讲讲为TreeView显示正确的层次结构图标。

    由于我开始说了,希望树的UI逻辑有比较高的内聚性,所以使用了一个可以递归的UI层次结构。我们使用一个TABLE元素来作为树的一个Level的容器,每个TABLE中的一个TR作为一个TreeNode的显示元素。TR又分为两个TD,第一个TD放Hierarchy图标;第二个TD中放TreeNode的具体内容,包括Image、CheckBox和Span(文字放Span中)。

    虽然贴代码不是写文章的好习惯,不过我觉得参照代码来说,还是更容易理解的。

ExpandedBlockStart.gif ContractedBlock.gif < script  language ="JavaScript" > dot.gif
InBlock.gifTreeNodeBase.prototype.Render 
= function(doc)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
var tr = doc.createElement('TR');
InBlock.gif    tr.Comment 
= 'TreeNode'; 
InBlock.gif    tr.Object 
= this;
InBlock.gif    
this.m_Element = tr;
InBlock.gif   
InBlock.gif    
var tdOpIcon = doc.createElement('TD');
InBlock.gif    tr.appendChild(tdOpIcon);
InBlock.gif    
var opImg = doc.createElement('IMG');
InBlock.gif    tr.OpIcon 
= opImg;
InBlock.gif    opImg.src 
= TreeStyle.OpIcon(this.GetOpIconName());
InBlock.gif    tdOpIcon.appendChild(opImg);
InBlock.gif   
InBlock.gif    
var tdContent = doc.createElement('TD');
InBlock.gif    tr.appendChild(tdContent); 
InBlock.gif    tdContent.noWrap 
= 'noWrap'; 
InBlock.gif    
var hasCheckBox = this.Attributes('HasCheckBox');
InBlock.gif    
if ( hasCheckBox )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
var input = doc.createElement('INPUT');
InBlock.gif        input.type 
= 'checkbox';
ExpandedSubBlockStart.gifContractedSubBlock.gif        input.onfocus 
= function()dot.gif{ FindParentElement(this, 'TD').focus(); };
InBlock.gif        tr.CheckBox 
= input;
InBlock.gif        tdContent.appendChild(input);
ExpandedSubBlockEnd.gif    }
     
InBlock.gif    
var icon = doc.createElement('IMG');
InBlock.gif    icon.src 
= TreeStyle.Icon(this.m_IconPath);
InBlock.gif    tdContent.appendChild(icon);
InBlock.gif    tr.Icon 
= icon;
InBlock.gif    
InBlock.gif    
var span = doc.createElement('SPAN');
InBlock.gif    span.noWrap 
= 'nowrap';
InBlock.gif    
var textIndent = this.Attributes('TextIndent');
InBlock.gif    
if ( textIndent > 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         indent 
= doc.createElement('SPAN');
InBlock.gif         indent.style.width 
= textIndent;
InBlock.gif         tdContent.appendChild(indent);
ExpandedSubBlockEnd.gif    }

InBlock.gif    tdContent.appendChild(span);
InBlock.gif    span.innerText 
= this.m_Text; 
InBlock.gif    tr.Content 
= span;
InBlock.gif    
return tr;
ExpandedBlockEnd.gif}
;
None.gif
</ script >


    我之前说过,菜单的UI元素都在TreeNodeBase这个类中负责生成,事件处理放在TreeNode中,所以这里我们只关注UI呈现问题。doc参数其实就是document,写在这里是为了防止在popup中使用时可能出问题,因为只有来自同一个document对象创建的HTML对象才能append到一起。tr.Object=this;和this.m_Element=tr;是用来建立脚本对象和DHTML对象的双向快捷引用,不过这里有一个IE内存泄露潜在问题(我前面的随笔有讲),怎么办?在Dispose中处理就行了。后面的就比较按部就班了,OpIcon是表示Hierarchy的TreeView操作图标,Icon是内容中的图标,CheckBox是一个Optional的条目,最后是用Span括起来的TreeNode的文本。

None.gif  TreeNodeBase.prototype.Dispose  =   function ()
None.gif {
None.gif    
if  (  this .m_Element )
None.gif    {
None.gif         
this .m_Element.clearAttributes();
None.gif         
this .m_Element  =   null ;
None.gif    }
None.gif };

    接下来说TreeView呈现中比较麻烦的OpIcon问题,就是上面代码中的:
None.gif  opImg.src  =  TreeStyle.OpIcon( this .GetOpIconName());
哇,这么简单就搞定了?! 做梦呢 77_77.gif

    由于使用了TABLE嵌套,同级菜单条目之间的连接变得非常的容易处理。就只有两种情况,对于有子菜单的TreeNode,如果不是该Level上最后一个Node,则显示一个"|"线,如果是最后一个Node,则什么都不用显示。然后每个OpIcon就对应于每个Node(一一对应),我们知道就是那些什么"+"、"-"、"|"和"|-"一类的东西了。对于普通Level上的OpIcon是比较容易处理的,可是对于Root那个Level,还需要做很多的特殊处理,以优化其显示。获取节点OpIcon图标的代码如下:

ExpandedBlockStart.gif ContractedBlock.gif < script  language ="javascript" > dot.gif
InBlock.gifTreeNodeBase.prototype.GetOpIconName 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
var nodeCount = this.m_Tree.m_Nodes.length;
InBlock.gif    
if ( ( nodeCount > 1 || this.m_IsLazyLoad ) && !this.IsLastNode() )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( !this.m_ChildTree )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( this.IsFirstNode() && this.m_Tree.m_ParentNode == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return 'TopLine';
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return 'MiddleCrossLine';
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( this.m_IsChildExpanded )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( this.IsFirstNode() && this.m_Tree.m_ParentNode == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return 'TopMinus';
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return 'MiddleMinus';
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( this.IsFirstNode() && this.m_Tree.m_ParentNode == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return 'TopPlus';
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return 'MiddlePlus';
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if ( !this.m_ChildTree )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( this.IsFirstNode() && this.m_Tree.m_ParentNode == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return 'SingleNode';
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{    
InBlock.gif                
return 'BottomLine';
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if ( this.m_IsChildExpanded )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( this.IsFirstNode() && this.m_Tree.m_ParentNode == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return 'SingleMinus';
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return 'BottomMinus';
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if ( this.IsFirstNode() && this.m_Tree.m_ParentNode == null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return'SinglePlus';
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
return 'BottomPlus';
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return '';
ExpandedBlockEnd.gif}
;
None.gif
</ script >

    从上面的代码可以看出,判断节点在Level是IsFirstNode还是IsLastNode是非常重要的哦,虽然这两个方法都简单到只有一行代码 emembarrassed.gif。TreeStyle.OpIcon是一个用于支持不同风格OpIcon图标的静态方法。
None.gif TreeStyle.OpIcon  =   function (imageName)
None.gif{
None.gif    
return  'Skins / default / +  imageName  +  '.gif';
None.gif};

    附上各中TreeView呈现的外观样式:
   TreeView-2.gif
    to be continued . . .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值