Copy link to clipboard
Copied
Hello,
I can't seem to figure out a simple/direct method to identify the index of the selected item in a list box.
I feel like I'm missing something, but I can't seem to figure which (if any) property returns an index number if you use it with the .onChange callback for a ListBox.
The following code accomplishes what I need but isn't particularly efficient or elegant and can get muddled if you introduce multiple lists to check.
Any suggestions to duplicate this same behavior with better code would be appreciated.
Thanks!
var res = "palette {text: 'Example List', properties:{resizeable:true}\
pnl: Panel{orientation: 'row',preferredSize: [400,600],\
list1: ListBox{preferredSize: [400,550], properties:{multiselect:true,numberOfColumns:2, showHeaders:true,columnTitles: ['List 1', 'Subitem 0']}},\
}}"
var win = new Window(res)
win.show()
for (i=0; i<10;i++) {
var row = win.pnl.list1.add('item', "Same Entry")
row.subItems[0].text = "Subitem "+i}
win.pnl.list1.onChange = function() {
var tempArray = new Array()
for (i=0; i<win.pnl.list1.items.length;i++) {
if (win.pnl.list1.items.selected == true){tempArray.push(i)}
}
var selectedRow = win.pnl.list1.items[tempArray[0]].text
var selectedRowSubitem = win.pnl.list1.items[tempArray[0]].subItems[0]
alert("Selection: "+tempArray+"\n Displayed Contents: "+selectedRow+"\t"+selectedRowSubitem)
}
...in the mean time, this should be enough
win.pnl.list1.onChange = function() {
alert(this.selection.index); // for this to work set, multiselect:false
}
I guess you already tried and the result is "undefined", the reason is you have this property
multiselect:true
in that case, selection returns an array,
this.selection[0].index;
would give you the first selected item, even if you only have one item selected, to get all selected items loop thru all items in the selection array.
not sure if Peter's
...Copy link to clipboard
Copied
Check out Peter Kahrel's wonderful ScriptUI for Dummies
Copy link to clipboard
Copied
Ah, thanks for the link. I've been reading & re-reading the Tools Guide. A secondary resource will certainly be helpful in clarifying some of these items.
Copy link to clipboard
Copied
...in the mean time, this should be enough
win.pnl.list1.onChange = function() {
alert(this.selection.index); // for this to work set, multiselect:false
}
I guess you already tried and the result is "undefined", the reason is you have this property
multiselect:true
in that case, selection returns an array,
this.selection[0].index;
would give you the first selected item, even if you only have one item selected, to get all selected items loop thru all items in the selection array.
not sure if Peter's wonderful guide explains this (it is explained in the Tools Guide), but you should read it too, it has tons of great info.
Copy link to clipboard
Copied
Thanks Carlos,
I knew I was missing something obvious, but hadn't considered that with multiselect: true, it would return an array whether or not there were actually multiple items selected.
I definitely need to read through more UI related documentation, but for now my script is up and running!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now