cancel
Showing results for 
Search instead for 
Did you mean: 

Delete node elements

Former Member
0 Kudos

I have created 5 elements of a node . I want to delete nodes. But it is giving error .

Context

\

sample (node ) [ cardinality 0...n , selection 0..1 ]

\

name (attr)

number(attr)

code :

public void onActioncreate(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActioncreate(ServerEvent)

for(int i=0;i<5;i++){

IPrivateClearnodeElements.ISampleNode node =wdContext.nodeSample();

IPrivateClearnodeElements.ISampleElement ele = node.createSampleElement();

ele.setName("Name "+i +" : " );

ele.setNumber(i+"");

node.addElement(ele);

}

//@@end

}

public void onActionclear(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )

{

//@@begin onActionclear(ServerEvent)

wdComponentAPI.getMessageManager().reportSuccess(wdContext.nodeSample().size()+"");

for(int i=0;i<5;i++)

{

try{

wdContext.nodeSample().removeElement(wdContext.nodeSample().getSampleElementAt(i));

wdComponentAPI.getMessageManager().reportSuccess("Deleted : "+ i+"");

}

catch(Exception e)

{

wdComponentAPI.getMessageManager().reportSuccess(e.getMessage()" : " i+"");

}

}

//@@end

}

output :

5

Deleted : 0

Deleted : 1

Deleted : 2

Index: 3, Size: 2 : 3

Index: 4, Size: 2 : 4

Why it is not able to delete the last two node elements ????????????????//

Also any other way is there to delete elements of the node?

Srini

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi srinivasa,

You have used the code to delete the element

for(int i=0;i<5;i++)

{

try{

wdContext.nodeSample().removeElement(wdContext.nodeSample().getSampleElementAt(i));

wdComponentAPI.getMessageManager().reportSuccess("Deleted : "+ i+"");

}

catch(Exception e)

{

wdComponentAPI.getMessageManager().reportSuccess(e.getMessage()" : " i+"");

}

Now.. See getSampleElementAt(i) method returns the element of i th element. On that case you have to use

getSampleElementAt(0). In that case it wiil return 0ih element end 0th element will be deleted. After deletion the 0th element 1st element will move to the 0th position.

So. in that way you can delete all the elements.

Answers (3)

Answers (3)

former_member485701
Active Participant
0 Kudos

Hi,

The problem here is in the way you are deleting, you should always delete elements of

node from end.

Because when you delete an element, it's node size gets reduced that's why when you go to

index 4 it's size it less than 4 and you index out of bound exception.

So, change your loop to

for(int i= wdContext.node<name>().size()-1; i>0;i--){

// now write your code of removing the elements here

}

Regards,

Praveen

Former Member
0 Kudos

Hi,

I dont know why you are getting the exception , but to clear all the elements you can use wdContect.nodeSample().invalidate() or wdContext.nodeSample().bind(null);

Regards,

Sudhir

Former Member
0 Kudos

Hi,

Try to iterate the loop in reverse order.

Ex:



for(int i=4;i>=0;i--)
{


try{

wdContext.nodeSample().removeElement(wdContext.nodeSample().getSampleElementAt(i));
wdComponentAPI.getMessageManager().reportSuccess("Deleted : "+ i+"");
}
catch(Exception e)
{
wdComponentAPI.getMessageManager().reportSuccess(e.getMessage()+" : "+ i+"");

}

}

Regards

Ayyapparaj

Former Member
0 Kudos

Thanks ,

Ayyapparaj for immediate response .

I came to know where is the mistake , thanks santhanu and praveen.

Srini