Highlighted

Override Values in Calculating Field

Explorer ,
Oct 18, 2020

Copy link to clipboard

Copied

I have fields that are filled automatically from a combobox. I would like the user to be able to override these fields. Is this possibe without adding another field to handle the users input? As it stands, when I enter a custom value, it just reverts to the calculated value. 

Try something like this then:

var drop = this.getField("Dropdown1").valueAsString;
if(drop == "One" || drop == "Two"){
event.value = drop;}
else event.rc = false;

 

I don't know what items you have in dropdown so

change "One" "Two" with dropdown items you have and add more if you have .

TOPICS
Create PDFs, PDF forms

Views

84

Likes

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

Override Values in Calculating Field

Explorer ,
Oct 18, 2020

Copy link to clipboard

Copied

I have fields that are filled automatically from a combobox. I would like the user to be able to override these fields. Is this possibe without adding another field to handle the users input? As it stands, when I enter a custom value, it just reverts to the calculated value. 

Try something like this then:

var drop = this.getField("Dropdown1").valueAsString;
if(drop == "One" || drop == "Two"){
event.value = drop;}
else event.rc = false;

 

I don't know what items you have in dropdown so

change "One" "Two" with dropdown items you have and add more if you have .

TOPICS
Create PDFs, PDF forms

Views

85

Likes

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
Oct 18, 2020 0
Contributor ,
Oct 18, 2020

Copy link to clipboard

Copied

How do you get value from dropdown field? If you use code pls post it here.

Likes

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
Reply
Loading...
Oct 18, 2020 0
Engaged ,
Oct 18, 2020

Copy link to clipboard

Copied

Use this as custom calculation script of text field:

if(event.source&&event.source.name=="Dropdown1"){
event.value = this.getField("Dropdown1").value;}

 

Change "Dropdown1" to your "Combobox" name.

Likes

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
Reply
Loading...
Oct 18, 2020 0
Explorer ,
Oct 18, 2020

Copy link to clipboard

Copied

That works, if the combobox entry is overridden, but I need to be able to override the value in a field that gets calculated based on the selection in that combobox. So, I guess, the combobox is irrelevant to the issue...

 

Essentially I need:

 

if (fieldA == manual entry) {

     event.value = manual entry;

}

else { event.value = calculated entry; }

Likes

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
Reply
Loading...
Oct 18, 2020 0
Engaged ,
Oct 18, 2020

Copy link to clipboard

Copied

Try something like this then:

var drop = this.getField("Dropdown1").valueAsString;
if(drop == "One" || drop == "Two"){
event.value = drop;}
else event.rc = false;

 

I don't know what items you have in dropdown so

change "One" "Two" with dropdown items you have and add more if you have .

Likes

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
Reply
Loading...
Oct 18, 2020 0
Explorer ,
Oct 18, 2020

Copy link to clipboard

Copied

So, I have a global array (set up like a table)

 

var gTable = new Array;
gTable[' '] = ' -- -- ';
gTable['Item 1'] = 'Item 1--1H--25--3';
gTable['Item 2'] = 'Item 2--2H--17--4';
gTable['Item 3'] = 'Item 3--1H--9--7';

 

The dropdown is set up with entries 'Item 1', 'Item 2', 'Item 3', Then there is a series of fields, which corospond to the entries on the table. If the dropdown were to be set on Item 1, for instance, the values of those fields would be as follows:

 

field.1.value = 'Item 1'

feild.2.value = '1H'

feild.3.value = 25

feild.4.value = 3

 

Now, what I need is the ability for the user to arbitrarily change the values in fields 1-5.

I'm not sure it's important, but I use .split [0] to parse out the values in the array, so it behaves like a multi-dimentional array...

Likes

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
Reply
Loading...
Oct 18, 2020 0
Adobe Community Professional ,
Oct 18, 2020

Copy link to clipboard

Copied

++Adding to NesaNurani's always valuable guidance,

 

You can run this script as the custom calculation script of the dropdown field object:

 

var gTable = new Array;
gTable[" "] = " -- -- ";
gTable["Item 1"] = "Item 1--1H--25--3";
gTable["Item 2"] = "Item 2--2H--17--4";
gTable["Item 3"] = "Item 3--1H--9--7";

if(event.source&&event.source.name=="myDropdown") {


this.getField("field.1").value = "Item 1";

this.getField("field.2").value = "1H";

this.getField("field.3").value = 25;

this.getField("field.4").value = 3;

}

 

For the .slipt method you need to share your code or be more specific to see how you're using it

Likes

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
Reply
Loading...
Oct 18, 2020 0
Adobe Community Professional ,
Oct 18, 2020

Copy link to clipboard

Copied

Here's a more realistic way of how I am using it on my end:

 

var gTable = new Array;
gTable[" "] = " -- -- ";
gTable["Item 1"] = "Item 1--1H--25--3";
gTable["Item 2"] = "Item 2--2H--17--4";
gTable["Item 3"] = "Item 3--1H--9--7";

if ((event.target.valueAsString !== "Item 1") || (event.target.valueAsString !=="Item 2") || (event.target.valueAsString !=="Item 2")) {

this.getField("field.1").value = "";
this.getField("field.2").value = "";
this.getField("field.3").value = "";
this.getField("field.4").value = "";
}


if(event.source&&event.source.name=="myDropdown") {

if (event.target.valueAsString=="Item 1") {

this.getField("field.1").value = "Item 1";

this.getField("field.2").value = "1H";

this.getField("field.3").value = 25;

this.getField("field.4").value = 3;

} else {

if (event.target.valueAsString=="Item 2") {

this.getField("field.1").value = "Item 2";

this.getField("field.2").value = "2H";

this.getField("field.3").value = 17;

this.getField("field.4").value = 4;

} else {

if (event.target.valueAsString=="Item 3") {

this.getField("field.1").value = "Item 3";

this.getField("field.2").value = "1H";

this.getField("field.3").value = 9;

this.getField("field.4").value = 7;

      }
    }
  }

}

Likes

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
Reply
Loading...
Oct 18, 2020 0
Explorer ,
Oct 18, 2020

Copy link to clipboard

Copied

Here's just the items involved.  https://bit.ly/3jbrvDa

 

I want to make it so field 2, 3 and 4 can be overwritten by the user. 

Likes

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
Reply
Loading...
Oct 18, 2020 0
Adobe Community Professional ,
Oct 18, 2020

Copy link to clipboard

Copied

Please disregard all my replies in this thread.

 

I was not reading the topic carefully and I feel like an idiot now.

 

My script examples are completely wrong.

 

 

NesaNurani already answer this correctly.

 

If you need to allow the users to be able to modify the values in the text fields just add  "event.source&&event.source.name" like this:

 

// LIKE THIS IN FIELD "field.02"

if(event.source&&event.source.name=="select.01"){

var entry = this.getField('field.01').value;     //get entry
var table = gTable;

var calc = table[entry].split('--')[1];   //use entry to retrieve value from global table

event.value = calc;

}


// // LIKE THIS IN FIELD "field.03

"if(event.source&&event.source.name=="select.01"){

var entry = this.getField('field.01').value;     //get entry
var table = gTable;

var calc = table[entry].split('--')[2];   //use entry to retrieve value from global table

event.value = calc;
}




// LIKE THIS IN FIELD "field.04"
if(event.source&&event.source.name=="select.01"){

var entry = this.getField('field.01').value;     //get entry
var table = gTable;

var calc = table[entry].split('--')[3];   //use entry to retrieve value from global table

event.value = calc;

}

 

Even though I am learning JavaScript I think your intent can be accomplish a lot easier. 

 

Right now you have lines of code all over the place, and a global variable is not exclusively necessary to accomplish the same results. However, I understand you may have your reasons for running the PDF form like this.

 

But if it helps, I am able to obtain the exact same results on each field if without using a global variable nor a hidden field to get the export value of the dropdown menu,.

 

To be honest this is all you need:

 

  • You can use the Acrobat's built-in feature(s) like shown in the slide below, and use the export value for each listed item like so

 

array-tables.png

 

 

And in the same dropdown field use a simple custom calulation script like this:

 

var f = event.value;

if(event.source&&event.source.name=="select.01") {

this.getField("field.02").value = f.split("--")[0];
this.getField("field.03").value = f.split("--")[1];
this.getField("field.04").value = f.split("--")[2];

}

 

This just do same results and also allows the users to edit (or override) the values in the textfields.

 

As you can see, using the script above doesn't require to do custom script calculations on each field when you can run everything from the dropdown field.

Likes

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
Reply
Loading...
Oct 18, 2020 0
Explorer ,
Oct 18, 2020

Copy link to clipboard

Copied

That is wehere I started. I abandoned this when it came clear that if I changed anything in a table, I woud have to manually chage each pulldown menu that used that data. That's why I went for the global table instead. This way I would have a central place to edit and add to the list of items and I would only have to update the entry names on the pulldown menus. I hope to learn at some point to create dynamicly populating pulldown menus, so I can maintian the data in one spot, and hopefully never have to touch the pulldown menus again.

 

This works, however there are two remaioning issues with this. The first is, if I make any chages to the pulldown entry, everything resets. The other issue is, if an entry is cleared, it remains blank, instead of reverting back to the calculated value.

 

I know how to get around this by having a sencond field for each one, to handle the user input. I was hoping there was a more elegant solution.

Likes

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
Reply
Loading...
Oct 18, 2020 0
Adobe Community Professional ,
Oct 18, 2020

Copy link to clipboard

Copied

To create dynamically populating dropdowns you may need to use a combination of an array .push method in combination with getItemAt (to get the face value of the selected item ; not the export value), and currentValueIndices (to get the current value selected).

 

This may not be a trivial task, but since you've mentioned about elegance these are the methods and of course some conditional statements will be necessary.

 

This is explained with examples in the Adobe Acrobat SDK JavaScript API Reference,  "Field methods" Page 417.

 

 

Likes

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
Reply
Loading...
Oct 18, 2020 0
Explorer ,
Oct 19, 2020

Copy link to clipboard

Copied

Cool. When I get the time and spare brainpower, I'll look into that. I bought the Acrobat DC classroom in a book, thinking it would have useful information about JS integration. What a waste of money...

Likes

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
Reply
Loading...
Oct 19, 2020 0
Adobe Community Professional ,
Oct 19, 2020

Copy link to clipboard

Copied

Lol!!

 

Hey , I will post back with a script that I am working on using your table.

 

In the meanwhile you can start with this example:

 

var f = this.getField("select.1");
var a = f.currentValueIndices;
var b =  f.getItemAt(a, false);

if(event.source&&event.source.name=="select.1") { 
this.getField("field.2").value = gTable[b].split("--")[1];
this.getField("field.3").value = gTable[b].split("--")[2];
this.getField("field.4").value = gTable[b].split("--")[3];

}

 

 

 

 

Likes

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
Reply
Loading...
Oct 19, 2020 0
Adobe Community Professional ,
Oct 19, 2020

Copy link to clipboard

Copied

Ok, so I think you can give this a try.

 

In the dropdown field use this script as the custom calculation script:

 

 

var f = this.getField("select.01");
var a = f.currentValueIndices;
var b =  f.getItemAt(a, false);
var c =  event.target.value;


if(event.source&&event.source.name=="select.01") {
if ( (c =="Item 1") || (c == "Item 2") || (c == "Item 3") ) {
this.getField("field.02").value = gTable[b].split("--")[1];
this.getField("field.03").value = gTable[b].split("--")[2];
this.getField("field.04").value = gTable[b].split("--")[3];
} else {
if ( (c !=="Item 1") || (c !== "Item 2") || (c !== "Item 3") || (c =="") ) {
this.getField("field.2").value = this.getField("field.02").target.value;
this.getField("field.3").value= this.getField("field.03").target.value;
this.getField("field.4").value = this.getField("field.04").target.value;
    }
  }
}

 

 

The script above use the global variable in the table format that you already created but it allows to change the listed Item to something else in that dropdown without resetting or affecting the values that were already populated in field 2, field 3, and field 4 respectively from a previous selection.

 

This also allows to modify field 2, field 3, and field 4 without affecting the other fields or the dropdown (overriding the values in this calculated fields).

 

Then  use this as custom calculation script in field 2:

 

var f = this.getField("select.01").valueAsString;

if ( ((c =="Item 1") || (c == "Item 2") || (c == "Item 3") || (c == " ")) && (event.target.value =="") ) {event.value = gTable[b].split("--")[1];}

 

 

Use this as custom calculation script in field 3:

 

var f = this.getField("select.01").valueAsString;

if ( ((c =="Item 1") || (c == "Item 2") || (c == "Item 3") || (c == " ")) && (event.target.value =="") ) {event.value = gTable[b].split("--")[2];}

 

 

Use this as custom calculation script in field 4:

 

var f = this.getField("select.01").valueAsString;

if ( ((c =="Item 1") || (c == "Item 2") || (c == "Item 3") || (c == " ")) && (event.target.value =="") ) {event.value = gTable[b].split("--")[3];}

 

 

These last three scripts will guarantee that if the user deletes an entry on either one of those fields and they become null("") or empty, it will reset to the original value taken from the selection of Item 1, Item 2, Item 3 listed in the dropdown. 

 

Let me know if this is what you were looking for.

Likes

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
Reply
Loading...
Oct 19, 2020 0
Explorer ,
Oct 20, 2020

Copy link to clipboard

Copied

That does seem to do the job. Now I have to figure out why, so I can apply it to my actual project, which is much larger and more complex.  Thank you.

Likes

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
Reply
Loading...
Oct 20, 2020 0
Explorer ,
Oct 20, 2020

Copy link to clipboard

Copied

Instead of having to maually check for each possible selection (Item 1, Item 2, Item 3), can't I use something like 'if (event.changeEx in gTable)', to determine if a valid selection has been made?

Likes

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
Reply
Loading...
Oct 20, 2020 0
Adobe Community Professional ,
Oct 21, 2020

Copy link to clipboard

Copied

Give me a little more detailed examples so I can visualize the use event.changeEx that you're trying to employ.

Likes

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
Reply
Loading...
Oct 21, 2020 0
Explorer ,
Oct 21, 2020

Copy link to clipboard

Copied

@ls_rbls Currently the only plaec I've been able to successfully use changeEx is under the combobox Custom Keystroke Script.

 

// Get references to our form fields.
var name = this.getField("field.1")

// Is the new export value a keyword in global table?
if (event.changeEx in gTable)
name.value = gTable[event.changeEx].split('--')[0] // Yes: Split the string up and get the value at position 0

                                                                                  // and assign it to field.1

Likes

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
Reply
Loading...
Oct 21, 2020 0
ls_rbls LATEST
Adobe Community Professional ,
Oct 25, 2020

Copy link to clipboard

Copied

 +++EDITED REPLY, did some corrections in the custom format script

 

Well, I've tried everything I could think of with my current level of JavaScript scripting.

 

I don't think that you're using the changeEx method appropriately. 

 

If I use the following :

 

  • Declare the global variable in a function using a literal Array method instead of "new Array(), "new Array", "Array" methods:

 

 

 

function testTable() {

gTable =   [

[" ", "-- -- --"],

["Item 1", "1H--25--3"],

["Item 2", "2H--17--4"],

["Item 3", "3--1H--9--7"]

] 


}

 

 

 

 

  • And then add a Custom Format script to the combobox to call that function:

 

 

 

 

testTable();

var f = this.getField("select.01");

f.setAction("MouseUp", "f.setItems(gTable);");


 

 

 

 

  •  And a custom Keystroke script in the same combobox:

 

 

 


if (event.willCommit) {

this.getField("field.01").value = event.value;

} else {

this.getField("field.02").value = event.changeEx.split("--")[0];
this.getField("field.03").value = event.changeEx.split("--")[1];
this.getField("field.04").value = event.changeEx.split("--")[2];

} 

 

 

 

 

It leads back to the same problem that you mentioned. You will get errors and undefined values wehn the commited and not commited values are processed., Also, it won't let you revert to the prior value when it is deleted in any of the text fields.

 

To work around this you may have to use the Adobe Acrobat API Reference which explain in full detail the differences of event.value, event.change, and event.changeEx when they're used in custom keystroes and  text boxes.

 

If you noticed in my script above there is no need to split the value that you want in field 1. This line alone does it:

 

this.getField("field.01").value = event.value;

 

That said, if you still prefer to use the code that I posted in my prior reply,  all you need to do is add this line

 

 

 

 

this.getField("field.01").value = gTable[b].split("--")[0];

 

 

 

 

into this script:

 

 

 

 

var f = this.getField("select.01");
var a = f.currentValueIndices;
var b =  f.getItemAt(a, false);
var c =  event.target.value;

if(event.source&&event.source.name=="select.01") {

if ( (c =="Item 1") || (c == "Item 2") || (c == "Item 3") ) {

this.getField("field.01").value = gTable[b].split("--")[0];
this.getField("field.02").value = gTable[b].split("--")[1];
this.getField("field.03").value = gTable[b].split("--")[2];
this.getField("field.04").value = gTable[b].split("--")[3];

} else {

if ( (c !=="Item 1") || (c !== "Item 2") || (c !== "Item 3") || (c =="") ) {

this.getField("field.01").value = this.getField("field.01").target.value;
this.getField("field.02").value = this.getField("field.02").target.value;
this.getField("field.03").value = this.getField("field.03").target.value;
this.getField("field.04").value = this.getField("field.04").target.value;

    }
  }
}
 

 

 

 

 

Likes

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
Reply
Loading...
Oct 25, 2020 0