Dropdown Menu with Ditto

From MODx Wiki

Jump to: navigation, search


Here's a simple example how to use Ditto to generate a dropdown/select menu.

Imagine you have a container full of documents, and you need to build a dynamic dropdown-menu for these documents for easy navigation. Here's how:


a) Create a chunk named 'dropdown':

  1. <option value="[~[+id+]~]">[+pagetitle+]</option>

In case you didn't know by now, the special syntax [~[+id+]~] generates a link: Even if you use "SEO-friendly URLs", MODx will make sure it outputs the correct page-name for you.

b) In your document (or template) insert:

  1. <script language="JavaScript">
  2. function jumpTo(selObj){
  3. thevalue = selObj.options[selObj.selectedIndex].value;
  4. if (thevalue != 0) window.location= thevalue;
  5. return false
  6. }
  7. </script>
  8. <form action="#" onsubmit="return false" method="post">
  9. <select name="goto" onChange="jumpTo(this);">
  10. [!Ditto? &parents=`56` &tpl=`dropdown` &display=`100` &total=`100` &sortBy=`pagetitle` &sortDir=`ASC`!]
  11. </select>
  12. </form>


The small JavasScript bit is responsible for jumping to the selected URL. If you use it on every page, it makes sense to save it as an external file of course.

The rest is just the form-enclosure with a Ditto call that lists all documents of parent id 56, sorts in ASCending order by pagetitle, and uses our custom template 'dropdown'.

Usability Tip
For browsers not using JS and searchbots, you can (and should) create simple text-links inside <noscript></noscript> tags. ;Depending entirely on JS alone for navigation can be dangerous in this regard.
Personal tools