大家好,我想问一个关于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+']';
}
}
}
}
我想实现只统计路的功能,就是只统计到倒数第二级别的节点。
请帮忙!!!!
|- 高速【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+']';
}
}
}
}
我想实现只统计路的功能,就是只统计到倒数第二级别的节点。
请帮忙!!!!
改成counts ++;
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 + ']';
}
}
错了 改为:
node.counts = node.counts + this.aNodes[n].counts;
........最后
tree.add('luxian','路线','-1');