0

Edit: This is for use in PDF scripting. I use Acrobat pro but have been using Foxit as of late because it is much quicker than Acrobat.

Below is the exact script I have to add items to a dropdown list titled TalentList. The following is the script in a button titled "Add new Talent"

aray=[this.getField("TalentName").value];{aray.push(this.getField("TalentCost").value)} 
var x = aray[0] + "," + aray[1];
this.getField("TalentList").insertItemAt(this.getField("TalentNa
me").value,x) 
{this.getField("TalentName").value="";}
{this.getField("TalentCost").value="";}
{this.getField("TalentName1").value=" ";}

Where I have the fields TalentName and TalentCost and I input values into those then click the button to add them to the dropdown TalentList. The script works fine as is. But, the problem I am having is every time I add a new item it adds it to the top of the list and does not sort it when I want it to be sorted. Is there something I can add to the above script in the button to cause this to happen automatically every time?

Please note that I have the "Sort Items" box checked in the properties of the TalentList dropdown.

I believe I would use insertItemAt (as told by someone else) but I don't know the method or the correct parameter to use.

ucMedia
  • 4,105
  • 4
  • 38
  • 46
Bruce
  • 1
  • 3
  • 2
    The formatting of your example code is pretty rough. There's also no markup so we have no way of really knowing what "the Sort Items box" or the "TalentList dropdown" are. Toss up a JSbin or Plunker? – bvaughn Mar 31 '15 at 03:55
  • possible duplicate of [Insert Item into Array at a Specific Index](http://stackoverflow.com/questions/586182/insert-item-into-array-at-a-specific-index) – PM 77-1 Mar 31 '15 at 03:56
  • Or just a misunderstanding of how `Array.prototype.sortOn` and `Array.prototype.push` work. – bvaughn Mar 31 '15 at 04:00
  • This is to do with [javascript scripting for PDF forms](http://www.adobe.com/devnet/acrobat/javascript.html), not normal JS. I believe the answer would be to do a binary search for the correct index to insert at, but I don't have the time to do this right now. See the [API reference PDF](http://wwwimages.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/js_api_reference.pdf) (7.5MB) for the methods available. – GregL Mar 31 '15 at 04:19
  • I was told by another expert that the answer is in using the insertItemAt method. An example from the API reference is: --- var l = this.getField("myList"); l.insertItemAt("sam", "s", 0); /* inserts sam to top of list l */ --- But he would not give me the correct method to sort the list in order. He wants me to go to another site that charges for help on the answer. I currently can't afford that. – Bruce Apr 02 '15 at 03:10

1 Answers1

0

I think you are looking to sort the listbox on your PDF form? If so you could do it like this:

aray=[this.getField("TalentName").value]; 
aray.push(this.getField("TalentCost").value);
var x = aray[0] + "," + aray[1]; 

this.getField("TalentList").insertItemAt(this.getField("TalentName").value,x);  
this.getField("TalentName").value=""; 
this.getField("TalentCost").value=""; 


count = this.getField("TalentList").numItems;

var l = this.getField("TalentList")

for (numsorted = 0; numsorted < count; numsorted ++){
    max = l.getItemAt(numsorted, false)
    maxindex = numsorted;
    for (i=numsorted; i<count; i++){
        item = l.getItemAt(i, false);
        if (item > max) {
            max = item;
            maxindex = i;
        }
    }
    item = l.getItemAt(maxindex, false);
    l.deleteItemAt(maxindex);
    l.insertItemAt(item, numsorted);
}
Chris H
  • 932
  • 4
  • 8
  • aray=[this.getField("TalentName").value];{aray.push(this.getField("TalentCost").value)} var x = aray[0] + "," + aray[1]; this.getField("TalentList").insertItemAt(this.getField("TalentName").value,x) this.resetForm(["TalentName", "TalentCost", "TalentName1"]); – Bruce Apr 27 '18 at 03:08
  • The issue I ran into when adding your script is that the box "TalentList" outputs to two other fields when selected. These two fields are the result of aray[0] and aray[1]. When I use the script you posted it completely clears out everything in aray[0] and aray[1]. Where aray[0] used to output the "TalentName" and aray[1] would output the "TalentCost" it now only puts out a seemingly random number. My revised script works fine except for still being unable to sort. – Bruce Apr 27 '18 at 03:08