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

Show/Hide One Field Based on Multiple Fields

Community Beginner ,
May 03, 2018 May 03, 2018

Copy link to clipboard

Copied

Hey everyone,

I have a form with a text field "Total". I'd like "Total" to be revealed if any one of ten other text fields are filled in. So if all ten fields are blank, "Total" is hidden. If any one of "Price_PP_1", "Price_PP_2", "Price_PP_3", "Price_PP_4", "Price_PP_5", "Price_PP_6", "Price_PP_7", "Price_PP_8", "Price_PP_9", or "Price_PP_10" are filled in with any info at all, "Total" shows itself.

It seems like it should be simple. I just don't know how to write the custom calculation script that basically tells it, "If this or this or this or this or this happens, show it. If none of it happens, don't show it."

Can anyone help?

TOPICS
Acrobat SDK and JavaScript

Views

1.2K

Translate

Translate

Report

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 ,
May 04, 2018 May 04, 2018

Copy link to clipboard

Copied

Hi,

You should be able to use something like the following to be able to show hide the field.

var totalValue = null

var curDoc = app.doc;

// do this for each field ( could be in a loop if you want)

var myField = curDoc.getField ("Price_PP_1").value;

if ( myField !== "")

{

     // have added parseInt here just in case you don't have any formatting on the field to stop non numbers being used

     totalValue += parseInt ( myField, 10);

}

var myTotal = curDoc.getField ("Total");

// if totalValue has changed from the above code, then we can show the field

if ( totalValue !== null)

{

     myTotal.hidden = false;

}

else

{

     myTotal.hidden = true;

}

myTotal.value = totalValue;

Hope this helps

Malcolm

Votes

Translate

Translate

Report

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 Beginner ,
May 04, 2018 May 04, 2018

Copy link to clipboard

Copied

Thank you for your help, but it didn't work. Or at least I didn't work it correctly. I planned on doing this to each Price_PP_ field. I go to custom calculation JavaScript Editor and enter...

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

var totalValue = null

var curDoc = app.doc;

var myField = curDoc.getField ("Price_PP_1").value;

if ( myField !== "")

{

     totalValue += parseInt ( myField, 10);

}

var myTotal = curDoc.getField ("Total");

if ( totalValue !== null)

{

     myTotal.hidden = false;

}

else

{

     myTotal.hidden = true;

}

myTotal.value = totalValue;

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

... I don't get any error in the JavaScript Editor, but I do get an error message in Acrobat. As soon as I go back to my form Acrobat pops up with, "The value entered does not match the format of the field [ Total ]". As I click around on the form in any field at all I get this error message.

What step did I miss along the way here?

Votes

Translate

Translate

Report

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 ,
May 04, 2018 May 04, 2018

Copy link to clipboard

Copied

Hi,

The script only needs to be added to the calculate of the Total field, as all calculate scripts are called when a field value is changed. The total field also needs to be called "Total" or you need to change the following line.

var myTotal = curDoc.getField("Total"); // replace Total with the name of your field.

Regards

Malcolm

Votes

Translate

Translate

Report

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 Beginner ,
May 04, 2018 May 04, 2018

Copy link to clipboard

Copied

Ahhhh, that may be a problem. I already have a calculation in the Total field. It is the basic sum of a few other fields on the form.

Votes

Translate

Translate

Report

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 ,
May 07, 2018 May 07, 2018

Copy link to clipboard

Copied

Hi,

Then the best plan would be to port the calculation in the total field to be included in the script above and placed on the Total field. If you post the calculation then we can probably help with this.

In general you want to keep the number of calculation scripts any file has small as they are called every time any field changes and this means that the more scripts you have the longer it takes, and the more possibility that you are calculating a given value more than once.

if you have 2 fields, field1 and field2, and a total field, if you put the calculation on both of the fields and not the total, then every time a value changes you will calculate the total field twice. The more fields the more times you run the exact same calculation.

Regards

Malcolm

Votes

Translate

Translate

Report

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 Beginner ,
May 07, 2018 May 07, 2018

Copy link to clipboard

Copied

Well the Value Is calculation in Acrobat doesn't generate a JavaScript that I can edit. In this case, I just tell it I want the Sum, them I check all the boxes I want it to total up. If I go to use the custom calculation option it turns off that generic calculation and I'd have to build it from scratch.

So I have an order form with ten line items. Each line item has a Quantity field, a Price Per Part field, a Rush Fee field, and a Sub Total field. (So I have ten of each of these kinds of fields). I have the ten Price Per Part fields formatted as monetary number values. I have the ten Rush Fee fields formatted as percentages. I have the ten Sub Total fields formatted as monetary number values. And I have the total formatted as a monetary number value. The Total field runs this generic Value Is Sum script to total up everything in the ten Sub Total fields.

I need these fields to show and hide only when someone fills in one of the previous fields. These things need to be hidden so that someone can print out this form as a blank document. If I don't show/hide them, then someone would only be able to print out a form with a bunch of zeros in all these fields.

If your script will get the Total field to hide unless someone fills in any one of the Price_PP_ fields, that'd be great. But I also need the Total field to keep the calculation it has for adding up all the Sub Total fields.

Once I get the total field to still have a Sum function, and will hide unless someone fills in any one of the Price_PP_ fields, I think that'll be all I need to have this thing do.

Votes

Translate

Translate

Report

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
Engaged ,
May 08, 2018 May 08, 2018

Copy link to clipboard

Copied

This will sum all 10 fields and display the total only if higher than 0.  Put it inside custom calculate script of the "Total" field.

//Create an array containing all fields to add

var prefix = "Price_PP_"

var aFields = []

for (x=1;x<=10;x++) aFields.push(prefix+x)

//loop through all fields and find sum

var total = 0

for (i in aFields) total += Number(this.getField(aFields).value)

//Display sum inly if higher than 0

event.value = total

if (total > 0) event.display = display.visible

else event.display = display.hidden

Votes

Translate

Translate

Report

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 Beginner ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

This is exactly the script I put into the Total field...

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

var prefix = "Price_PP_"

var aFields = []

for (x=1;x<=10;x++) aFields.push(prefix+x)

var total = 0

for (i in aFields) total += Number(this.getField(aFields).value)

event.value = total

if (total > 0) event.display = display.visible

else event.display = display.hidden

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

... It works for adding up the total, but the field remains visible at all times. Does it matter to the script that the Price_PP_ and Total fields are formatted to be monetary number values?

Votes

Translate

Translate

Report

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
Engaged ,
May 09, 2018 May 09, 2018

Copy link to clipboard

Copied

LATEST

change this:

if (total > 0) event.display = display.visible

else event.display = display.hidden

to this:

if (total > 0) event.target.display = display.visible

else event.target.display = display.hidden

Votes

Translate

Translate

Report

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