4 Replies Latest reply on Jul 14, 2016 4:53 AM by Bernd Alheit

    How can I get a list of selections in a select box? (Script on Page 387 of Acrobat JavaScript Scripting Refence not Working)

    adpatter

      I am trying to use the well known code on page 387 of Acrobat JavaScript Scripting Reference:

       

      var f = this.getField("myList");

      var a = f.currentValueIndices;

      if (typeof a == "number") // a single selection

      console.println("Selection: " + f.getItemAt(a, false));

      else {// multiple selections

      console.println("Selection:");

      for (var i = 0; i < a.length; i ++)

      console.println(" " + f.getItemAt(a[i], false));

      }

       

      However, in my code, it is not working (please see: <ACRO_source>Room:Keystroke</ACRO_source> at the bottom of the listing).  Most of the time it just produces the text:

       

      Selection: FIRST_ELEMENT

       

      Sometimes it produces a short list, but never the correct list of the selected elements in the select box.

       

      //<Document-Level>

      //<ACRO_source>Main</ACRO_source>

      //<ACRO_script>

      /*********** belongs to: Document-Level:Main ***********/

       

      var buildingRoom = {};

       

      var oFile = this.getDataObjectContents("export.csv");

       

      var cFile = util.stringFromStream(oFile, "utf-8");

       

      var arr = cFile.replace(/\r\n/g, ",").split(",");

      var building;

      var room;

      for(var i = 0; i < arr.length-1; i++)

      {

         building = arr[i].replace(/\r?\n|\r|"/g,'');

       

         if(!(building in buildingRoom))

              buildingRoom[building]=[];

       

         room = arr[++i].replace(/\r?\n|\r|"/g,'');

         buildingRoom[building].push([room,room]);

      }

       

      var buildingArr = Object.keys(buildingRoom);

      var buildingArrItems = [];

      for(var i = 0; i < buildingArr.length; i++)

          buildingArrItems.push([buildingArr[i],buildingArr[i]]);

       

      this.getField("Room").setItems(buildingRoom[Object.keys(buildingRoom)[0]]);

      this.getField("Building").setItems(buildingArrItems);

       

      var f = this.getField("Room");

       

      //</ACRO_script>

      //</Document-Level>

       

      //<AcroForm>

      //<ACRO_source>Building:Keystroke</ACRO_source>

      //<ACRO_script>

      /*********** belongs to: AcroForm:Building:Keystroke ***********/

      if(event.willCommit)

      {

      //console.println("buildingEvent");

      //console.println(event.value);

      //console.println(buildingRoom[event.value]);

      this.getField("Room").clearItems();

      this.getField("Room").setItems(buildingRoom[event.value]);

      }

      //</ACRO_script>

      //</AcroForm>

       

      //<AcroForm>

      //<ACRO_source>Room:Keystroke</ACRO_source>

      //<ACRO_script>

      /*********** belongs to: AcroForm:Room:Keystroke ***********/

      var a = f.currentValueIndices;

      if (typeof a == "number") // a single selection

      console.println("Selection: " + f.getItemAt(a, false));

      else {// multiple selections

      console.println("Selection:");

      for (var i = 0; i < a.length; i ++)

      console.println(" " + f.getItemAt(a[i], false));

      }

      //</ACRO_script>

      //</AcroForm>