大家好,我想问一个关于Dtree统计的问题,|- 路线
  |- 高速【2】
     |-高速路1【2】
        |-高速路1天津段
        |-高速路1北京段
     |-高速路2
  |- 国道【2】请注意红色部分和绿色部分,我的问题是:如何才能让高速只统计路,而非路段,而路统计路段呢????我已经实现了一个,但是不合要求,现在将代码写在这里,有用可以拿去!
这是我的代码,请看:
//统计每个父节点的子节点数量
dTree.prototype.GetNodesChildrenCounts = function(node) 
{
    //如果当前节点是叶子节点,那么直接返回1
    if(this.isLeafNode(node.id))
    {
return 1;
}
else //否则进行遍历所有节点,统计他的子节点数量
{
    var counts=0; //定义统计变量
    for (var n = 0; n < this.aNodes.length; n++) //遍历所有节点
        {
    if (this.aNodes[n].pid == node.id ) //判断是否为当前节点的子节点
    {
    counts +=this.GetNodesChildrenCounts(this.aNodes[n]);//还要统计孩子的孩子数量
    }
    }
    return counts; //返回本节点的孩子数量
}
}//遍历每个节点,统计数量
dTree.prototype.GetAllChildrenCounts = function () 
{
    for (var n = 0; n < this.aNodes.length; n++) //遍历所有节点
    {
        if(!this.isLeafNode(this.aNodes[n].id))
        {
            var counts=0;
            counts=this.GetNodesChildrenCounts(this.aNodes[n]);
            if(this.aNodes[n].pid != '-1'){
            this.aNodes[n].name=this.aNodes[n].name+'['+counts+']';
        }
        }
        
    } 

我想实现只统计路的功能,就是只统计到倒数第二级别的节点。
请帮忙!!!!

解决方案 »

  1.   

    counts +=this.GetNodesChildrenCounts(this.aNodes[n]);//还要统计孩子的孩子数量
    改成counts ++;
      

  2.   

    GetNodesChildrenCounts 修改部分:node.counts = 0; //定义统计变量
            for (var n = 0; n < this.aNodes.length; n++) //遍历所有节点
            {
                if (this.aNodes[n].pid == node.id) //判断是否为当前节点的子节点
                {
                    if(node.pid == '-1'){
                     node.counts = node.counts + this.GetNodesChildrenCounts(this.aNodes[n]);//还要统计孩子的孩子数量
                    }else{
                     node.counts = node.counts + 1;
                    }
                    
                }
                       }
            return node.counts; //返回本节点的孩子数量        
    GetAllChildrenCounts  修改部分:        for (var n = 0; n < this.aNodes.length; n++) //遍历所有节点
        {
            if(!this.isLeafNode(this.aNodes[n].id))
            {
                GetNodesChildrenCounts(this.aNodes[n]);
                
                this.aNodes[n].name = this.aNodes[n].name + '[' + this.aNodes[n].counts + ']';
              
            }
            
        }
      

  3.   

    思路就是给每个节点增加一个counts属性,再统计就好了
      

  4.   

    node.counts = node.counts + this.GetNodesChildrenCounts(this.aNodes[n]);//还要统计孩子的孩子数量
    错了 改为:
    node.counts = node.counts + this.aNodes[n].counts;
      

  5.   

    嗯,加一个属性,我也曾经想过,但是主管不太赞成,毕竟项目中利用dtree的地方很多,不想因为这个地方而破环了这个文件的独立性,和项目结合的太紧密,怕以后项目不好再扩展!所以没有采用。我看到里面一个_hc属性,或许是一个用得着的属性,再探探再说吧!!谢谢你!!
      

  6.   

    我现在想想,好像我上面的代码也是有问题的但是你要把根节点 放到最后一个add进去,像这样:var tree = new DTree();tree.add('gaosu','高速','luxian');
    ........最后
    tree.add('luxian','路线','-1');
      

  7.   

    那没办法了,其实dtree效率很低的