javascript复选框三态树实例

<html>
<script language=Javascript>

 TreeNode = function(name, data, parentNode)
 {
  this.name = name;
  this.parentNode = parentNode;
  this.data = data;
  this.childNodes = [];

  this.addChildNode = function(node)
  {
   node.parentNode = this;
   this.childNodes.push(node);
   return this;
  }

  this.fullSelected = function()
  {
   for(var index in this.childNodes)
   {
    if(!this.childNodes[index].data.checked)
    {
     return;
    }
   }
   this.data.checked = true;
   if(this.parentNode){
    
    this.parentNode.fullSelected();
   }
  }

  this.select = function()
  {
   this.data.checked = true;
   for(var index in this.childNodes)
   {
    this.childNodes[index].select();
   }
  
   if(this.parentNode)
   {
    this.parentNode.fullSelected();
   }
  
  }

  this.cancel = function()
  {
   this.data.checked = false;
   if(this.parentNode)
   {
    this.parentNode.cancel();
   }
  }

  this.cancel2 = function()
  {
   this.data.checked = false;
   for(var index in this.childNodes)
   {
    this.childNodes[index].cancel2();
   }
  }

  this.init = function()
  {
   var instance = this;
   instance.data.instanceNode = this;
   instance.data.onclick = function()
   {
    if(this.checked == true)
    {
     this.instanceNode.select();
    }
    else
    {
     this.instanceNode.cancel();
     this.instanceNode.cancel2();
    }
   }
  }

  this.each = function(itFun)
  {
   itFun(this);
   for(var index in this.childNodes)
   {
    this.childNodes[index].each(itFun);
   }
  }

  this.init();
 }

function $(id)
{
 return document.getElementById(id);
}

 

var allx = null;
function main()
{
 allx = new TreeNode("all", $('all'));

 var a = new TreeNode("a", $('a'));
 var a1 = new TreeNode("a1", $('a1'));
 var a2 = new TreeNode("a2", $('a2'));
 var a3 = new TreeNode("a3", $('a3'));

 a.addChildNode(a1).addChildNode(a2).addChildNode(a3);

 var b = new TreeNode("b", $('b'));
 var b1 = new TreeNode("b1", $('b1'));
 var b2 = new TreeNode("b2", $('b2'));
 var b3 = new TreeNode("b3", $('b3'));
 b.addChildNode(b1).addChildNode(b2).addChildNode(b3);

 var c = new TreeNode("c", $('c'));
 var c1 = new TreeNode("c1", $('c1'));
 var c2 = new TreeNode("c2", $('c2'));
 
 var c3 = new TreeNode("c3", $('c3'));
 c.addChildNode(c1).addChildNode(c2).addChildNode(c3);

 allx.addChildNode(a).addChildNode(b).addChildNode(c);
}

 

/** 获得叶子节点的值,以数组返回 */
function getValues(treeNode){
 var values = [];
 var itFun = function(node)
 {
  if(!node.childNodes || node.childNodes.length == 0)
  {
   if(node.data.checked){
    values.push(node.data.value);
   }
  }
 }

 treeNode.each(itFun);
 return values;
}

</script>

<body οnlοad="main()">
ALL<input type=checkbox id="all" ><br><br><br>

A<input type=checkbox id="a" ><br>
<input type=checkbox id="a1" value="1"><input type=checkbox id="a2"  value="2"><input type=checkbox id="a3"  value="3"><br><br>

B<input type=checkbox id="b" ><br>
<input type=checkbox id="b1" value="4"><input type=checkbox id="b2" value="5"><input type=checkbox id="b3" value="6"><br><br>

C<input type=checkbox id="c" ><br>
<input type=checkbox id="c1" value="7"><input type=checkbox id="c2" value="8"><input type=checkbox id="c3" value="9"><br><br>


<input type="button" value="getValues" οnclick="alert(getValues(allx))"/>
</body>
<html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值