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

Calling for multiple objects in an Array

Participant ,
Jun 17, 2016 Jun 17, 2016

Hello,

I've been trying to figure out how to call for multiple objects from an array.

What I want to do is set the display value of certain objects in an array at certain points.

So for example;

var CheckBox1 = this.getField("CheckBox1").value;

var CheckBox2 = this.getField("CheckBox2").value;

var arrObj = [

"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"];

if (CheckBox1 = "Yes") {

     if (CheckBox2 = "Yes") {

          arrObj[1].display = display.hidden;

          arrObj[7, 8, 9].display = display.visible;

          arrObj[4, 5, 6].display = display.noView;

          arrObj[2, 3].display = display.noPrint;

     }

     else if ...

          //do something else

}

so instead of writing out 10 different statements I can condense it down to maximum of 4 or less depending.

However every way I have tried to do this has failed and I can't find any clear examples on the web.

Any suggestions?

As always any help is much appreciated.

Thank you in advance.

TOPICS
Acrobat SDK and JavaScript , Windows
765
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 , Jun 17, 2016 Jun 17, 2016

Here is a potential problem with your code: In a JavaScript array, the first element is element 0 and not 1.

Try this instead (this is just the inner loop):

var a1 = [7, 8, 9];

var a2 = [4, 5, 6];

var a4 = [2, 3];

if (CheckBox2 == "Yes") { 

    arrObj[1].display = display.hidden; 

    for (var i in a1)

        arrObj[a1].display = display.visible; 

    for (var i in a2)

        arrObj[a2].display = display.noView; 

    for (var i in a3)

        arrObj[a3].display = display.noPrint; 

}

Translate
LEGEND ,
Jun 17, 2016 Jun 17, 2016

I would look at using functions that take an array parameter and perform some action.

You still will have overall more code than you want, but for individual fields your coding will be much simpler since you call a function to do something like hide an array of fields and you only pass the field names to the function and the function processes each named field.

Where are you placing your code?

Have you tried to run your code?

If so, what is the error in the JavaScript console?

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 17, 2016 Jun 17, 2016

Here is a potential problem with your code: In a JavaScript array, the first element is element 0 and not 1.

Try this instead (this is just the inner loop):

var a1 = [7, 8, 9];

var a2 = [4, 5, 6];

var a4 = [2, 3];

if (CheckBox2 == "Yes") { 

    arrObj[1].display = display.hidden; 

    for (var i in a1)

        arrObj[a1].display = display.visible; 

    for (var i in a2)

        arrObj[a2].display = display.noView; 

    for (var i in a3)

        arrObj[a3].display = display.noPrint; 

}

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 17, 2016 Jun 17, 2016

Line #5 should be:

if (CheckBox2 == "Yes") {

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 17, 2016 Jun 17, 2016

Thanks for catching that copy and paste error - I was focused on how to loop over the elements in the array and assumed the rest of the script was working.

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 ,
Jun 17, 2016 Jun 17, 2016
LATEST

Another approach.

Using these 2 document level functions:

function SetDisplay(aFieldNames, cDisplayValue)
{
/*
purpose to set the passed display value for the array of fields;
inputs: aFieldNames = array of field names to process;
   cDisplayValue = display value to apply to each field;
returns: nothing;
*/
var oField;
  switch(cDisplayValue)
  {
   default:
   app.alert("Invalid field display option: \"" + dDisplayValue + "\"", 1, 0);
   break;
   case "hidden" :
   case "noprint" :
   case "noview" :
   case "visible" :
   var oField; // field object to process;
   for(var i = 0; i < aFieldNames.length; i++)
   {
    oField = this.getField(aFieldNames); // get field object;
    eval("oField.display = display." + cDisplayValue); // set display property;
   } // end field processing;
  } // end switch valid property;
  return;
} // end SetDisplay function;

function EvaluateOptions(Check1, Check2)
{
// set default display property state;
SetDisplay(["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"], "visible") // set default dispaly state;
switch(true)
{
  case (Check1 == "Off" && Check2 == "Off") :
  SetDisplay(["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"], "visible") // display all fields;
  break;
  case (Check1 == "Yes" && Check2 == "Off") :
  SetDisplay(["One", "Three", "Five", "Seven"], "hidden") // hide odd fields;

  break;
  case (Check1 == "Off" && Check2 == "Yes") :
  SetDisplay(["Two", "Four", "Six", "Eight"], "hidden") // hide even fields;
  break;
  case (Check1 = "Yes" && Check2 == "Yes") :
  SetDisplay(["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight"], "hidden") // hide all fields;
  break;
}
return;
} // end EvaluateOptions;

And then for the mouse up actions check box 1:

// mouse up action for check box1;
EvaluateOptions(event.target.value, this.getField("Check Box2").value);

And the mouse up action for check box 2:

// mouse up action for check box2;
EvaluateOptions(this.getField("Check Box1").value, event.target.value);

More SetDisplay calls with appropriate field name array and display property can be added to set different display properties for other fields.

This code will execute as each check box is changed.

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