Sorting Problem when using HtmlDataTable
Hello,
In my table, I am displaying a list of homegrown, POJOs that represent users. Each row displays some info about the user as well as several command links for performing certain actions (like showing details, for instance). The sorting criteria is based upon whichever column is selected (the column names are being displayed via command links).
The sorting for my list of users is working fine with the exception of the command links per row. That is, if I have the following:
Last NameFirst Name
Conners JohnShow Details
Conners AmyShow Details
Say I select the "Show Details" link for the first row. Details for John Conners are displayed. Good. Now I decide to sort the same list by First Name so I get the following:
Last NameFirst Name
Conners AmyShow Details
Conners JohnShow Details
The problem now is that when I select "Show Details" for the first row, information for John Conners appears. No matter what I do, it appears that the links in each row remain associated with whatever objects are displayed for that row the very first time the table is built.
How can I get the command links to "know" that their associated objects have changed after performing a sort routine?
Any ideas?
Jeff
[1291 byte] By [
marendoja] at [2007-10-3 7:29:36]

Solution:
The problem was related to my usage of the list in conjunction with the code in my backing bean. Initially I was storing my list in the request scope. Basically, what was happening was I'd sort the list based upon a column that was selected. However, before the response was committed, I was retrieving the same list again--only in its previous, unordered state.
Now I have a less bulky (compared to what it could be) list that I store in the session. After I sort, I make sure to update the list in the session so each link in each row of the data table stay associated with the correct data.
Here's the sort method:
public void sort(ActionEvent actionEvent) {
sortColumn = (ColumnType) ColumnType.getEnumManager().getInstance(
actionEvent.getComponent().getId());
logger.debug("Attempting to sort by " + sortColumn);
Comparator c = null;
// Sort method will depend upon the column type
switch (sortColumn.getValue()) {
case ApplicationConstants.USER_ID_COLUMN:
c = new BeanPropertyComparator(ApplicationConstants.ID);
Collections.sort((List) this.getSearchResults(), c);
break;
case ApplicationConstants.LAST_NAME_COLUMN:
c = new BeanPropertyComparator(ApplicationConstants.LAST_NAME);
Collections.sort((List) this.getSearchResults(), c);
break;
case ApplicationConstants.FIRST_NAME_COLUMN:
c = new BeanPropertyComparator(ApplicationConstants.FIRST_NAME);
Collections.sort((List) this.getSearchResults(), c);
break;
default:
break;
}
/*
* Update the object stored in the session so the sort order will be
* retained between requests.
*/
facesContext.getApplication().createValueBinding(
"#{sessionScope.searchResults}").setValue(facesContext,
this.getSearchResults());
}
The search results are initialized from a previous search page and its backing bean. The results are passed as a managed property to the bean containing the sort method.
Some snippets of my edit page that calls the sorting method is below:
<h:dataTable id="userTable" styleClass="table-background"
rowClasses="table-odd-row,table-even-row" cellpadding="5"
value="#{pc_Search_user_results.searchResults}" var="searchResult"
binding="#{pc_Search_user_results.userTable}">
<h:column>
<f:facet name="header">
<h:commandLink styleClass="table-header" id="userId"
actionListener="#{pc_Search_user_results.sort}">
<h:outputText value="User ID" />
</h:commandLink>
</f:facet>
<h:outputText value="#{searchResult.id}" />
</h:column>
<h:column>
<h:commandLink id="editLink" action="#{pc_Search_user_results.editUser}">
<h:outputText value="Edit" />
</h:commandLink>
</h:column>
This is a long post, but I thought I'd try to explain for those who attempted to help as well as others.
Jeff