Expand my Community achievements bar.

How to add script variable to a document & best practice?

Avatar

Level 2

Hi - I am working though some scripting that populates a dropdown list from entries in a previous field.  I am trying to recreate the example the Thom Parker put together - Programming Lists Part 1, XFA Form Example

1)  I cannot understand how to add the script variable anywhere.

I see the following appear the variable secion on the form -

"TopmostSubform.Page1.OrderSection.OrderItems.#variables[0].Example1 - (JavaScript, client)"

I do not see how to add variables as a function.... When I insert the "var xxxx = {..."  I half expected the above to appear but it did not. What am I doing wrong?

2)  I need this to be repeatable as I add lines.  Is the variable array (if that is the technical term) example from Thom Parker sill the best way to implement a dependant drop down list?  I am also going to hang a "price" off of the added items to do basic calculation. 

2a)  I also have looked at code solutions from Assure Dynamics where they use a preOpen function to make this a happen.  As in the "Populating a dropdown based on selected checkboxes" example on there site - great examples there.  Is this approach better than in Thom Parker's example?  I get the impression the the preOpen approach is not repeatable.  Am I correct in assuming the raw bound value can easily be replaced by a variable?  Anyone have an example of this?

Sorry for the rambling questions and thanks in advance to any help.

Tom

8 Replies

Avatar

Level 10

Hi Tom,

Form variables are added via File > Form Properties > Variables tab. Once you have added the variables there, they will appear in the hierarchy under (Variables).

In script while you access an object's contents using .rawValue, when accessing or setting a variables contents you use .value.

In LC there there tends to be more than one way to get a solution. If Thom's solution is working for you, then go for it. If your users all have Acrobat/Reader version 9.1, then there is a single line to set the dropdown - setItems().

I am not sure what event Thom is using, but the preOpen event is made for this type of task with dropdowns. I am not sure what you mean that the approach is not repeatable. Can you explain.

Niall

Avatar

Level 2

Niall - thank you so much for responding.

"Form variables are added via File > Form Properties > Variables tab. Once you have added the variables there, they will appear in the hierarchy under (Variables).

This is the frustrating part for me - I see the variables there when I do this, but they have a "dot" logo in front of them which I assume means form variables... but the ones in many of the examples have a "form" logo which I assume means a script variable.  Do I need to add a form variable before I can add a script variable? These little logos appear in the hieracy tree - I hope I am being clear.

Again - I am expecting something like -

"TopmostSubform.Page1.OrderSection.OrderItems.#variables[0].Example1 - (JavaScript, client)"

to appear before the form variable - but it never does.  I am thinking it should be a dropdown from the function list, but this must be incorrect.

"I am not sure what event Thom is using, but the preOpen event is made for this type of task with dropdowns. I am not sure what you mean that the approach is not repeatable. Can you explain."

I will have rows that will repeat and fill the same dependant dropdowns.  I tried your example and it worked once, but did not work in the next row.

Thanks again.



Avatar

Level 10

Hi Tom,

Does this help:

Parallels Desktop1.png

The Script object is used to contain a block of script that is then referenced from many objects on the form. This allows you to have one piece of code that can be reused by many objects on the form. This is efficient, as you only have one piece of code to develop and maintain.

A form variable only contains a value. This can be accessed and set by any object on the form (or even by script within a script object).

If I wanted to assess the above form variables from an object on the form, I would just use:

this.rawValue = errMsg.value;

Does that helps,

Niall

Avatar

Level 2

Yes that script object is what I am trying to define.  How do I accomplish this?  If I am starting with a blank from how do I define them?  I do not see a function all for variable?  Meaning change event, exit, etc.... how do I actually put these in the script.  Do they have to be attached to an object or just attached to a sub form?

I cannot get that variable/script to occur in my hierarchy.....  In essence - how/where do I define script variables.  I have seemingly tried everything.

Thanks so much again for your help.  Sorry to be a layman...

Tom

Avatar

Level 10

Hi Tom,

Put script objects out of your mind for the time being.

Form Variable (or Global Variable)

This is defined in the File > Form Properties Variables tab. Variables that are defined there can be accessed from any object on the form, eg errMsg.value;

Script Variables

These are variables that are defined within the script in an event in an object. You declare a variable using "var" and then just sets its value by referening it (without .value or .rawValue).

Note: script variables only exist within the script that declares them, they cannot be used/referenced by other objects.

For example,

// This declares the variable

var myVariable;

myVariable = "10";

this.rawValue = myVariable   // This would set the valus of the object to the variable value, which is 10

You can also set the value of a script variable when you declare it:

var myVariable = "10";

These are two types of variables, which are completely different to script objects.

Does that help?

Niall

Avatar

Level 2

Yes it helps - but I cannot get the variables>Assured Dynamics to show up in the hierarcy....  I have it associated with an object in the subform.

I must be doing something wrong.

Avatar

Level 10

Okay,

"AssureDynamics" is a script object (and not a variable).

Right-click on the root node in the hierarchy and select Insert Script Object. This will insert an unnamed script object, which you can then name in the hierarchy.

You can then select it and place/type your script in the Script Editor.

Niall

Avatar

Level 2

THANKS! - that was the part I was missing.

Now - I still cannot get this form to work...  would like to put the entire PDF up for review is that possible.  I see others have link to files.

Here is the code from the base form...

form1.mainpage.subform.#variables[0].variables - (JavaScript, client)

var vitem = {

Agroup: [ ["Part 1",1], ["Part 2",2], ["Part 3",3], ["Part 4","4"]],

Bgroup: [ ["Part A",11], ["Part B",12], ["Part C",13], ["Part D",14]]

};

function SetPartEntries(){

item.clearItems();

item.rawValue = null;

var newitem = vitem[xfa.event.change];

if(newitem && newitem.lenght){

          for(var i=0;i<newitem.length;i++){

          item.addItem(newitem[i][0],newitem[i][1].toString());

                    }

          }

}

function SetPriceValue(){

var newprice = 0;

newprice = item.boundItem(xfa.event.newText);

if(isNAN(newprice)){

          newprice = 0;

          price.rawValue = newprice;

          }

}

form1.mainpage.subform.category::change - (JavaScript, client)

SetPartEntry();

form1.mainpage.subform.item::change - (JavaScript, client)

SetPriceValue();