XUL Tutorial - 7.5 - Tree Selection
Previous Contents Reference Next

XUL Tutorial - Tree Selection

The section will describe how to get and set the selected items in a tree.

Getting the Selected Tree Items

Each treeitem element in a tree may be selected individually. If you add the multiple attribute to the tree, the user can select multiple items at a time. The selection is not necessarily contiguous. The tree provides a number of functions which can be used to determine whether an item is selected.

The following examples will use the same tree example, which, because of its size, can be viewed separately. It contains three top-level items, each with a varying number of child items. It also allows multiple selection.

First, let's see how we can determine when an item is selected. The onselect event handler may be added to the tree element. When the user selects an item from the tree, the event handler is called. The handler is called if the user clicks on an item. The user may also change the selection using by using the cursor keys. If the user holds down the cursor key to rapidly scroll through the items, the event handler is not called until the user stops. This results in a performance improvement. This also means that highlight will appear on several items even though the select event is never fired for those items.

The syntax of the onselect event handler is shown below. You could modify the tree example linked to above to that below to experiment with tree selection.

<tree id="treeset" onselect="alert('You selected something!');">

The tree element has a property selectedItems which holds an array of all the selected items in the tree. This will include all selected items, no matter how deeply nested they are. If no items are selected, the array will have zero length.

Modify the tree example to that below. It will add a button which, when clicked, will display a list of the currently selected items.

<script src="treetest.js"/>

<button value="Show Selected" onclick="showSelected()"/>


<tree id="treeset" multiple="true" flex="1">

treetest.js:

function showSelected()
{
  var tree=document.getElementById('treeset');
  var items=tree.selectedItems;
  if (items.length==0) alert("No items are selected.");
  else {
    txt="You have selected:\n\n";
    for (t=0;t<items.length;t++){
      txt+=items[t].firstChild.firstChild.getAttribute('value')+'\n';
    }
    alert(txt);
  }
}

The function showSelected first gets a reference to the tree which can be found using its id. The variable items is assigned to the selectedItems property. If nothing is selected, an alert is displayed saying so. Otherwise, a string txt is created that contains the text of each item. The line that does this gets the first child of the first child of each item in the array. This is necessary to get a reference to the treecell from the treeitem. Then, we get the value of the value attribute, which holds the displayed text. Finally, outside the loop, we display an alert containing the created text.

Remember that the selection array does not distinguish between top-level items and items further down in the tree. For this, you can examine the items themselves, or you can look at the container attribute on the items.

Selecting Items

In addition to retreiving the selected items, we can also modify the selection. There are a variety of functions of the tree object that can be used for this.

Some of the functions described below require or return an index. This will be the position of the item with-in the tree. The items in the tree are numbered starting from 0. Only items that are displayed are included in the count. Thus, if an item with child items is collapsed so that the child items are hidden, the child nodes are not included in the count. If the item is expanded, the child nodes are included in the count.

Child items are included in the count just after their parents. This means that if there are 3 top-level items and each has two child items, there will be a total of 9 items. The first item (at index 0) will be the first top-level item. The next item at index 1 will be its first child. The second child will be at index 2 and the second parent item will be at position 3 and so on.

[Image of tree selection example]

In the image to the right, there are eight rows displayed, of which two are selected. The first selected row has an index of 4 and the second has an index of 7. The rows that are not displayed are not included in the index count.

The function getItemAtIndex can be used to get a reference to the treeitem at a particular index. The function takes one parameter, the index of the item you want to retreive. You can also retrieve treeitem references by looking them up by their ids. Once you have a treeitem, you can modify whether it is selected or not.

The selection functions allow you to have multiple items selected whether the multiple is used or not. This attribute controls only whether the user can select multiple items at once or not.

The following table lists the selection functions that can be used. These are all methods of the tree object, not of the treeitems.

addItemToSelection ( treeitem ) Adds the item to the current set of selected items. The selection state of other items is not changed.
removeItemFromSelection ( treeitem ) Unselects the item, without changing the state of other items.
selectItem ( treeitem ) Selects the item and unselects all currently selected items.
toggleItemSelection ( treeitem ) Toggles the selection state of the item. If the item is selected, it is unselected. If it is not selected, it is selected. Other items are not affected.
clearItemSelection ( ) Unselects all of the items in the tree.

The following example demonstrates how to select items from a tree. It adds a textfield and two buttons. Enter an index into the field and press the Select button. The item at that index will be selected. Press the Unselect button to unselect it. You can experiment with this by expanding and collapsing the items.

<box>
  <text value="Select item at index:"/>
  <textfield id="tfield"/>
  <button value="Select" onclick="doSelect()"/>
  <button value="Unselect" onclick="doUnselect()"/>
</box>

<tree id="treeset" multiple="true" flex="1">

treetest.js:

function doSelect()
{
  var tree=document.getElementById('treeset');
  var tfield=document.getElementById('tfield');
  var idx=parseInt(tfield.value);
  tree.addItemToSelection(tree.getItemAtIndex(idx));
}
  
function doUnselect()
{
  var tree=document.getElementById('treeset');
  var tfield=document.getElementById('tfield');
  var idx=parseInt(tfield.value);
  tree.removeItemFromSelection(tree.getItemAtIndex(idx));
}

Other Tree Functions

There are a number of other functions which can be used with trees. They are listed below as a short reference. For invalid items or indices, nothing happens.

getIndexOfItem ( treeitem ) Given an item, this function returns the index of the item. This value will be different depending on what items are displayed.
getItemAtIndex ( idx ) Given an index, this function returns the displayed treeitem at that index.
getNextItem ( startItem, delta ) Given an item startItem, the function will return an item following it. The parameter delta is used to specify the offset. If you use a value of 1, you can get the next item. A value of 5 will return the item 5 items afterwards. This function returns null if there is no such item.
getPreviousItem ( startItem, delta ) Given an item startItem, the function will return an item before it. The parameter delta is used to specify the offset. If you use a value of 1, you can get the previous item. This function returns null if there is no such item.
scrollToIndex ( idx ) Scrolls the view of the tree to an item at a particular index. This is useful for trees that have many items to move the view to a particular spot. The item is always placed at the top of the view, if possible.
ensureIndexIsVisible ( idx ) This function is similar to scrollToIndex except that no scrolling occurs if the item is already visible.
ensureElementIsVisible ( treeitem ) This function is similar to ensureIndexIsVisible except that its argument is a treeitem instead of an index.
getNumberOfVisibleRows ( ) Returns the number of rows that can be displayed in the current tree frame.
getIndexOfFirstVisibleRow ( ) Returns the index of the item that is displayed at the top of the visible set of rows. This is useful to determine where in a long list the view is positioned.

(Next) Next, we'll find out how to use observers.

XUL Tutorial - 7.5 - Tree Selection
Previous Contents Reference Next