Skip to main content
Inspiring
July 16, 2013
Answered

Determining the index of selected items in a ListBox?

  • July 16, 2013
  • 2 replies
  • 2242 views

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)

            

     }

This topic has been closed for replies.
Correct answer CarlosCanto

...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.

2 replies

CarlosCanto
Community Expert
CarlosCantoCommunity ExpertCorrect answer
Community Expert
July 16, 2013

...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.

Inspiring
July 19, 2013

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!

Larry G. Schneider
Community Expert
Community Expert
July 16, 2013

Check out Peter Kahrel's wonderful ScriptUI for Dummies

http://www.kahrel.plus.com/indesign/scriptui-2-1.pdf

Inspiring
July 19, 2013

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.