I sometimes feel like the answer to every question I get asked by customer is somewhere buried in this steaming heap of a weblog. In less than a month, this blog will be eight years old, and still requests are made for "new" features that existed in our Midas Rich Text LSX before I started the blog. Today, I got a question about sorting tables from the web. Of course, there are lots of ways to do that with views or lookups, but this customer has tables which are embedded in rich text fields, and wanted to export them to XHTML or DXL and then use fancy coding techniques to make them into lists, etc. etc. etc. I did a quick search on the blog, and came up with this handy bit of coding I posted back in 2006. It does just what the customer asked with a bit of Ajax and a simple agent.
Anyway, here it is, in all its original glory. Of course, Ajax is taken for granted these days, but this was quite the demo in 2006. And, of course, it is still something you can't really do with native Domino (at least not with rich text fields). And, of course, the code is even easier to make work in the Notes client. Incidentally, do you know when I added that SortByRow method to sort a table in a rich text field? Version 1.00 in 1997.
Table to be sorted using Midas and Ajax (for display):
Jammer, Jo Jo
Gatwick, George G.
Franklin, James Earl T.
Sort by column 1
Sort descending by column 1
Sort numerically by column 2
Sort numerically descending by column 2
Sort numerically by column 3
Sort numerically descending by column 3
So, what is happening in this demo? The agent code is really, really simple, so I thought I would just put it here in-line for you to look at:
Dim session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim rtitem As New GeniiRTItem
Dim rtchunk As GeniiRTChunk
Dim unid As String
Dim pos As Integer
Dim column As Integer
Dim reverse As Integer
Dim numeric As Integer
Set db = session.CurrentDatabase
Set doc = session.DocumentContext
pos = Instr(doc.Query_String(0), "&Sort=")
unid=Mid(doc.Query_String(0), pos+6, 32)
pos = Instr(doc.Query_String(0), "&Column=")
column = Cint(Mid(doc.Query_String(0), pos+8, 1))
pos = Instr(doc.Query_String(0), "&Numeric=")
numeric = Cint(Mid(doc.Query_String(0), pos+9, 1))
pos = Instr(doc.Query_String(0), "&Reverse=")
reverse = Cint(Mid(doc.Query_String(0), pos+9, 1))
Set doc = db.GetDocumentByUNID(unid)
Call rtitem.ConnectBackend(doc.Handle, "BlogBodyRT", True)
Set rtchunk = rtitem.DefineChunk("Table 1")
Call rtchunk.SortByRow(column, True, numeric, reverse)
Print rtchunk.GenerateHTML("Table 1", "HTML", "Generation='Fragment' StandardFontTags='Yes' ")
And that is it. Midas reads the contents of this blog post itself, sorts the table and then spits out the content again into the space occupied before by the table, all using Ajax. It never saves the changes, so dozens of people can try this at the same time without causing conflicts.
Of course there are other ways to accomplish this. There are other ways to accomplish anything. But ask anyone who has ever worked with the Midas Rich Text LSX, and they'll tell you, it's just plain easy and intuitive. Did you ever stop to think about using Midas with Ajax and Xpages in a modern website?
Copyright © 2011 Genii Software Ltd.
Tags: Lotus Notes