Index

How do I sort content?

You can sort data as long as it is generated from RDF. It is most common to sort content in a tree, however you can sort any template-generated content.

You can use the sortResource attribute to specify a resource to sort by. Use the sortDirection attribute to specify the direction to sort by. Both attributes must be present to have sorted data.

The following example demonstrates sorting content:

View
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<window id="sort-window" title="Sort Example"
   xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script>
function changeSort()
{
  // get the box to sort
  var box = document.getElementById("info");
  // if the sort direction is descending, change it to ascending.
  // Otherwise, set it to ascending.
  if (box.getAttribute("sortDirection") == "descending")
    box.setAttribute("sortDirection","ascending");
  else box.setAttribute("sortDirection","descending");
  // you must rebuild the content after changing sort information
  box.builder.rebuild();
}
</script>
<button label="Reverse" oncommand="changeSort();"/>
<vbox id="info" datasources="animals.rdf"
      ref="http://www.some-fictitious-zoo.com/mammals" flex="1"
      sortResource="http://www.some-fictitious-zoo.com/rdf#name" sortDirection="ascending">
  <template>
    <label uri="rdf:*" value="rdf:http://www.some-fictitious-zoo.com/rdf#name"/>
  </template>
</vbox>
</window>

In this example, data is sorted by name, as indicated by the sortResource attribute. However, any property may be used, even if it isn't displayed.

Copyright © 1999 - 2005 XULPlanet.com