Expand my Community achievements bar.

Radically easy to access on brand approved content for distribution and omnichannel performant delivery. AEM Assets Content Hub and Dynamic Media with OpenAPI capabilities is now GA.

How to make a script dynamic while adding forminstances in a dynamic form

Avatar

Level 9

I have a form . Which has 8 pages (page 1-8).There is a radio button at the bottom of the page - 8 , If it's clicked as yes , then it will add an instance of Page 6,7,7&8.There are two Dropdowns in Page 6.

1st Dropdown is for Entity and the 2nd Dropdown is for State Value.Script is written on the onChange event for the Entity : Suppose a value is selected in the entity , then basing on that value some fixed valued dropdown list will appear for State field.If i add an instance for the Page 6,7,8 which is under Subform Section -1 Then the Drop Down for the State value is not working in the new instance created.I tried a lot but it's not working.Is there anyhelp for it ?

Script for Entity field

        TopmostSubform.Sub_Form_Section1.Page6.Entity::change - (JavaScript, client)
var newVal = this.boundItem(xfa.event.newText);
switch (newVal)
{
    case "A":
        TopmostSubform.Sub_Form_Section1.Page6.State.clearItems();
        TopmostSubform.Sub_Form_Section1.Page6.State.addItem('x');
        break;

    case "B":
        TopmostSubform.Sub_Form_Section1.Page6.State.clearItems();
        TopmostSubform.Sub_Form_Section1.Page6.State.addItem('a');
        TopmostSubform.Sub_Form_Section1.Page6.State.addItem('a');
        TopmostSubform.Sub_Form_Section1.Page6.State.addItem('c');
        TopmostSubform.Sub_Form_Section1.Page6.State.addItem('d');
        TopmostSubform.Sub_Form_Section1.Page6.State.addItem('e');
        TopmostSubform.Sub_Form_Section1.Page6.State.addItem('f');
        break;

       
    default:
    break;
}

2 Replies

Avatar

Level 10

Hi,

It is a bit awkward without the form, but basically the script does not know which page6 your are referring to. When there is a single instance of the page, it can be referenced by "...page6.State", but when there are several instances of the page, they are referenced by "...page6[0].State", "...page6[1].State", "...page6[2].State", etc.

So the task is to find out which instance of page 6 the dropdown is in and then reference that instance. You will see from above that the index is zero based, it is a good idea to include a line that will print the output (i) to the Javascript Console (Press Control+J when previewing). You can comment this out (//) or delete it after testing.

The next thing is to resolve the node when combining the page SOM and the particular instance of it (i).

So:

var i = this.parent.index;

console.println("i: ", i); // when previewing press Control+J to see the output

var currentPage6 = xfa.resolveNode("TopmostSubform.Sub_Form_Section1.Page6[" + i + "]");

var newVal = this.boundItem(xfa.event.newText);
switch (newVal)
{
    case "A":
        currentPage6.State.clearItems();
        currentPage6.State.addItem('x');
        break;

    case "B":
        currentPage6.State.clearItems();
        currentPage6.State.addItem('a');

          ...
        break;

       
    default:
    break;
}

It may need some tweaking, but should be close to working.

Good luck,

Niall

Avatar

Level 9

Thanks a lot

Niall O'Donovan for your quick response . I did some editing , then it worked .Keep replying quick . I'll come again with more weird problems.

Cheers ...

'