how to remove content children in webapp | Community
Skip to main content
Level 3
June 7, 2021
Question

how to remove content children in webapp

  • June 7, 2021
  • 2 replies
  • 2172 views

I am building a webapp for editing email content, and have a problem when saving content child elements; I can modify the children via ctx in the webapp but, when I save via Storage object, removed children still remain. That is, if I load the following content:

 

<content> <child id="1" ... /> <child id="2" ... /> <child id="3" ... /> </content>

 

And manipulate ctx in webapp to change order of 1 and 2, and remove 3

 

<content> <child id="2" ... /> <child id="1" ... /> </content>

 

After passing the Storage Object the resulting children on the stored content is 2,1,3

If I change to 3,2 the resulting children on the stored content is 3,2,3

 

So my conclusion are: child elements are inserted or updated in the storage processes - excising ones are not removed. Anyone have a workaround or tips?

 

I am thinking of doing an enabled attribute and set to false for all "excedeing" children but is seems like a very unnecessary step, and making the solution more complicated that needed.

 

For info, my code for manipulating ctx is as follows:

var ids = "2,1".split(",") delete ctx.content.child for(var i=0;i<ids.length;i++){ ctx.content.appendChild(<child id={i} ... />) }

 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

2 replies

CedricRey
Level 5
June 7, 2021

Hello Jonas,

 

I'm not sure this is a good way to delete child elements from a XML.

Can you try to user the removeChild method instead of 'delete' instruction ?

Here a code but I'm not 100% sure (because iterate and delete from a list is always not simple... but as I use the element and not the index for removeChild, it should work ?)

var allChilds = ctx.content.getElements('child'); for(var i=0;i<allChilds.length;i++){ ctx.content.removeChild(allChilds[i]) }

Then you can add your new child.

Or if you want to replace a child, you can use 'replaceChild' function.

If it doesn't work I'll try on my side.

 

Cedric

RBE_jonasAuthor
Level 3
June 7, 2021

Dear Cédric, thank you for your kind reply. I get TypeError: ctx.content.getElements is not a function However I can do:

 

for(var i=0;i<ctx.content.child.length();i++){ ctx.test.child[i] = ctx.content.child[i] // ctx.content.removeChild(ctx.content.child[i]) }

 

and ctx.test is populated, hower removeChild causes the same type of error. ctx.content.appendChild(<child ... />) seems to work fine thought. The mysterium deepens...

Sukrity_Wadhwa
Community Manager
Community Manager
June 23, 2021

Hi @rbe_jonas,

Were you able to resolve this query or do you still need more help here? Do let us know.

Thanks!

Sukrity Wadhwa