ferr
6 Dec 2007, 5:18 PM
I'm trying to iterate through child nodes of a tree to remove nodes that meet requirements for removal via either eachChild or childNodes. The problem is that I'm pretty sure the structure of both objects is being altered to the point of malfunction (i.e. it will try to remove a node that lacks definition because the object has shifted around nodes and mixed things up).
Firebug reports the error as "node has no properties." referring to "return node.isAncestor(this)". I was hoping to simply do something like wrap my node interaction within the loop with something like if node == undefined break out of the loop and start over because the object is fuxored, but I can't seem to find the correct way to make that comparison with the node.
code:
var done = false;
while(!done)
{
done = true;
for(var i = 0; i < 10; i++)
{
if(defaultColumnRoot.childNodes[i] != null)
{
if(userColumnRoot.contains(userColumnRoot.findChild("text", defaultColumnRoot.childNodes[i].attributes.text)))
{
defaultColumnRoot.removeChild(defaultColumnRoot.childNodes[i])
}
}
else
done = false; //hit a bump, break out and go again
}
// defaultColumnTree.root.eachChild(function(child)
// {
// done = false; //it has children, so it isn't done
// if(child != undefined)
// {
// if(userColumnRoot.contains(userColumnRoot.findChild("text", child.attributes.text)))
// defaultColumnRoot.removeChild(child)
// }
// });
}
What is a good method to go about doing this?
thanks
edit:
this doesn't work, and it makes me wonder if something needs to be called to update the child nodes:
var done = false;
while(!done)
{
done = true;
for(var child in defaultColumnRoot.childNodes)
{
if(userColumnRoot.contains(userColumnRoot.findChild("text", defaultColumnRoot.childNodes[child].attributes.text)))
{
defaultColumnRoot.removeChild(defaultColumnRoot.childNodes[child]);
done = false;
break;
}
}
}
Firebug reports the error as "node has no properties." referring to "return node.isAncestor(this)". I was hoping to simply do something like wrap my node interaction within the loop with something like if node == undefined break out of the loop and start over because the object is fuxored, but I can't seem to find the correct way to make that comparison with the node.
code:
var done = false;
while(!done)
{
done = true;
for(var i = 0; i < 10; i++)
{
if(defaultColumnRoot.childNodes[i] != null)
{
if(userColumnRoot.contains(userColumnRoot.findChild("text", defaultColumnRoot.childNodes[i].attributes.text)))
{
defaultColumnRoot.removeChild(defaultColumnRoot.childNodes[i])
}
}
else
done = false; //hit a bump, break out and go again
}
// defaultColumnTree.root.eachChild(function(child)
// {
// done = false; //it has children, so it isn't done
// if(child != undefined)
// {
// if(userColumnRoot.contains(userColumnRoot.findChild("text", child.attributes.text)))
// defaultColumnRoot.removeChild(child)
// }
// });
}
What is a good method to go about doing this?
thanks
edit:
this doesn't work, and it makes me wonder if something needs to be called to update the child nodes:
var done = false;
while(!done)
{
done = true;
for(var child in defaultColumnRoot.childNodes)
{
if(userColumnRoot.contains(userColumnRoot.findChild("text", defaultColumnRoot.childNodes[child].attributes.text)))
{
defaultColumnRoot.removeChild(defaultColumnRoot.childNodes[child]);
done = false;
break;
}
}
}