Alphabetic order of drop down list

I have a few drop downs boxes with the Custom coding like this:

var f = event.target;
if (event.commitKey === 2) {
var insertAt = f.numItems;
try {
f.insertItemAt(event.value, event.value, insertAt);
}
catch (e) {
// do nothing
}
this.customProp = 1;
}

Everything works fine, I'm able to build a list of entries but I'm curious to know if it possible to have the new entries typed in go into an alphabetic order? As it stands at the moment each entry typed in just goes to the bottom of the list. I'd really like it if I could have the list sorted without going into the editing mode and selecting the "Sort" of the drop down box after each entry.

Is it possible to have the list automatically alphabetized with each entry? Possibly with a javascript? Can someone help me accomplish this.


Joel Browne


2 Answers

Voted Best Answer

Enter this function as a document level script:

function ListEntrySort(cFldName)
{
var oFld = this.getField(cFldName);
var nSelIdx = oFld.currentValueIndices;
if(typeof(nSelIdx)!="number")
nSelIdx = nSelIdx[0];
var cSelName;
var aEntries = [];
var oExports = {};
for(var i=0;i<oFld.numItems;i++)
{ 
var cEntry = oFld.getItemAt(i,false);
var cExport = oFld.getItemAt(i,true);
aEntries.push(cEntry);
oExports[cEntry] = cExport;
if (i == nSelIdx)
cSelName = cEntry;
}
aEntries.sort();
for(var i=0;i<aEntries.length;i++)
{  
if (aEntries[i] == cSelName)
nSelIdx = i;
aEntries[i] = [aEntries[i],oExports[aEntries[i]]];
}
oFld.setItems(aEntries);
oFld.currentValueIndices = nSelIdx;
}

Enter this script as a mouseDOWN action into any combo box field that you want to sort:

ListEntrySort(event.target.name)


By David Dagley   

The "sort" checkbox in the Field properties dialog is not opened to JavaScript. This means that the "easy" way does not work.

I think (but haven't verified) that the checked "sort" box works when you add an item programmatically.

Otherwise, you could read out the current items, put them in an array, add the new item(s) at the end of the array, and sort it. Then you can feed it back to the combo box with setItems().

Hope this can help.

Max Wyss.


Max Wyss   


Please specify a reason: