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

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

Guest
Jul 13, 2016 Jul 13, 2016

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, 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.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,buildingArr]);

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, false));

}

//</ACRO_script>

//</AcroForm>

TOPICS
Acrobat SDK and JavaScript
592
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 ,
Jul 14, 2016 Jul 14, 2016

I don't see where you defined the value of the "f" parameter...

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 ,
Jul 14, 2016 Jul 14, 2016

It is defined at document level.

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
Guest
Jul 14, 2016 Jul 14, 2016

I placed a button on the page and configured it to execute the code on mouse up and it works as expected.  It appears that putting the code in the keystroke event itself isn't how it was supposed to be used.  I should have figured this out yesterday evening, as I was observing the phenomenon where I see different results under seemingly identical circumstances, which indicates it is a timing issue.

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 ,
Jul 14, 2016 Jul 14, 2016
LATEST

During the keystroke event you get the old selection with your code, not the actual.

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