Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Auto fill a textfield in subform

Avatar

Level 1

Ok, so I have this table on page 1 of a form.

I have a subform called Enrollment on page 4 of said form.

I add another instance of Enrollment for every row filled in on the table on page 1.

i.e. John Brown     Grade 4     Male

Jane Brown          Grade 5     Female

The student's name gets populated in the instance of Enrollment that is visible because there is always one student.

However, when I use scripting to create another instance of Enrollment, I can't get the student's names to populate(auto-fill) the names on the new instance of the subform Enrollment.

I have tried all this...

Enrollment.firstname[1].rawValue = first2.rawValue;

      Enrollment[1].middlename.rawValue = middle2.rawValue;

        oSubForm.lastname.rawValue = last2.rawValue;

oSubForm is calculated by...

var nIndex = 1;

var sSOM = "Enrollment[" + nIndex + "]";

var oSubform = xfa.resolveNode(sSOM);

I even tried xfa.resolveNode("Enrollment[1].lastname").rawValue

NOTHING is filling in the second subform fields. How do I get to them?

I can't see what they would be called because the instance of the form is generated on the fly by the dropdown!

Enrollment.instanceManager.addInstance(true);

I have searched and searched, but there is no definitive way I have seen to call these new fields.

Anyone?

0 Replies

Avatar

Level 7

Here's an example of this that works. You can modify it to fit your needs. Maybe put the code from the click event somewhere else. Of course, bear in mind that my example doesn't worry about looking pretty or anything. So, if you want adding and removing rows to be more precise, you'll have to add code for that. Also, you'd want to make sure you don't add rows if you aren't exiting the last line of the table.

example.png

Avatar

Level 1

For some reason, it does not work.

The rows in the table are not dynamic, there are 6 of them.

If they fill out the First Middle and Last names (called first1 for row 1, first2 for row 2...and so on) then the change event on the gender creates the instance of the form.

This is the code on row 1

var sNewSel = this.boundItem(xfa.event.newText);

switch (sNewSel)

{

  case "1": // Female

      Enrollment.firstname.rawValue = first1.rawValue;

      Enrollment.middlename.rawValue = middle1.rawValue;

      Enrollment.lastname.rawValue = last1.rawValue;

    break;

  case "2": // Male

      Enrollment.firstname.rawValue = first1.rawValue;

      Enrollment.middlename.rawValue = middle1.rawValue;

      Enrollment.lastname.rawValue = last1.rawValue;

    break;

 

  default: // unknown value -- do nothing

 

      Enrollment.firstname.rawValue = first1.rawValue;

      Enrollment.middlename.rawValue = middle1.rawValue;

      Enrollment.lastname.rawValue = last1.rawValue;

    break;

}

For the gender drop down in row 2 I have this code...

var sNewSel = this.boundItem(xfa.event.newText);

switch (sNewSel)

{

  case "1": // Female

   Enrollment.instanceManager.addInstance(true);

  

      Enrollment.firstname[1].rawValue = first2.rawValue;

      Enrollment[1].middlename.rawValue = middle2.rawValue;

       xfa.resolveNode("form1.Enrollment[1].lastname").rawValue = xfa.resolveNode("form1.#subform.Table1.Row1[1].last2").rawValue;

    break;

  case "2": // Male

   Enrollment.instanceManager.addInstance(true);

    break;

 

  default: // unknown value -- do nothing

 

     Enrollment.instanceManager.addInstance(false);

    break;

}

As you can see, I have tried a lot of ways, however, none of the fields are populating.

What am I doing wrong?

Avatar

Level 1

printscreen2.jpgHere is my Enrollment subform....set up to have 6 instances.

Avatar

Level 7

What are your rows named? Row1[0], Row1[1], etc. or Row1, Row2, etc?

Also, I see a lot of different naming schemes in your code. Remember that as the script runs, if something doesn't work, then whole thing fails, and no other lines attempt to run.

Avatar

Level 1

Yes...except row1. I can't change it either or I don't know how.

Row1

Row1[1]

Row2

Row3

Row4

etc....

Avatar

Level 1

HOLY CRAP!!!!

That was it...I did not know that if something fails the script stops.....OH MY GOD!!!

I put just this and it worked!!!!

xfa.resolveNode("form1.Enrollment[1].lastname").rawValue = xfa.resolveNode("form1.#subform.Table1.Row1[1].last2").rawValue;

JESUS a lot of time was wasted by not knowing the script would just STOP!!!

Why did they do that?

Avatar

Level 7

As best as I understand it, Javascript doesn't "compile" so there's no syntax check or anything. But, when you have an event occur, it processes the code and stops once you reach a bad line like code interpreter.

Avatar

Level 1

Awesome. Thank you so much!

One more question. In my code, I create a new instance no matter which gender they pick....problem is if they make a mistake...new subForm gets created and throws everything off.

How do I say "don't do this if you just did"?

if Enrollment[3] exists?

don't create again....

Avatar

Level 7

If you renamed your rows to Row1, then you can put this condition before all of the actions you do when you add the subform instance

if (subform.instanceManager.count <= this.parent.index) {

    subform.instanceManager.addInstance();

    //and whatever else

}

Now, you might notice that the user can just go to the bottom and start swapping back and forth creating instances. If someone wants to be "that guy" then they still get stopped once they reach the sixth instance of the subforms. There's not a way to remove them in your current form, so you might consider a little button in the top right corner of the subform to do that, but then there's other confusion involved since the Gender in each row seems to make a difference.