I’m using a table with a SQLContainer and a FreeformStatementDelegate in order to do lazy loading.
Everything works fine, except that I can’t sort the table by clicking on one of the column headers.
If I click a column header, an arrow is displayed to indicate that the table is now sorted on that column, but the sort never happens.
By running in debug mode, I noticed that the following methods are executed:
table -> changeVariables(Object source, Map<String, Object> variables)
table -> setSortContainerPropertyId(id, false)
table -> this.sort()
table -> sort(new Object[] { sortContainerPropertyId }, new boolean[]
{ sortAscending });
SQLContainer -> sort(Object[] propertyId, boolean[]
ascending)
-> this adds a new sorter by calling sorters.add(new OrderBy((String) propertyId[i]
, asc));
SQLContainer -> refresh();
SqlContainer -> refresh(true);
SQLContainer -> fireContentsChange();
and finally:
MyFreeformStatementDelegate -> getQueryStatement(int offset, int limit);
By searching the forums, docs, examples, etc I found several indications that the FreeformStatementDelegate method should handle the sorting, but they all use something like this:
private String getOrderByString() {
StringBuffer orderBuffer = new StringBuffer("");
if (orderBys != null && !orderBys.isEmpty()) {
orderBuffer.append(" ORDER BY ");
OrderBy lastOrderBy = orderBys.get(orderBys.size() - 1);
for (OrderBy orderBy : orderBys) {
orderBuffer.append(Util.escapeSQL(orderBy.getColumn()));
if (orderBy.isAscending()) {
orderBuffer.append(" ASC");
} else {
orderBuffer.append(" DESC");
}
if (orderBy != lastOrderBy) {
orderBuffer.append(", ");
}
}
}
return orderBuffer.toString();
}
Therefore I also implemented this method and call the getOrderByString() method in the getQueryStatement() method, but the orderBys array never gets populated, resulting in an empty orderby string.
My getQueryStatement() method:
public StatementHelper getQueryStatement(int offset, int limit) throws UnsupportedOperationException {
StatementHelper sh = new StatementHelper();
StringBuffer query = new StringBuffer(formatQuery());
query.append(getOrderByString());
if (offset != 0 || limit != 0) {
query.append(" LIMIT ").append(limit);
query.append(" OFFSET ").append(offset);
}
sh.setQueryString(query.toString());
return sh;
}
}
So, how is it supposed to work / to be implemented ?
Thanks.
Guy