I was searching for a work around to allow view column to be sortable by clicking on column header but I was not able to find any reliable solution using View Panel. Now I found a way to provide this feature using Repeat Control or Data Table.
Please go through the following code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
var loView:NotesView = null;
var loEntryColl:NotesViewEntryCollection = null;
var loTreeMap:java.util.TreeMap = new java.util.TreeMap();
var lsSortBy = viewScope.get("sortBy");
try {
loView = database.getView("view");
if(null == lsSortBy || "" == lsSortBy) {
lsSortBy = 0;
}
if(null != loView && loView.getEntryCount() > 0) {
loEntryColl = loView.getAllEntries();
var loEntry:NotesViewEntry = loEntryColl.getFirstEntry();
var loTempEntry:NotesViewEntry = null;
while(null != loEntry) {
loDoc = loEntry.getDocument();
var lsKey = loEntry.getColumnValues().get(lsSortBy);
while(loTreeMap.containsKey(lsKey)) {
lsKey += "~";
}
loTreeMap.put(lsKey, loEntry.getColumnValues());
loTempEntry = loEntryColl.getNextEntry(loEntry);
loEntry.recycle();
loEntry = loTempEntry;
}
}
} catch(e) {
print("Error : " + e);
} finally {
if(null != loView) loView.recycle();
if(null != loEntryColl) loEntryColl.recycle();
}
return loTreeMap.values(); |
In line 3 I am defining a TreeMap data structure. By definition TreeMap is sorted according to its natural ordering of its keys.
Since the data structure is always sorted so we can insert NotesViewEntry column values to the TreeMap and the corresponding column value (by which the view needs to be sorted) should be passed as key.
Look at line no 17 I am forming the key by which the view needs to be sorted. By default it is first column value.
At this point of time the only thing which creates problem is the duplicate values of the key. In TreeMap the value is overwritten if the key is already existing.
From line 18-21 I am appending additional character to make the key unique.
In line 37 I am returning value set from the TreeMap which is already sorted by key.
Rest of the code is quite easy to understand. I hope it may help in your application.
Now that’s a nice piece of code you got there. I liked the way you have used finally. Not many developers do that (including me!).
But sorry for being a party pooper, have some questions:
1. How should this hold against 10,000+ documents. Looping through all those documents would certainly bring a performance hit as I don’t see any use full-text search?
2. As you said, to handle duplicate entries you have made provisions in code. But I think you didn’t took into account if there are 3 or more duplicate entries.
3. What is “var i=0″ doing on line number 18? Please don’t kill me for this one…
Thanks Naveen for your nice comment!! Good programming practices must be followed to achieve better performance.
1. You raised very valid point point but I never found any web application in which 10,000+ documents are being displayed at a time. Above code can be optimised further to suit your business needs.
2. There is while loop on line 18 so if there are 3 or more duplicate entries, they are handled already. Instead of staring at line 18 you should have read line 19
3. There is no use of “var i=0″. I removed it. Thanks for correcting me
#2 Whoops…. my bad! :-}
articles doesn’t necessarily need too much words to be good, and yours amazing.