Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to set the export values for dropdown list in Adobe PDF JavaScript?

New Here ,
Jan 06, 2022 Jan 06, 2022

I want to set the Export Values for a dropdown list in an Adobe pdf form using javascript. This is the code I have currently in the document-wide javascript:

var dropDownVal = ["one", "two", "three"];
var dropDownExportVal = [1, 2, 3];

var field = this.getField("Numbers");

field.setItems(dropDownVal);
field.exportValues(dropDownExportVal);

The dropdown list is populated with the dropDownVal array but the properties of the cell show no export value. Any assistance or links to documentation would be great.

TOPICS
How to , JavaScript , PDF forms
5.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Jan 06, 2022 Jan 06, 2022

You need to use a 2D-array as the parameter for setItems, like this:

 

var dropDownVal = [["one", 1], ["two", 2], ["three", 3]];
var field = this.getField("Numbers");
field.setItems(dropDownVal);

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 06, 2022 Jan 06, 2022

exportValues is a property, not a method. You assign to it, rather than call it. See the example in the API Reference. Also, the array should contain strings. Did you get an error in the JavaScript console? I'd expect one.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 06, 2022 Jan 06, 2022

Hey there, thanks for the reply. Here is the updated code based on your suggestions, is this correct? Unfortunately, the export values are still not showing:

var dropDownVal = ["one", "two", "three"];
var dropDownExportVal = ["1", "2", "3"];

var field = this.getField("Numbers");

field.setItems(dropDownVal);
field.exportValues = dropDownExportVal

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 06, 2022 Jan 06, 2022

You need to use a 2D-array as the parameter for setItems, like this:

 

var dropDownVal = [["one", 1], ["two", 2], ["three", 3]];
var field = this.getField("Numbers");
field.setItems(dropDownVal);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 06, 2022 Jan 06, 2022

This worked, thank you so much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 20, 2022 Jul 20, 2022

Is it possible to use two separate arrays when using setItems? For example I have an array of 10 item numberss and another array of 10 values. Is there a way to use field.setItems() with the 2 arrays as the parameters? The first array for the item list and the second for the Export Value?If this is not possible, is there a way to use two separate arrays to create a 2 dimensional array? I have a list of over 100 items I need to add and typing each item and export value is not efficient.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 20, 2022 Jul 20, 2022

No. You have to use a single array with each item in it being another array, with 2 values in it.

Merging two arrays is easy, assuming they are the same length.

 

var arr1 = ["A", "B", "C"];

var arr2 = [1, 2, 3];

var arr3 = [];

for (var i=0; i<arr1.length; i++) arr3.push(arr1[i], arr2[i]);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 20, 2022 Jul 20, 2022

Thank you for this information! It is not working as intended. This is what I am trying to accomplish. I have a part order form that I need to update. The form has multiple combo boxes with the same item lists and values. When we get an order for replacement parts, our employee will select the parts ordered, enter the quantity order and the form will automatically calculate the extended price based on the export value of the combo list item selected on the order line. We need to update this list, I would like to copy and paste the list items and export values and set the field using a javascript button. I found a snippet of code by georgez88320566 here which allows me to paste the list box items in a field, it then separates each line into an array using split and the carriage return character to create the array. I use this to create both arrays, but when I create the two dimensional array using the code you provided, it just combines both items into one. 

TlJI2336743141bg_0-1658351325128.pngexpand image

 

Here is my function code:

 

(function () {

// Get the contents of the multi-line text field

// where the list values have presumably been pasted or otherwise entered

var sItems = getField("PartsList").valueAsString;
var sValues = getField("PartsPrice").valueAsString;

// Alert the user if the field is empty

if (!sItems||!sValues){

app.alert("Please enter a list of items in the text field below.", 3);

return;

}

// Create the array of items by splitting the string from the field

var aItems = sItems.split(/\r/);
var aValues = sValues.split(/\r/);
var aTwoDim = [];
for(var i=0; i<aItems.length;i++)aTwoDim.push(aItems[i],aValues[i]);

//

getField("Part").setItems(aTwoDim);

})();

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 08, 2023 Feb 08, 2023

@try67 , using your syntax, is it also possible to force the setItems command to include leading zeroes on the export values? I know how to do this in other situations using 'valueAsString', but I can't figure out how to make that work when setting the dropdown items dynamically.

var dropDownVal = [["one", 01], ["two", 02], ["three", 03]];
var field = this.getField("Numbers");
field.setItems(dropDownVal);

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2024 Jun 20, 2024

Been trying to use this now, and it keeps reverting to the first dropdown option, and therefore only pushing that out as a value.

Any way to fix that?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 20, 2024 Jun 20, 2024

Did you use script under 'Calculate' tab? It should go under 'Validate'.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2024 Jun 20, 2024

Doing that is only a half solution. It calls the right value, but it causes Acrobat to freeze and hang, and then still changes the dropdown to the default value. So even if the numbers are right, the labeling is wrong.

var dropDownVal = [["No Armor", 0], ["Light", this.getField("ArmorDef").value], ["Medium", this.getField("ArmorDef").value], ["Heavy", this.getField("ArmorDef").value], ["Unarmored Defense", this.getField("StrengthMod").value+this.getField("VitalityMod").value]];
var field = this.getField("ArmorSkill");
field.setItems(dropDownVal);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2024 Jun 20, 2024

Forwhatever reason, putting this in an Action also overrides the options with the options of the same other dropdown, and I don't understand why.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2024 Jun 20, 2024

Is there any way to record the option I choose, then set that as the display value after setItem is done?

I've seen examples of that, but those cause my drop down to adopt the options of a different dropdown, and I don't know why.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 20, 2024 Jun 20, 2024

Try this as custom calculation script of any text field:

if (event.source && (event.source.name=="ArmorDef" || event.source.name=="VitalityMod" || event.source.name=="StrengthMod")) {
var dropDownVal = [["No Armor", 0], ["Light", this.getField("ArmorDef").value], ["Medium", this.getField("ArmorDef").value], ["Heavy", this.getField("ArmorDef").value], ["Unarmored Defense", this.getField("StrengthMod").value+this.getField("VitalityMod").value]];
var field = this.getField("ArmorSkill");
field.setItems(dropDownVal);}

It will allow you to select a choice from the dropdown, and it won't set it back to default as long as you don't change any of the 3 fields (ARM, STR, VIT) afterward.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2024 Jun 20, 2024

The issue is that I have two of these that are adding together somewhere else, and the other attributes will be changing often. So I need to figure out a way to set the dropdowns once, and then have them reflect the other values as they change.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2024 Jun 20, 2024

I tried it in a new field, and it just stayed blank. The fixed option eject values aren't showing at all.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 20, 2024 Jun 20, 2024

If you want to use another field to show value, first set all choices to dropdown field (No export value), and use this as custom calculation script of the field where you want to show result:

var drop = this.getField("ArmorSkill").valueAsString;
var arm = this.getField("ArmorDef").value;
var str = Number(this.getField("StrengthMod").valueAsString);
var vit = Number(this.getField("VitalityMod").valueAsString);

if(drop == "No Armor")
event.value = 0;
else if(drop == "Light" || drop == "Medium" || drop == "Heavy")
event.value = arm;
else if(drop == "Unarmored Defense")
event.value = str+vit;
else
event.value = "";
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 20, 2024 Jun 20, 2024
LATEST

I retried my original idea, which now seems to work. So I guess I learned something I needed.

I put this in the final result field:

var AS = this.getField("ArmorSkill").value;
var AD = this.getField("ArmorDef").value;
var SS = this.getField("ShieldSkill").value;
var SD = this.getField("ShieldDef").value;
var Agi = this.getField("AgilityMod").value;
var Vit = this.getField("VitalityMod").value;
var Str = this.getField("StrengthMod").value;
var ADC = this.getField("ActiveDefense").value;

switch(AS){
case "No Armor":
ASR = 0;
break;
case "Unarmored Defense":
ASR = Vit + Str;
break;
default:
ASR = AD;
break;
}

switch(SS){
case "No Shield":
SSR = 0;
break;
default:
SSR = AD;
break;
}

if(ADC == "Yes"){
ADR = 0;
}else{
ADR = 10;
}

event.value = ADR + Agi + ASR + SSR;

I wish I could have figured the other thing out, but this works.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines