Skip to main content
Inspiring
February 8, 2022
Question

Want to map table data from a pulldown menu to various places in a document

  • February 8, 2022
  • 1 reply
  • 11021 views

I’m updating a form used for booking truck rentals. I’ve come up with a plan for the interactive elements. I have a couple holes in my knowledge and would like to ask for for some advice to help me complete it. 

 

Here’s the situation. The client would like to have it where up to 30 trucks can be listed on the form, and each of the 30 trucks could be one of seven different models. The idea is to choose from a pull-down menu for each truck which has “NO TRUCK” as the default choice with the seven models as options. Once a model is chosen, there are eight data sets associated with each model that would be mapped to the truck rental number. That truck rental number’s data results will show up in various places in the forms.

 

The truck rentals are given names T01, T02, T03, etc… so their data sets are given a suffix in their fields like this…

 

T01d - Truck 1 depreciation value

T01v - Truck 1 original value

T01w - Truck 1 weekly fixed charge

… and so on for eight different sets.

 

The truck models have their data sets listed like this….

TD1d - (first model) depreciation value

TD2d - (second model) depreciation value

TD2v - (second model) original value

… and so on for all the models and all the data sets. 

 

I want to be able for the appropriate model TD data sets to get mapped to the T01 to T30 data sets when the user uses the drop-down menu and picks a model.

 

And it seems to me that by using a document level script I could save a lot of hassle in editing. Someone here recommended that I should think that way for a future project and I haven’t forgotten. So I’m anticipating doing that and here’s how I thought it out…

 

My plan is that each dropdown menu would be fields labeled T01t, T02t, etc. Would it be possible to write a document level script that can:

- grab the first three characters of the current rental dropdown menu field name (ex.: T01) as a variable

- grab the first three characters of the chosen truck model field name (ex.: TD3) as a variable

- use those variables to join with a chosen fourth character to know which data sets to find

- map those data sets together and display them in the appropriate fields in the forms

 

My thought is that would allow a single document level script to do all the work of combining rentals with trucks and their data in various places on the form. Rental 1 would pick Truck model 3, and the script would know to grab Truck model 3’s data and map them to Rental 1’s spots on the various forms.

 

I’ve split path parts before and joined, but I’m not finding how to grab character parts here. I seem to see FieldName as something to work with. 

 

Lastly, am I overthinking this and is there a simpler way? I’ve done three projects with javascript in Acrobat so I’m taking what I know from the limited experience I have and attempting something much bigger. But some advice would certainly be welcome. Thank you.

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
February 8, 2022

Here are a couple of articles that describe a technique for maping selections to a data set. Both use a JavaScript object defined in a document script to hold the data, and a document script to define a generic function for mapping the data based on the field name of the drop down. 

 

https://acrobatusers.com/tutorials/change_another_field/

https://acrobatusers.com/tutorials/js_list_combo_livecycle/

 

The second one is for mapping selections dropdowns to a new set of items in another dropdown, but the technique is the same in both. A selection value in the dropdown selects a set of data in the JavaScript Object. 

You can read more about list and dropdown scripting here:

https://www.pdfscripting.com/public/List-Field-Usage-and-Handling.cfm

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
February 10, 2022

I read through all those resources, Thom, and I'm starting to get how this works. Your tutorials gave good examples of why they work; thanks for putting example PDFs in there to try the actual use of them.

 

Here's my question: I see how you can create document objects with the lists of items using arrays and small arrays, like this from your tutorial: 

 

var DeptData = { Accounting:{ contact: "Steala Damuni",
                              email: "accounting@mycomp.com",
                              deptnum: "cmp1234" },
                 Engineering:{ contact: "Frank N. Stien",
                              email: "engineering@mycomp.com",
                              deptnum: "eng1234" },
... and so on...

 

 

Your examples show that this is done in the document level script. My client is interested in being able to edit those values occasionally (prices and such which can fluctuate). I'm creating a table for "Truck Data" on a page where each model of truck's specifications are there. The field id's are like this: TD1w, TD1v, TD1x... example: TD1v is the truck value (Truck Data 1 value). I assume I would be able to populate the data in the small array to be callouts to the data in the table on that entry page like this...

 

var TruckData = { TruckModel1:{ charge: (this.getField("TD1w").value),
                              value: (this.getField("TD1v").value),
                              weight: (this.getField("TD1x").value) },
                 TruckModel2:{ charge: (this.getField("TD2w").value),
                              value: (this.getField("TD2v").value),
                              weight: (this.getField("TD2x").value) },
... and so on...

 

 

I'd just like to know if this looks right to you, as that would allow me to set up all the groups for all seven truck rental types, but leave them open to my client to edit them on the front end.

Thom Parker
Community Expert
Community Expert
February 24, 2022

I went and added the SetupTruckData function to the Document Scripts....

function SetupTruckData()
{
var  oIDFld, strNameBase, oTruckData = { };
for(var i=1;i<8;i++)
{
    strNameBase = "TD" + i ;
    oIDFld = this.getField(strNameBase  + "o");
    if(oIDFld.valueAsString != "")
        oTruckData[oIDFld.valueAsString] = 
                     { year: (this.getField(strNameBase +"y").value), 
                       options: (this.getField(strNameBase +"o").value), 
                       charge: (this.getField(strNameBase +"w").value), 
                       value: (this.getField(strNameBase +"v").value), 
                       depreciation: (this.getField(strNameBase +"d").value), 
                       weight: (this.getField(strNameBase +"x").value), 
                       cab: (this.getField(strNameBase +"c").value), 
                       fees: (this.getField(strNameBase +"f").value),
                       asset: (this.getField(strNameBase +"a").value),
                       residual: (this.getField(strNameBase +"r").value) }; 
}
}
SetupTruckData(this);

 

... and I added the SetupTruckData(this); function to the button under the Data Table.

 

Next is to add the script to the first combo box. I read your tutorial examples on List Field Usage and Handling, and redid my script for the SetFieldValues so that I'm using commitOnSelChange and I assume this needed to be under the Format > Custom Keystroke Script section in the Field Properties for field "T.01". Since each truck rental need to populate specific fields, I'll have to edit this script in each combo box to reflect that rental number.

 

if( event.commitOnSelChange )
{
     SetFieldValues(event.value);
}

// set the field values by grabbing data from SetupTruckData function
function SetFieldValues(cTruckModel)
{
  this.getField("T01y").value = oTruckData[cTruckModel].year;
  this.getField("T01o").value = oTruckData[cTruckModel].options;
  this.getField("T01w").value = oTruckData[cTruckModel].charge;
  this.getField("T01v").value = oTruckData[cTruckModel].value;
  this.getField("T01d").value = oTruckData[cTruckModel].depreciation;
  this.getField("T01x").value = oTruckData[cTruckModel].weight;
  this.getField("T01c").value = oTruckData[cTruckModel].cab;
  this.getField("T01f").value = oTruckData[cTruckModel].fees;
  this.getField("T01a").value = oTruckData[cTruckModel].asset;
  this.getField("T01r").value = oTruckData[cTruckModel].residual;
}

  

This didn't populate any of of the "T01" group fields when implemented and choosing a truck model. I feel I'm missing something in the implementation. I must be real close, though, I think.


Ok, so onto the next section of the form. And the next lesson. All variables have a characteristic called Scope. A variable's scope determines where it can be seen. A variable with function scope, can only be seen inside the function where it is declared. Declaring a variable in a function, means that variable only exists in that function.  A variable with document scope can be seen and used by any script in the document.  Variables are given document scope by declaring them in a document script, or attaching them to the document object. 

 

The "oTruckData" vaiable needs to be at the document level so that it can be setup and seen by all scripts in the document. Right now, the function that sets it up, gives it function scope. 

 

 

 

 

 

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