using event.changeEx

I want to modify your example given at
http://acrobatusers.com/tutorials/js_...
for AcroForms

In this example, each part has one price value.
In my calculation, I want relate to each part three values (e.g. price, weight, and length) and show them in the corresponding fields <Price>, <Weight> and <Lenght> in order to use them for further calculations.

The script works perfectly if there is only the field <Price>. But when I extent the array of each Part by the values for "Weight" and "Length" in such way:

var oAssemblyParts = {
Chasis: [
["-","None", "None", "None"], ["Rear Bracket",205.95, 2, 3.2], ["Front Bearing",48.95, 1, 1.75]
]

};

then something goes wrong: All three values are indicated in the part list behind teh part name, the prize field is empty and the fields for weight and length as well.

I think I have to add two more functions similar to the function SetPriceValue()

function SetPriceValue()
{

 if(!event.willCommit)
 {
   
  var nSelExp = 0;
  if(!isNaN(event.changeEx))
   nSelExp = event.changeEx
   this.getField("Price").value = nSelExp;
 }
}


like this

function SetWeightValue()
{

 if(!event.willCommit)
 {
   
  var nSelExp = 0;
  if(!isNaN(event.changeEx))
   nSelExp = event.changeEx
   this.getField("Weight").value = nSelExp;
 }
}

function SetLengthValue()
{

 if(!event.willCommit)
 {
   
  var nSelExp = 0;
  if(!isNaN(event.changeEx))
   nSelExp = event.changeEx
   this.getField("Length").value = nSelExp;
 }
}


But how can I make sure that these functions address the right value of the array, i.e. SetPriceValue() exports the price value, SetWeightValue() exports the weight value and SetLengthValue() exports the value for the length to the fields <Price>, <Weight>, and <Length>, respectively?


Lutz Wittenmayer


3 Answers

Voted Best Answer

You got the right idea, as far as extending the "oAssemblyParts" object to include the other values, but your approaching adding them into the code in the wrong way.

To simplify the code I used the
oAssemblyParts to built the items/exports in the drop lists, then ued the export values to set the field value, or the next list, as necessary. To extend that same technique you need to group all 3 of your values into the export value. Like this:

var oAssemblyParts = {
Chasis: [
["-","None,None,None"], ["Rear Bracket","205.95,2,3.2"], ["Front Bearing","48.95,1,1.75"]
]

};

Now the trick to using the eports is to break out all threes values on the selection.


function SetFieldValues()

{

if(!event.willCommit)
{

var nSelExp = 0;
if(event.changeEx!= "")
{
/ / Now split the string
aSelExp = event.changeEx.split(",");
this.getField("Price").value = aSelExp[0];
this.getField("Weight").value = aSelExp[1];
this.getField("Length").value = aSelExp[2];
}
}
}

This is not the ideal method, but as long as you only have three values it will work. For larger data sets the correct technique is to create mapping objects to "map" the selected value to the correct data set.


Thom Parker
The source for PDF Scripting Info pdfscripting.com
All About PDF Stamps in Acrobat and Paperless Workflows - THE BOOK !!

The Acrobat JavaScript Reference, Use it Early and Often

The most important JavaScript Development tool in Acrobat
The Console Window (Video tutorial)
The Console Window(article)

Having trouble, Why Doesn't my Script Work


By Thom Parker   

Hi Thom,

Thank you for your immediate support. Finally, I choose four export fields and it works perfectly.

In comparison to your original script there is still a little difference in usability.

In your script, after selection of an assembly, the corresponding part list becomes available and I can select the part I want. And when I select in the part list "-", the prize becomes zero again - perfect!

But when I do this in my script, I get an error message that the export field format (which actually is a number) is wrong. I changed the field format to "no format" and I saw that the word “None” is displayed which belongs to the array when the part value is “-“. In your script the array contains also the the combination “-, None”. I think you avoid this error message by using this code

var nSelExp = 0;
if(!isNaN(event.changeEx))
nSelExp = event.changeEx


I inserted this code in my code this way


function SetFieldValue()
{
if(!event.willCommit)
{
var cRowName = event.target.name.split(".").shift();
var nSelExp = 0;
// if(event.changeEx!= "")
if(!isNaN(event.changeEx))
aSelExp = event.changeEx
{
// Now split the string

aSelExp = event.changeEx.split(",");
this.getField(cRowName + ".exField1").value = aSelExp[0];
this.getField(cRowName + ".ExField2).value = aSelExp[1];
this.getField(cRowName + ".exField3").value = aSelExp[2];
this.getField(cRowName + ".exField4").value = aSelExp[3];
}
}
}

but there is still this error message. (I do not want that None is displayed)

And on last question: I do not understand why you started your script with var nSelExp and for splitting you use var aSelExp. Is there a specific meaning of changing "a" to "n" as the first letter of a varible?



Thank you in advance!


Lutz Wittenmayer   

I just missed converting "nSelExp" to "aSelExp". But the general idea is that the export value is no longer a number, it is a list of values in a string, so the "isNaN()" test does not work.

To check for the "0" value, you can do one of two things.

1. use the "if" statement to detect "none", and then set the values to 0.

2. Or a much better option is to replace "none" with "0", so the fields are naturally populated with 0.


Thom Parker   


Please specify a reason: