(转)对微软那棵TreeView进行试用,主要是对CheckBox进行操作

总结这次的工作,明白了几个关键的地方:
    1.如果在服务器端进行CheckBox的初始化,这样操作就会导致在客户端进行node.getAttribute("checked")时总是得到True,这样就达不到客户端操作的目的.
    2.如果想在生成树的时候对某些节点进行CheckBox的初始化选中,就需要在客户端进行遍历树的操作(构造生成树的同时进行).
    3.在客户端对树的节点进行选中与取消选中的操作时,必须同时进行一个必要的操作:Tree.queueEvent('oncheck',node.getNodeIndex()),这样做的目的使你在客户端进行树操作的同时,能让服务器端也同时知道操作了哪些节点,以便在服务器端进行遍历的同时能知道哪些节点被选中.
    4.以及'checked'的大小写问题也会对树的状态改变有所影响.

之后又对代码进行了优化,我把生成树时的初始化操作,以及保存选中状态都放在了客户端进行!

第一次发表文章,请各位前辈多多指导,多多帮助

None.gif var  strArray  =   new  Array();
None.gif
var  strList  =   "" ;
None.gif
None.gif
function  tree_oncheck()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var ChildNode = new Array();
InBlock.gif    
var node = document.getElementById("TreeView1").getTreeNode(event.treeNodeIndex);
InBlock.gif    
var Pchecked = node.getAttribute("checked");
InBlock.gif    
InBlock.gif    ChildNode 
=  node.getChildren();
InBlock.gif    
if( parseInt( ChildNode.length ) != 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif       node.setAttribute(
"checked",!Pchecked);
InBlock.gif       
return;
ExpandedSubBlockEnd.gif    }

InBlock.gif    setParent(node,Pchecked);
InBlock.gif    document.getElementById(
"TreeView1").queueEvent('oncheck',node.getNodeIndex());
ExpandedBlockEnd.gif}

None.gif
// ===============================================//
None.gif//
在点击子节点时,让父节点的状态也随子节点的状态而变化
None.gif//
===============================================//
None.gif
function  setParent(el,state)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var ParentNode = el.getParent();
InBlock.gif    
if(ParentNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//检查el的兄弟节点状态
InBlock.gif
        if(!checkSiblingdNode(el))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ParentNode.setAttribute('checked',state);
InBlock.gif            setParent(ParentNode,state);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
// ===========================================//
None.gif//
检查兄弟节点状态
None.gif//
===========================================//
None.gif
function  checkSiblingdNode(el)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var parentNode = el.getParent();
InBlock.gif    
var childNodes = new Array();
InBlock.gif    childNodes 
= parentNode.getChildren();
InBlock.gif            
InBlock.gif    
for(var i=0;i< childNodes.length; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if(el.getNodeIndex() != childNodes[i].getNodeIndex())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(childNodes[i].getAttribute("Checked"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return false;
ExpandedBlockEnd.gif}

None.gif
// =============================================//
None.gif//
点击父节点时,对其子节点状态进行同步改变
None.gif//
=============================================//
None.gif
function  setcheck(node,Pc)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var ChildNode = new Array();
InBlock.gif    ChildNode 
= node.getChildren();
InBlock.gif    
if( parseInt(ChildNode.length) != 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
forvar i=0; i<ChildNode.length;i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var cNode = ChildNode[i];
InBlock.gif            
if( cNode.getAttribute("checked"!= Pc )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if( parseInt( cNode.getChildren().length ) != 0 )
InBlock.gif                    setcheck(cNode,Pc);
InBlock.gif                cNode.setAttribute(
"checked",Pc);
InBlock.gif                document.getElementById(
"TreeView1").queueEvent('oncheck',cNode.getNodeIndex());
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ==========================================//
None.gif//
需要对某些节点做初始化处理时
None.gif//
==========================================//
None.gif
function  onload()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var node 
InBlock.gif    
var nextNode = 0;
InBlock.gif    
var childNodes = new Array();
InBlock.gif    
var childNodes = document.getElementById("TreeView1").getChildren();
InBlock.gif
InBlock.gif    SetArray();
//把节点ID组合为一个数组
InBlock.gif
    for(var i=0;i<childNodes.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{    
InBlock.gif        SetTree(childNodes[i]);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ============================================//
None.gif//
组合数组
None.gif//
============================================//
None.gif
function  SetArray()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var IndexStart = 0;
InBlock.gif    
var IndexEnd = 0;
InBlock.gif    
var ArrIndex = 0;
InBlock.gif    
var SignStr = "";
InBlock.gif    
//var SpanString = "0001,00010001,000100010001,";
InBlock.gif
    var SpanString = document.getElementById("H_IncomeCode").value;
InBlock.gif    
var strLen = SpanString.length;
InBlock.gif            
InBlock.gif    
while( IndexEnd < strLen )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        SignStr 
= SpanString.substr(IndexEnd,1);
InBlock.gif        
if( SignStr == "," )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
var TempString = SpanString.substr(IndexStart,IndexEnd-IndexStart);
InBlock.gif            strArray[ArrIndex] 
= TempString;
InBlock.gif                    
InBlock.gif            IndexStart 
= IndexEnd + 1;
InBlock.gif            ArrIndex 
= ArrIndex + 1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        IndexEnd 
= IndexEnd + 1;
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ==============================================//
None.gif//
遍历整棵树,进行节点的Checked的初始化设置//
None.gif//
==============================================//
None.gif
function  SetTree(node)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var childNodes = new Array();
InBlock.gif    childNodes 
= node.getChildren();
InBlock.gif    
var i = 0;
InBlock.gif    
var j = 0;
InBlock.gif            
InBlock.gif    
for( i=0 ; i<strArray.length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if( strArray[i] == node.getAttribute("ID") )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            saveCheckState(node);
InBlock.gif            node.setAttribute(
"checked","true");
InBlock.gif                    
//saveCheckState(node.getNodeIndex());
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
if( parseInt(childNodes.length) != 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for( j=0;j< parseInt(childNodes.length); j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for( i=0 ; i< parseInt(strArray.length); i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if( strArray[i] == childNodes[j].getAttribute("ID") )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    saveCheckState(childNodes[j]);
InBlock.gif                    childNodes[j].setAttribute(
"checked","true");
InBlock.gif                    
//saveCheckState(childNodes[j].getNodeIndex());
ExpandedSubBlockEnd.gif
                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            SetTree(childNodes[j]);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for( i=0 ; i<strArray.length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( strArray[i] == node.getAttribute("ID") )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                saveCheckState(node);
InBlock.gif                node.setAttribute(
"checked","true");
InBlock.gif                
//saveCheckState(node.getNodeIndex());
ExpandedSubBlockEnd.gif
            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ====================================================//
None.gif//
保存节点状态(保证在回传时节点状态依然存在)
None.gif//
====================================================//
None.gif
function  saveCheckState(el)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
if(el.getNodeIndex())
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        document.getElementById(
"TreeView1").queueEvent("oncheck",el.getNodeIndex().toString());
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
// ====================================================//
None.gif//
点击保存按钮时对所选中的节点进行整理
None.gif//
====================================================//
None.gif
function  FindChecked()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    strList 
= "";
InBlock.gif    
var ChildNodes = new Array();
InBlock.gif    ChildNodes 
= document.getElementById("TreeView1").getChildren();
InBlock.gif    
forvar i=0; i<ChildNodes.length; i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        CheckedAll(ChildNodes[i]);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
//alert(strList);
InBlock.gif
    doSelect(strList);
ExpandedBlockEnd.gif}

None.gif
function  CheckedAll(node)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
var i = 0;
InBlock.gif    
var strID;
InBlock.gif    
var childNodes = new Array();
InBlock.gif    childNodes 
= node.getChildren();
InBlock.gif            
InBlock.gif    
if( parseInt(childNodes.length) != 0 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
for( i=0; i<parseInt(childNodes.length); i++ )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            CheckedAll(childNodes[i]);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if( node.getAttribute("checked"== true )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            strID 
= node.getAttribute("id");
InBlock.gif            strList 
= strList + strID + ",";
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }
            
ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/wskaihd/archive/2006/10/25/539011.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值