Hello Jason,

I hope these functions might help somebody.  Sometimes you want to
know the index of the selected item, or you might want to select the
n-th item in the list.

These functions work well with combobox and treeview.  They  should
work with listview, too, since it uses a SelectionManager, but I have
not tested it.

/**
* Gets the selected index for a combobox or treeview
* returns -1 if nothing is selected.
*/
function getSelectedIndex (xwidget)
{
var mgr = xwidget.getManager();
if (mgr.getSelectedItem() == null)
{
return -1;
}
var items = mgr.getItems();
var i;
for (i = 0; i < items.length; i++)
{
if (items[i] == mgr.getSelectedItem())
{
return i;
}
}
return -1;
}
/**
* Gets the selected item by index for a combobox or treeview
* You can pass "-1" to deselect all items, and if you pass an
* index that is larger than the number of items (minus 1), the
* last item will be selected.
*/
function setSelectedIndex (xwidget, idx)
{
var mgr = xwidget.getManager();
if (idx < 0)
{
mgr.setSelectedItem (null);
return;
}
var items = mgr.getItems();
if (items.length == 0)
{
mgr.setSelectedItem (null);
return;
}
var selitem = null;
if (idx > items.length - 1)
{
idx = items.length - 1;
}
selitem = items[idx];
mgr.setSelectedItem (selitem);
// we need to set the value of a QxComboBox explicitly to change
// the value that is displayed.
if (xwidget.setValue != null)
{
xwidget.setValue(selitem.getLabel());
}
}


Hi, it looks good indeed but for some reason it doesnt work for me it allways came up with an empty list whe executes mgr.getItems(); so i have changed the code a lill bit to reflect the beahavior in the examples...

This is my code to do the same and i added a function to set the selected item by value:

/**
* Gets the selected item by index for a combobox or treeview
* You can pass "-1" to deselect all items, and if you pass an
* index that is larger than the number of items (minus 1), the
* last item will be selected.
*/
function setSelectedIndex (xwidget, idx)
{
   var mgr = xwidget.getList();
   if (idx < 0)
   {
   xwidget.setSelected(null);
   return;
   }

   var items = mgr.getChildren();
   if (items.length == 0)
   {
   xwidget.setSelected(null);
   return;
   }

   var selitem = null;
   if (idx > items.length - 1)
   {
   idx = items.length - 1;
   }

   selitem = items[idx];
   xwidget.setSelected (selitem);

   // we need to set the value of a QxComboBox explicitly to change
   // the value that is displayed.
   if (xwidget.setValue != null)
   {
   xwidget.setValue(selitem.getLabel());
   }
}

/**
* Selects an item by its value
*/
function setSelectedItemByValue(xwidget, value)
{

   var items = xwidget.getList().getChildren();
   if (items.length == 0)
   {
   xwidget.setSelected(null);
   return;
   }

   var selitem = null;

   for (var i = 0; i < items.length; i++)
   {
       if (items[i].getValue() == value)
       {
           selitem = items[i];
           xwidget.setSelected(selitem);
           // we need to set the value of a QxComboBox explicitly to change
           // the value that is displayed.
           if (xwidget.setValue != null)
           {
               xwidget.setValue(selitem.getLabel());
           }
       }
   }
}





-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Qooxdoo-devel mailing list
Qooxdoo-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to