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());
    }
} 



-------------------------------------------------------
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&kid0944&bid$1720&dat1642
_______________________________________________
Qooxdoo-devel mailing list
Qooxdoo-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel

Reply via email to