3 Replies Latest reply on May 26, 2015 8:39 AM by AL3XIO

    How to display a dynamic listbox with multiple columns

    AL3XIO

      Hi all.

      In my project I have a listbox that is populated dinamically through an array.

      var my_arr= [];
      .....
          var myListBox = add_list (win, my_arr);
              function add_list (container, my_arr) {
                  var list_data = container.add("listbox", undefined, my_arr, {multiselect: true, numberOfColumns: 1, showHeaders: true, columnTitles: ["TITLE"], columnWidths: [150]});
                  return list_data;
              }
      .....
      

       

      Now I need two show more info for each item , so I set showColumns = 2 and through .subItems I added the required info, and here start the problems.

      First problem : while running the code the data are shown as expected, but I can no longer operate over them.

      Second problem : if I run the whole code in a function I can no longer show the data.

       

      Here the code :

      #targetengine WorkInProgress
      
      my_arr = [];
      
      myTest();
      
      function myTest() {
          var win = new Window ("palette", "Two columns");
              var myButton = win.add("button", undefined, "Folder");
              var myListBox = add_list (win, my_arr);
                  function add_list (container, my_arr) {
                      var list_data = container.add("listbox", undefined, my_arr, {multiselect: true, numberOfColumns: 2, showHeaders: true, columnTitles: [" > BOOK CONTENTS", " INFO "], columnWidths: [150,90]});
                      return list_data;
                  }
                  myListBox.bounds = [0,0,250,250];
              var btn_group = win.add("group");
                  var up_btn = btn_group.add("button", undefined, "UP");
                  var dw_btn = btn_group.add("button", undefined, "DOWN");
              var quit = win.add("button", undefined, "CLOSE");
      
              myButton.onClick = function() {
                  the_book = File.openDialog ("");
                  if (myListBox.items.length > 0) {
                      my_arr = [];
                      upd_data(myListBox, my_arr);
                  }
                  getBookDocs(the_book);
              }
      
              up_btn.onClick = function () {
                  var selItems = [];
                  for (var c = 0; c < myListBox.selection.length; c++) {
                      if (myListBox.selection[c].selected) {
                          selItems.push(myListBox.selection[c].index);
                      }
                  }
                  selItems.sort();
                  for (var m = 0; m < selItems.length; m++) {
                      var movefrom = selItems[m];
                      var moveto = selItems[m] - 1;
                      if (moveto < 0) {
                          return;
                      } else {
                          my_arr.splice(moveto, 0, my_arr.splice(movefrom, 1)[0]);
                      }
                  }
                  upd_data(myListBox, my_arr);
                  for (var s = selItems.length - 1; s >= 0; s--) {
                      for (var a = 0; a < myListBox.items.length; a++) {
                          if (selItems[s] === myListBox.items[a].index) {
                              myListBox.items[a - 1].selected = true;
                          }
                      }
                  }
              };
      
              dw_btn.onClick = function () {
                  var selItems = [];
                  for (var c = 0; c < myListBox.selection.length; c++) {
                      if (myListBox.selection[c].selected) {
                          selItems.push(myListBox.selection[c].index);
                      }
                  }
                  selItems.sort();
                  for (var m = selItems.length - 1; m >= 0; m--) {
                      var movefrom = selItems[m];
                      var moveto = selItems[m] + 1;
                      if (moveto > my_arr.length - 1) {
                          return;
                      } else {
                          my_arr.splice(moveto, 0, my_arr.splice(movefrom, 1)[0]);
                      }
                  }
                  upd_data(myListBox, my_arr);
                  for (var s = selItems.length - 1; s >= 0; s--) {
                      for (var a = 0; a < myListBox.items.length; a++) {
                          if (selItems[s] === myListBox.items[a].index) {
                              myListBox.items[a + 1].selected = true;
                          }
                      }
                  }
              };
      
              quit.onClick = function() {
                  win.close();
              }
      
      
          win.center();
          win.show();
      }
      
          function getBookDocs(doc) {
              app.open(doc);
              var book_ref = app.activeBook;
              for (var x3 = 0; x3 < book_ref.bookContents.length; x3++) {
                  var full = book_ref.bookContents[x3].name;
                  var no_ext = full.substring(0,full.indexOf("."));
                  myListBox.add("item", no_ext);
                  if (book_ref.bookContents[x3].status === BookContentStatus.NORMAL) {
                      myListBox.items[x3].subItems[0].text = ""; // it's ok
                  } else if (book_ref.bookContents[x3].status === BookContentStatus.DOCUMENT_OUT_OF_DATE) {
                      myListBox.items[x3].subItems[0].text = "MOD"; // modified
                  } else if (book_ref.bookContents[x3].status === BookContentStatus.MISSING_DOCUMENT) {
                      myListBox.items[x3].subItems[0].text = "N_A"; // not available
                  } else if (book_ref.bookContents[x3].status === BookContentStatus.DOCUMENT_IS_OPEN) {
                      myListBox.items[x3].subItems[0].text = "A_O"; //already open
                  }
              }
              book_ref.close();
              return doc;
          }
      
          function upd_data(obj_data, arr_data) {
              obj_data.removeAll();
              var x2 = 0;
              while (obj_data.items.length < arr_data.length) {
                  obj_data.add("item", arr_data[x2]);
                  x2++;
              }
          }
      

      Any help is really appreciated.