Expand my Community achievements bar.

how to count subforms and add a value to a dropdown?

Avatar

Former Community Member
Hello!

I want to make a form with a subform that the user can dublicate by clicking a button (JS: ... addInstance(1);)



after he dublicates the subform serval times and entered some text in the fields i wanto to count the subforms and go in every subform to get a value from a field.



Example:

A form with a subform for Firstname and Lastname.

the user dublicates the subform 5 times and filled the fields.

now at the end i want to have a dropdown with all Lastnames from the 5 subforms.



How can i make this? I looked in serval Samples but i dont find an example for it.



Thanks for Help.



Greetings A. Peters
5 Replies

Avatar

Former Community Member
Let's build your example:



First, you would need a subform (NameSubform) with two fields, one for the first name and one for the last name (FirstName and LastName, respectively). Let's say you also add two buttons to this subform called Add and Del which, when clicked, add and remove an instance of NameSubform, respectively. In particular, the Del button removes the instance of NameSubform to which it pertains.



Then you would need a second subform (let's call this one TotalSubform), probably located below NameSubform, which contains a drop down list. Let's call it NameDropList.



The next step is to make sure that the TotalSubform moves up or down whenever the set of instances of NameSubform is modified. To do this, you could simply make the page subform (let's give it a name, say Page1Subform) have a flowed layout. You can do this by using the Content property on the Object palette's Subform tab. Set it to
Flowed.



Now you need to make NameSubform dynamic so that you can add and remove instances. You can do this by checking the
Repeat Subform for Each Data Item check box on the Object palette's Binding tab.



Finally, the NameDropList must be setup so that it adds, removes and modifies names from its list whenever instances of NameSubform are added or removed and whenever the values in the FirstName and LastName fields change. This can be done using the drop down list's Calculate event. This event is triggered everytime the form's data changes. As such, you can write script in this event to get the total number of instances of NameSubform and then loop through each one in order to get its FirstName and LastName field values, adding them to the list as you go.



I've attached a sample form which implements your example. Please let me know if you have any questions.



Stefan

Adobe Systems

Avatar

Former Community Member
Hello Stefan!

Thanks for your good example. It works really good. But when i opened it in the Designer and saves it under a new Name i get some warnings is that normal?



------------------------------------------

Here a copy from the log:

------------------------------------------

Ungültiger Node-Typ: macroCache

Ungültiger Node-Typ: macroCache

PDF-Dokument generieren...

Skript fehlgeschlagen (Sprache ist javascript; Kontext ist xfa[0].form[0].form1[0].Page1Subform[0].TotalSubform[0].NameDropList[0])

Skript=var sCurValue = this.rawValue; // save current value before we clear all the items

var bResetValue = false; // true if sCurValue should remain the list's value

this.clearItems();



for (var i = 0; i < Page1Subform._NameSubform.count; i++)

{

var oNameSFInstance = Page1Subform.resolveNode("NameSubform[" + i + "]");



if (oNameSFInstance.FirstName.rawValue != null && oNameSFInstance.LastName.rawValue != null)

{

if (!bResetValue && (sCurValue == oNameSFInstance.LastName.rawValue))

bResetValue = true;



// fields have been filled

this.addItem(oNameSFInstance.FirstName.rawValue, oNameSFInstance.LastName.rawValue); // first name is the text, last name is the value

}

}



if (bResetValue)

this.rawValue = sCurValue;

Fehler: Ungültiger Abruf-Vorgang für Eigenschaft; instanceManager hat keine Eigenschaft 'count'

PDF erfolgreich generiert.

Ungültiger Node-Typ: macroCache

PDF-Dokument generieren...

Skript fehlgeschlagen (Sprache ist javascript; Kontext ist xfa[0].form[0].form1[0].Page1Subform[0].TotalSubform[0].NameDropList[0])

Skript=var sCurValue = this.rawValue; // save current value before we clear all the items

var bResetValue = false; // true if sCurValue should remain the list's value

this.clearItems();



for (var i = 0; i < Page1Subform._NameSubform.count; i++)

{

var oNameSFInstance = Page1Subform.resolveNode("NameSubform[" + i + "]");



if (oNameSFInstance.FirstName.rawValue != null && oNameSFInstance.LastName.rawValue != null)

{

if (!bResetValue && (sCurValue == oNameSFInstance.LastName.rawValue))

bResetValue = true;



// fields have been filled

this.addItem(oNameSFInstance.FirstName.rawValue, oNameSFInstance.LastName.rawValue); // first name is the text, last name is the value

}

}



if (bResetValue)

this.rawValue = sCurValue;

Fehler: Ungültiger Abruf-Vorgang für Eigenschaft; instanceManager hat keine Eigenschaft 'count'

PDF erfolgreich generiert.

Ungültiger Node-Typ: macroCache

PDF-Dokument generieren...

Skript fehlgeschlagen (Sprache ist javascript; Kontext ist xfa[0].form[0].form1[0].Page1Subform[0].TotalSubform[0].NameDropList[0])

Skript=var sCurValue = this.rawValue; // save current value before we clear all the items

var bResetValue = false; // true if sCurValue should remain the list's value

this.clearItems();



for (var i = 0; i < Page1Subform._NameSubform.count; i++)

{

var oNameSFInstance = Page1Subform.resolveNode("NameSubform[" + i + "]");



if (oNameSFInstance.FirstName.rawValue != null && oNameSFInstance.LastName.rawValue != null)

{

if (!bResetValue && (sCurValue == oNameSFInstance.LastName.rawValue))

bResetValue = true;



// fields have been filled

this.addItem(oNameSFInstance.FirstName.rawValue, oNameSFInstance.LastName.rawValue); // first name is the text, last name is the value

}

}



if (bResetValue)

this.rawValue = sCurValue;

Fehler: Ungültiger Abruf-Vorgang für Eigenschaft; instanceManager hat keine Eigenschaft 'count'

PDF erfolgreich generiert.



7 Warnungen/Fehler gemeldet.

Avatar

Former Community Member
Unfortunately, the warnings are in a language that I don't understand and so I can't tell if they're normal or not...



Does the form still work after you've saved it with a different name? Also, make sure you're saving the form as an "Acrobat 7.0.5 Dynamic PDF" form, assuming you're using Designer 7.1 (which I used to create the form). If you saved it as a static PDF file, you may get some warnings because of certain properties being used on the subforms which require a dynamic PDF file format to be used.



Stefan

Adobe Systems

Avatar

Former Community Member
No, it works when i save it. it was only a simple question, because in the log-window comes these warnings. i think i can ignore these warnings.

Avatar

Former Community Member
Hello Stefan,



Your example is perfect. Unfortunatley, I am not able to replicate what you have done. In my scenario, whatever value I entered in row 2, will be automatically be replicated to row3. Its like they are the same instance but appearing twice. Can you kindly help please ?



from

Kwok Wei