How do you spawn a template from a button on an already spawned template and rename the field on it?

Hi. I've run into a problem and would really appreciate some help. I'm pretty new to javascript. Currently I have a button on a regular page that runs the javascript to spawn a template called 'newpage'

var a = this.getTemplate("newpage");
a.spawn({nPage: pageNum + 1, bRename: true, bOverlay: false});

on that template page, I have only one field and another button with the same javascript to add the same template again.

It works find when adding sequential, newly spawned pages, but when I go back to a page I've already spawned and hit the button again, the template is spawned but the text I enter in the field on the new page overwrites the field from the initial spawned template from that earlier instance.

I can see that the spawned pages are amending the field name with #0 and so on, but I can't figure out how to stop the overwriting in the field. Been scouring the forum for a solution for over 6 hours with no luck. Help would be so appreciated.

Thanks in advance - Jon.


Jon Pike


4 Answers

Voted Best Answer

Hi Jon,

Change your deletebutton script to this:

var pg=this.pageNum
this.pageNum--;
this.deletePages(pg,pg);

    for(var i = this.numFields - 1; i > -1; i--)
    {
    var fieldName = this.getNthFieldName(i);
    if (this.getField(fieldName).page==this.pageNum &&
    this.getField(fieldName).name!="DeleteButton"&&
     (/SpawnButton/.test(this.getField(fieldName).name) ||
     /DeleteButton/.test(this.getField(fieldName).name)))
    {this.getField(fieldName).display=display.noPrint}
    }

var num=0; 
for(var i = this.numFields - 1; i > -1; i--)
{
var fieldName = this.getNthFieldName(i);
if (/SpawnButton/.test(this.getField(fieldName).name)) 
{num=num+1;}
}

if (num==1)
{this.getField("SpawnButton").display=display.noPrint}
else
{this.getField("SpawnButton").display=display.hidden}


By David Dagley   

When you go back to the same button you are doing the exact same thing so an exact replica of the first spawned page is being created, including field names. To avoid this, I usually add a couple of scripts to my button. One, to hide the original button, another to hide the new button (after it is activate), and another to advance to the next page. This way, there is only ever one button visible for spawning another page and the field names will be different because the page number prefix will change. Assume your button name is Button1. Use this script in the button:

this.getField("Button1").display=display.noPrint;
var a = this.getTemplate("newpage");
a.spawn({nPage: pageNum + 1, bRename: true, bOverlay: false}); 
this.getField("Button1").display=display.hidden;
event.target.display=display.hidden;
this.pageNum=this.pageNum+1;


David Dagley   

The problem point in the code is the nPage argument. It always spawns a page after the current page.

To correct it, you would use this.numPages+1, and then it will always spawn at the end of the document, making the line of code look like this:

a.spawn({nPage: this.numPages+1, bRename: true, bOverlay:false}) ;

You also mention deleting a page. This will mess up things as soon as you spawn another page (unless you deleted the last page of the document). This is caused by the prefix generated when bRename is set to true:

Pn.template

where

• "P" is fixed • n is the logical page number of the spawned page (0-based) • template is the name of he Template you use

So, when you delete a page, the n value in the prefix is no longer the same as the logical page number (which is not critical per se). But when you spawn a new page, the n value becomes the same as the one of the last page number of the last page before spawning.

In order to keep things in order, you would essentially have to save all the data of the pages following the page you delete, delete all pages from the one to be deleted until the end of the document, spawn the remaining pages again, and fill the data back into the newly spawned pages.

Hope this can help.

Max Wyss.


Max Wyss   

Set up 2 buttons on your template called "SpawnButton" and "DeleteButton". The spawn button will be visible or non-printable, while the delete button will be hidden. The script in the spawn button will:

1) Spawn the page after the current page.

2) Advance the focus to the spawned page.

3) Hide the original spawn button.

4) Display the spawn button on the spawned page.

5) Display the delete button the spawned page.

The delete button will:

1) Delete the current page.

2) Advance the focus to previous page (the one before the deleted page).

3) Show the spawn and delete buttons on this page (unless it is the original page, in which case it will only show the spawn button.

This will ensure only one spawn button is visible at one time, which will spawn the current page after the current page and only one delete button is visible at one time to delete the current page. This user will be forced to spawn and delete pages in order. In this example the template name is "MyTemplate". This is the spawn button script:

var sb=event.target.name;
var db=event.target.name.replace(/Spawn/,"Delete");

    this.getField("SpawnButton").display=display.noPrint;
    this.getField("DeleteButton").display=display.noPrint;
    this.getTemplate("MyTemplate").spawn ({nPage:this.pageNum+1,bOverlay:false});

    this.getField("SpawnButton").display=display.hidden;
    this.getField("DeleteButton").display=display.hidden;
    this.getField(sb).display=display.hidden;
    this.getField(db).display=display.hidden;

    this.pageNum=this.pageNum+1;

This is the delete button script:

var pg=this.pageNum
this.pageNum--;
this.deletePages(pg,pg);


    for(var i = this.numFields - 1; i > -1; i--)
    {
    var fieldName = this.getNthFieldName(i);
    if (this.getField(fieldName).page==this.pageNum &&
    this.getField(fieldName).name!="DeleteButton"&&
     (/SpawnButton/.test(this.getField(fieldName).name) ||
     /DeleteButton/.test(this.getField(fieldName).name)))
    {this.getField(fieldName).display=display.noPrint}
    }


David Dagley   


Please specify a reason: