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

Determining the index of selected items in a ListBox?

Explorer ,
Jul 16, 2013 Jul 16, 2013

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)

            

     }

TOPICS
Scripting
2.2K
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

correct answers 1 Correct answer

Community Expert , Jul 16, 2013 Jul 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

...
Translate
Adobe
Community Expert ,
Jul 16, 2013 Jul 16, 2013

Check out Peter Kahrel's wonderful ScriptUI for Dummies

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

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
Explorer ,
Jul 19, 2013 Jul 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.

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 16, 2013 Jul 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.

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
Explorer ,
Jul 19, 2013 Jul 19, 2013
LATEST

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!

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