Sunday July 6, 2008

Sorting Lists in TWiki

Where I work, we have started a TWiki page for "new people" in the department. People are added to the page as they join the group. Each person has a section for a mini-bio and other information. There is a table of contents (TOC) at the top.

The problem: as the page gets larger, it's more difficult to find the entry for a given person. The TOC is ordered by when the person joined. It would be convenient to be able to sort the TOC alphabetically (it's just a list, after all)

I started looking for ways to sort a list in TWiki. As long as I was at it, I figured I might as well look for a generalizable solution that can sort any bullet list.

I looked at SpreadSheetPlugin but the sort functions want table cells. No tables here.

I figured JavaScript could solve the problem. I did a web search for JavaScript that sorts lists and found a very promising script written by Bruno Bornsztein (blog.feedmarker.com).

The script worked well on simple bullet lists but needed some work to modify it for a TOC. In particular, the script was finding the list by ID and TWiki TOCs don't have a unique ID :-( However, they do have a unique class :-) (providing, of course, that there is only one TOC on the page!)

I modified the JS to handle classes (using the named class to set an id on the list in question) and got it working. Then I handed it to Rich for review and he said: you should put in a way to revert to the original order.

Well, duh. That was a bit trickier. I could just reload the page - but that's time consuming and would revert every list on the page.

So I asked around, got some help**, and learned a few new JS tricks. Now lists are revertible to initial order as well as sortable in either ascending or descending order.

You can see a detailed writeup, with code and working examples, in my TWiki at TWikiTutListSort.


** Many thanks go to Kevin Frost, David Scott and others on the SF Perl User Group mailing list for their invaluable assistance.

Sorting Lists in TWiki ( in category Computerware , Show & Tell ) - posted at Sun, 06 Jul, 11:07 Pacific | «e»


Post a comment

Any posted comments will be viewable by all visitors. Please try to stay relevant ;-) If you simply want to say something to me, please send me email.

All comments will be moderated. Thank you for your consideration.