Expand my Community achievements bar.

How to reset a subform to it's initial state ?

Avatar

Level 2

Hi,

I've been searching all over the forum for an answer to my question, but I couldn't find anything...

So basically, what I want to do is reset just a part of my form (I even want to reset the duplicated subforms I created).

So far, I tried this on a clear button :

var newXML = "<?xml version='1.0' encoding='UTF-8'?><form1></form1>"; // form1 is the root node of my form.

xfa.datasets.data.loadXML(newXML,1,1);

It resets ALL my form (the value in the fields AND the duplicated subforms).

How can I specify to reset only one section of my form ?

Thanks alot for you time and help

7 Replies

Avatar

Level 10

Use the resetData() method.

If you don't provide any parameters it resets the whole form, otherwise only those objects and its childs.

xfa.host.resetData(Subform1.somExpression) resets only Subform1 for example.

Avatar

Level 2

Hi Radzmar,

Thanks for your answer. I already used the resetData with somExpression in my forms and it works fine, but it doesn't "kill" all the subforms I dynamically created with instanceManager.insertInstance.

I tried something new with the xfa.datasets.data.loadXML, but it doesn't work perfectly. Let me give you an example.

If you specify the path you want to reset BEFORE the .loadXML and you add "xfa.form.remerge();" after, it only resets (clear all the fields AND kill all the subforms created dynamically) the path you specified.

xfa.datasets.data.form1.page1.loadXML("<form1/>", true, true);

xfa.form.remerge();

The thing is, it also hides all the subforms I dynamically set to visible on all the other pages of my form. Since it's kind of hard to explain, I've linked a document to show you the problem.

https://workspaces.acrobat.com/?d=tpyBDPbEiA1hWS-FkPtgLg

Thanks and have a nice day !

Jonathan

Avatar

Level 10

You can also add a list the the resetData method.

Try xfa.host.resetData(xfa.host.resolveNodes("Subform1[*]")).

The resolveNodes() returns a list of objects with the same name.

Avatar

Level 2

Hi,

Thanks for your answer. I tried your suggestion but I can't get it to work properly (I get this error : xfa.host.resolveNodes is not a function).

Anyway, i'm developping a function which will reset all the fields and delete all the duplicated subforms (see in the myScript object) : https://workspaces.acrobat.com/?d=xd*FX-aRfUt*M4orAeWwMQ

I'm just having one last bug. I get this error when I delete the duplicated subforms : "Index value is out of bounds". I know what it means, but I don't know how to fix it.

Have a nice day

Jonathan

Avatar

Level 10

Hi,

you get the out of bounds error because you use the nodes count in your for expression that is one more than the available index.

If you use ...nodes.length -1; it will work correct.

Anyway, your current script will never reach the child nodes so I created a new one.

This is a recursive for loop that will reset all subforms to its initial state.

function resetSection(vNode) {

    if (vNode.className === "subform") {

              xfa.host.resetData(vNode.somExpression);

        if (vNode.instanceManager.count > vNode.occur.min) {

                  vNode.instanceManager.setInstances(vNode.occur.min);

        }

    }

    for (var i = 0; i < vNode.nodes.length; i += 1) {

        resetSection(vNode.nodes.item(i));

    }

}

Avatar

Level 2

Hi Radzmar,

You're a genius !!! It works perfectly. Thanks

Jonathan

Avatar

Level 2

Hi,

After using your function, I noticed some lag issues.

1. There is way too many iteration because resetSection is called again for each object instead of only on the subforms.

2. resetData needed to be called only once on the main subform (the first vNode) and not on all the subsequent subforms in the hierarchy (this caused the lag issues).

So I did my best to do the corrections and it works fine.

[code]

function resetSection(vNode, resetData)

{

     if (resetData === undefined || resetData != 0)

     {

          xfa.host.resetData(vNode.somExpression);

     }

     for (var i = 0; i < vNode.nodes.length; i++)

     {

          if (vNode.nodes.item(i).className === "subform")

          {

               if (vNode.nodes.item(i).instanceManager.count > vNode.nodes.item(i).occur.min)

               {

                    vNode.nodes.item(i).instanceManager.setInstances(vNode.nodes.item(i).occur.min);

               }

               resetSection(vNode.nodes.item(i), 0);

          }

     }

}

[/code]