Next / Last functionality using fields

I have a bunch of tiddlers that are numbered consecutively in one of the fields. I’d like to link to the next and last tiddler in that field’s sequence on each tiddler’s page, how might I go about that?

Try this:

Create a tiddler (e.g., “NavSeq”), tagged with $:/tags/ViewTemplate, containing:

\define go(label)
<$button> $label$
   <$action-navigate/>
   <$action-sendmessage $message="tm-close-tiddler" $param=<<here>>/>
</$button>
\end

<$list filter="[<currentTiddler>has[seq]]" variable="here">
<$set name=tids filter="[has[seq]sort[seq]]">
<$list filter="[enlist<tids>first[]!match<here>]"><<go first>></$list>
<$list filter="[enlist<tids>before<here>]"       ><<go prev>></$list>
<$list filter="[enlist<tids>after<here>]"        ><<go next>></$list>
<$list filter="[enlist<tids>last[]!match<here>]" ><<go last>></$list>

Notes:

  • I have assumed that the “sequence number” for each tiddler is in a field named “seq
  • Tagging the “NavSeq” tiddler with $:/tags/ViewTemplate causes it to be rendered at the bottom of EVERY tiddler.
    • Note that the first $list widget checks if the current tiddler has a non-blank “seq” field so that the rest of the code is only rendered for tiddlers that have a “seq” field
    • This widget also saves the title of the current tiddler in a variable named “here”, so that we can use that title later (see below).
  • The go() macro defines a button to perform the “navigation” actions:
    • <$action-navigate/> opens the tiddler to be viewed
    • The tm-close-tiddler message closes the current tiddler
  • The $set widget gets a list of all tiddlers that have a “seq” field, and sorts that list in ascending order by sequence number.
  • The remaining 4 $list widgets show navigation buttons for “first”, “prev”, “next”, and “last”
    • Each $list widget finds the appropriate tiddler title within the the <tids> list, relative to the current tiddler title, (which we previously stored in the “here” variable).
    • Note that the “first” and “last” filters also makes sure we are not currently viewing the first or last tiddler. If we are, then that button will not be rendered

Let me know how it goes…

enjoy,
-e

6 Likes

Incredible, works perfectly. Just had to change sortby to sort. Thank you!

1 Like