Tree

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Expanding/Collapsing all tree nodes

To expand all tree nodes:

 var treeView = tree.treeBoxObject.view;
 for (var i = 0; i < treeView.rowCount; i++) {
   if (treeView.isContainer(i) && !treeView.isContainerOpen(i))
     treeView.toggleOpenState(i);
 }

To collapse all tree nodes just change the condition:

 var treeView = tree.treeBoxObject.view;
 for (var i = 0; i < treeView.rowCount; i++) {
   if (treeView.isContainer(i) && treeView.isContainerOpen(i))
     treeView.toggleOpenState(i);
 }

Getting the text from the selected row

Assuming the given <tree>:

 <tree id="my-tree" seltype="single" onselect="onTreeSelected()">

Use the following JavaScript:

 function onTreeSelected(){
   var tree = document.getElementById("my-tree");
   var cellIndex = 0;
   var cellText = tree.view.getCellText(tree.currentIndex, tree.columns.getColumnAt(cellIndex));
   alert(cellText);
 }

Getting the tree item from the focused row

Assuming <tree id="my-tree">, you can use the following to get the tree item:

var view = document.getElementById("my-tree").view;
var sel = view.selection.currentIndex; //returns -1 if the tree is not focused
var treeItem = view.getItemAtIndex(sel);

Note that the current index may be unselected (for example, a multi-select tree).

Getting the cell from a mouse click

Your first choice is likely to try <treecell onclick="yourfunc();"/> or something similar. But this will not work. You can't add event handlers to <treecell>. Instead, you can add the event handler to the <tree> element. You can then use the event and some utility methods to find the <treecell>. For example, assuming the given <tree>:

 <tree id="my-tree" onclick="onTreeClicked(event)">

Use the following JavaScript:

function onTreeClicked(event){
  var tree = document.getElementById("my-tree");
  var tbo = tree.treeBoxObject;
  // get the row, col and child element at the point
  var row = { }, col = { }, child = { };
  tbo.getCellAt(event.clientX, event.clientY, row, col, child);
  var cellText = tree.view.getCellText(row.value, col.value);
  alert(cellText);
}

Getting the selected indices of a multiselect tree

  var start = {}, end = {}, numRanges = tree.view.selection.getRangeCount(), selectedIndices = [];
  for (var t = 0; t < numRanges; t++){
    tree.view.selection.getRangeAt(t, start, end);
    for (var v = start.value; v <= end.value; v++)
      selectedIndices.push(v);
  }

Other resources

Document Tags and Contributors

Tags: 
 Last updated by: bunnybooboo,