on 05-11-2007 7:00 PM
Hi all,
I'm trying to remove some items from a context node.
Pretty new with webdynpro, so all suggestions are welcome.
//Reset table
IWDNode node = wdContext.currentContextElement().node().getChildNode("Projects", 0);
wdContext.currentContextElement().setTest("Size: "+node.size());
node.moveFirst();
for(int i=0; i < node.size(); i++){
wdContext.currentContextElement().setTest("Success: "+node.removeElement(wdContext.currentContextElement()));
node.moveNext();
}
This code will not work, I don't know why, but removeElement returns false.
Regards,
Adri
Thanks for your replies, things are much clearer now.
One problem I couldn't solve with this code:
for(int i=0; i < node.size(); i++){
node.removeElement(wdContext.nodeProjects().getProjectsElementAt(i));
}
Not all the elements are removed after the for loop. If I check it with node.size() still 1 item is in the node. It also appears in my table.. (which is a little annoying..)
Did someone know why?
@Abhijeet, I will try your code later, because testing isn't possible right now. Thanks anyway!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Adri,
It's a common idiom that in collections with index-based access you must remove lements in reverse order (from tail to head):
for(int i=node.size() - 1; i >= 0; i--){
node.removeElement(wdContext.nodeProjects().getProjectsElementAt(i));
}
This solves "fundamental" problem with your code: on every iteration you are reducing size of collection but increasing index at the same time, so some elements "survive". Here is how "forward remove" should work (also I don't recommend it due to being non-obvious for occasional code reader):
int count = node.size();
while( count-- > 0 ) {
node.removeElement(wdContext.nodeProjects().getProjectsElementAt(0));
}
Notice that we remove zero element on every step and repeat this operation N time where N is size of node.
Valery Silaev
SaM Solutions
Thanks for your reply.
The problem is solved now!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Adri,
try this:
wdContext.currentContextElement().setTest("Success: "+node.removeElement(node.current<give Node name>Element());
instead:
wdContext.currentContextElement().setTest("Success: "+node.removeElement(wdContext.currentContextElement()));
because in your case you are reffering to the root context's element but we want to remove the node's element
regards
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try this...
wdContext.nodeProjects().removeElement(wdContext.nodeProjects().getProjectsElementAt(i));
Don't forget to decrement i each time you remove an element (because the size() method will update each time).
(you don't need to worry about moveFirst and moveNext this way)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
86 | |
10 | |
10 | |
9 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.