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

Populating Prices from Dropdown Box

New Here ,
Nov 30, 2017 Nov 30, 2017

Hi,

I have been trying to write a script that automatically populates a price in another field once an item in a dropdown box is selected. From looking at the tutorials, I have a good portion completed but I am running into the issue where every "cost" field is populated and I am not sure how to create a 1:1 selection with each row.

Example: desc1 populates for cost1

                desc2 populates for cost2

Code:

var cateringData = { "Large Cheese Board":{ cost: "165"},

                                 "Regular Cheese Board":{cost: "98"}};

function setFieldValues(cCateringName)

{

  this.getField("cost1").value = cateringData [cCateringName].cost;

  this.getField("cost2").value = cateringData [cCateringName].cost;

  this.getField("cost3").value = cateringData [cCateringName].cost;

  this.getField("cost4").value = cateringData [cCateringName].cost;

  this.getField("cost5").value = cateringData [cCateringName].cost;

  this.getField("cost6").value = cateringData [cCateringName].cost;

  this.getField("cost7").value = cateringData [cCateringName].cost;

  this.getField("cost8").value = cateringData [cCateringName].cost;

  this.getField("cost9").value = cateringData [cCateringName].cost;

  this.getField("cost10").value = cateringData [cCateringName].cost; }

Then this code is in the dropdown box field (desc1),

if( event.willCommit ) {

    if(event.value == "") this.resetForm(["cost1"]); else setFieldValues(event.value); }

Screen Shot 2017-11-30 at 5.03.33 PM.png

Any help is much appreciated!

Thanks!

TOPICS
Acrobat SDK and JavaScript
937
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

correct answers 1 Correct answer

Community Expert , Nov 30, 2017 Nov 30, 2017

So you have one dropdown for each cost field? Then all you need is a simple way to specify the associated cost field, when a particular dropdown is selected.

Modify your script so the name of the associated field is passed into the function that modifies the field value

function setFieldValues(cCateringName, cCostFieldName)

{

  this.getField(cCostFieldName).value = cateringData [cCateringName].cost;

}

The dropdown code for the desc1 field includes the name of the field it will affect:

if( event.willCom

...
Translate
Community Expert ,
Nov 30, 2017 Nov 30, 2017

So you have one dropdown for each cost field? Then all you need is a simple way to specify the associated cost field, when a particular dropdown is selected.

Modify your script so the name of the associated field is passed into the function that modifies the field value

function setFieldValues(cCateringName, cCostFieldName)

{

  this.getField(cCostFieldName).value = cateringData [cCateringName].cost;

}

The dropdown code for the desc1 field includes the name of the field it will affect:

if( event.willCommit ) {

    if(event.value == "") this.resetForm(["cost1"]); else setFieldValues(event.value,"cost1"); }

I personally prefer self-aligning systems, where the target fields are derived from the name of the current field. For this you'd add code to the Dropdown keystroke code:

var cCostName = event.targetName.replace(/desc/,"cost");

This code automatically generates the name of the associated field.

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Dec 01, 2017 Dec 01, 2017

Awesome! Thank you so much for helping me.

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 ,
Dec 11, 2017 Dec 11, 2017

Hi,

I ran into an issue where if I try to add custom text to the dropdown fields, I get this error.

setFieldValues, script Document-Level:setField Values

Exception in line 2 of function top_level, script AcroForm:desc2:Keystroke

TypeError: cateringData[cCateringName] is undefined

74:Document-Level:setField Values

I’ve been trying to write a regular expression in the cateringData object as a “catchall” but I can’t get it working. Is their an easier way to accomplish this?

Thanks Again

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 ,
Dec 11, 2017 Dec 11, 2017

The error is caused by an undefined value. Have you checked this? Do you know why it is undefined?

What do you mean by a regular expression as a catchall? How is this RegEx used?

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Dec 12, 2017 Dec 12, 2017

Hi,

It is undefined because it is not one of the items in cateringData. I am trying to set it up, so if the item is not in the list we can still add it manually.

I was trying to use the RegEx like this,

var cateringData = { "Add Gluten Free":{ cost: "2"},

                     "Bacon,Egg,& Cheese Biscuit":{ cost: "8"},

                     "Banana Bread Loaf, 8 slices":{ cost: "28"},

                     "/*/": { cost: "/*/"}};

Thanks

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 ,
Dec 12, 2017 Dec 12, 2017

You can't use regular expressions with this kind of literal object. You

would need a different approach to be able to do that.

On 12 December 2017 at 22:26, campbellc36855280 <forums_noreply@adobe.com>

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 ,
Dec 12, 2017 Dec 12, 2017

So as try states, you can't use the regEx in this way.  But you can catch the error and respond to it.

To do this you'll need to break up the code that uses this list into two parts: 1) acquire object from catering data, 2) check for a valid value before using it.

So here is a redo:

function setFieldValues(cCateringName, cCostFieldName)

{

  var oItemData = cateringData [cCateringName];

  if(oItemData)

      this.getField(cCostFieldName).value = oItemData.cost;

  else

  { ... do something else ... }

}

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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 ,
Dec 16, 2017 Dec 16, 2017
LATEST

Thanks Thom!

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