5 Replies Latest reply on Jun 17, 2016 12:31 PM by gkaiseril

    Calling for multiple objects in an Array

    alexh90310518 Level 1

      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.

        • 1. Re: Calling for multiple objects in an Array
          gkaiseril MVP & Adobe Community Professional

          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?

          • 2. Re: Calling for multiple objects in an Array
            Karl Heinz Kremer Adobe Community Professional

            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[i]].display = display.visible;  
                for (var i in a2)
                    arrObj[a2[i]].display = display.noView;  
                for (var i in a3)
                    arrObj[a3[i]].display = display.noPrint;  
            }
            
            1 person found this helpful
            • 3. Re: Calling for multiple objects in an Array
              try67 MVP & Adobe Community Professional

              Line #5 should be:

               

              if (CheckBox2 == "Yes") {

              1 person found this helpful
              • 4. Re: Calling for multiple objects in an Array
                Karl Heinz Kremer Adobe Community Professional

                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.

                • 5. Re: Calling for multiple objects in an Array
                  gkaiseril MVP & Adobe Community Professional

                  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[i]); // 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.

                  1 person found this helpful