XPage – Sort View Column

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.
[javascript]
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();
[/javascript]

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.

Optimization of the above code is discussed in next blog.

10 thoughts on “XPage – Sort View Column

  1. 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… ;)

  2. 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 :P :D

    3. There is no use of “var i=0″. I removed it. Thanks for correcting me :)

  3. Hi!
    Thanks for posting this useful code snippet above. I cannot seem to get it working with my data table however. I have modified your code to reference your line#12:loEntryColl = mycolname1.getAllEntries();
    where mycolname1 is the data table’s collection.
    I have placed sortBy on the Xpage inside the data table column as a combobox with my sort values (Name & Age) and referenced it as a viewscope variable. I have placed your code with my modifications in the onChange event of the combobox and partial updating the datatable. Not sure what I might be doing wrong. It almost seems nothing is happening. i don’t get any errors either. Perhaps I am not using the code as it is intended.
    Thanks for your insight in advance.
    Shabana.

  4. Hi Shabana,
    “sortBy” is a viewScope variable not a field and it contains column number. On line 23 I am putting the value of that particular column as key in the treemap.
    In the header row of the datatable you can put a link and on click of that link you set this “sortBy” viewScope variable and update the data table partially.

    I did not understand why are you using combobox and what is its relation with sorting.

  5. Hi Rashid!

    Thanks for replying to my post. I am using the combobox as a store for sort by category values. Hence, I am not sure if your code is meant for such usage. Perhaps it won’t work? Essentially I am trying to dynamically sort the entire data table by category keys specified in the combobox. My sortBy is held in a combobox and referenced asa viewscope variable varname sortBy and I am getting the value the user enters in that combobox to perform the sort. Does that help? Do you have any suggestions on the best way of accomplishing such a category sort in XPage data table?
    Thanks in advance!
    Shabana.

    1. Hi Shabana,
      Let me explain as per my understanding of your problem. The order of columns in the backend view and the data table must be same.
      1. create a notes view from which the documents will come in the data table.
      2. create a data table on the xpage and write the above code in the iterator of the data table
      3. let us assume you have three columns(col1, col2 and col3) int data table. So in you combobox the value list would be [‘col1|1′, ‘col2|2′, col3|3]. Pipe sign(|) is used for alias value.
      4. On change event of the combo box set viewScome variable “sortBy” and update the data table partially. It will set this variable as 1, 2 or 3.
      5. Your data table will be sorted as per the column number selected.

      If still you have issues then share your implemented code. I will be happy to help you out :)

  6. Rashid,
    Yes, everything per your mention above is accurate. I did not have the alias on the sortBy, which I have since added. Now, I get an empty data table. I added the unmodified code above to the iteration code and I do not get any values returned in the data table. I also DO NOT have any computed fields on my data table now. If I need them, I am not sure how I would bind them to the data source? My code attached below:
    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(“MyName”);
    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();

    You can test drive it here: http://bit.ly/VHOb1J
    THANKS !!
    Shabana.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">