function Tree(name,parent,child){
this.name = name;
this.parent = parent || '';
this.children = child || [];
this.addNode = function (parent){
this.children = parent;
}
this.addChild = function (childName, parentName){
this.children.push(new Tree(childName, parentName));
}
this.addChildrenForNode = function (nodeChild, nodeName) {
if (this.name == nodeName) {
this.addChild(nodeChild, nodeName);
}
else if (this.children != null && this.children.length > 0) {
(function () {
var subTree = arguments[0];
var indicatorName = arguments[1];
for (var j = 0; j < subTree.length; j++) {
if (subTree[j].name == indicatorName) {
subTree[j].addChild(nodeChild, indicatorName);
break;
}
else if (subTree[j].children != null && subTree[j].children.length > 0)
{
arguments.callee(subTree[j].children, nodeName);
}
}
} ) (this.children, nodeName);
}
}
}
function getNodeAllChildren(node) {
if (node.children != null && node.children.length > 0) {
for (var i = 0; i < node.children.length; i++) {
childrenNames.push(node.children[i].name);
if (node.children[i].children != null && node.children[i].children.length > 0) {
getNodeAllChildren(node.children[i]);
}
}
}
}
function deleteNodeAllChildren(tree, childName) {
if (tree.name == childName) {
getNodeAllChildren(tree);
tree.children =[];
}
else if (tree.children != null && tree.children.length > 0) {
return(function () {
var subTree = arguments[0];
var indicatorName = arguments[1];
for (var j = 0; j < subTree.length; j++) {
if (subTree[j].name == indicatorName) {
getNodeAllChildren(subTree[j]);
subTree[j].children = [];
break;
}
else if (subTree[j].children != null && subTree[j].children.length > 0)
{
arguments.callee(subTree[j].children, childName);
}
}
} ) (tree.children, childName);
}
return tree;
}
function deleteDrillDownIndicator(tree, indicator) {
if (tree.name == indicator) {
tree.children =[];
}
else if (tree.children != null && tree.children.length > 0) {
return(function () {
var subTree = arguments[0];
var indicatorName = arguments[1];
for (var j = 0; j < subTree.length; j++) {
if (subTree[j].name == indicatorName) {
deleteNodeAllChildren(tree, subTree[j].parent);
break;
}
else if (subTree[j].children != null && subTree[j].children.length > 0)
{
arguments.callee(subTree[j].children, indicator);
}
}
} ) (tree.children, indicator);
}
return tree;
}
var tree = new Tree("Indicator"); // create a tree (or a portion of a tree) with root "A" and empty children
tree.addChildrenForNode("1A","Indicator"); // A -> B1
tree.addChildrenForNode("1B","Indicator"); // A -> B2
tree.addChildrenForNode("2AA","1A"); // add this sub tree under A->B1
tree.addChildrenForNode("2AB","1A");
tree.addChildrenForNode("2BA","1B"); // add this sub tree under A->B1
tree.addChildrenForNode("2BB","1B");
tree.addChildrenForNode("3BAA","2BA");
tree.addChildrenForNode("3BAB","2BA");
var childrenNames = [];
console.log(JSON.stringify(tree));
deleteDrillDownIndicator(tree, "2BA");
console.log(JSON.stringify(childrenNames));
childrenNames = [];
console.log(JSON.stringify(tree));
deleteDrillDownIndicator(tree, "1B");
console.log(JSON.stringify(childrenNames));
console.log(JSON.stringify(tree));
//findChild(tree,"2BA");
其中deleteDrillDownIndicator 为项目所需,当在Data Warehouse Query 中执行Drill-down操作时,产生不同level的新Drill-down indicator,当删除特定indicator时,该indicator父节点所有子树都需要被删除。