Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sort columns if media changes #544 #661

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions displaytag/src/main/java/org/displaytag/model/TableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public class TableModel {
/** The totaler. */
private TableTotaler totaler;

/**
* Column visibilities to apply sort column properly
*/
private List<Boolean> columnVisibilities = new ArrayList<>();

/**
* Constructor for TableModel.
*
Expand Down Expand Up @@ -487,21 +492,28 @@ public RowIterator getRowIterator(final boolean full) {
*/
private void sortRowList(final List<Row> list) {
if (this.isSorted()) {
final HeaderCell sortedHeaderCell = this.getSortedColumnHeader();

// If it is an explicit value, then sort by that, otherwise sort by the property...
if ((sortedHeaderCell != null) && (sortedHeaderCell.getBeanPropertyName() != null
|| this.sortedColumn != -1 && this.sortedColumn < this.headerCellList.size())) {

final String sorted = sortedHeaderCell.getSortProperty() != null ? sortedHeaderCell.getSortProperty()
: sortedHeaderCell.getBeanPropertyName();

Collections.sort(list, new RowSorter(this.sortedColumn, sorted, this.getTableDecorator(),
this.sortOrderAscending, sortedHeaderCell.getComparator()));
int oldSortedColumn = this.sortedColumn;
for (int i = 0; i < oldSortedColumn && i < columnVisibilities.size() && this.sortedColumn > 0; i++) {
if (!columnVisibilities.get(i)) {
this.sortedColumn--;
}
}
try {
final HeaderCell sortedHeaderCell = this.getSortedColumnHeader();

// If it is an explicit value, then sort by that, otherwise sort by the property...
if ((sortedHeaderCell != null) && (sortedHeaderCell.getBeanPropertyName() != null
|| this.sortedColumn != -1 && this.sortedColumn < this.headerCellList.size())) {
final String sorted = sortedHeaderCell.getSortProperty() != null ? sortedHeaderCell.getSortProperty()
: sortedHeaderCell.getBeanPropertyName();

Collections.sort(list, new RowSorter(this.sortedColumn, sorted, this.getTableDecorator(),
this.sortOrderAscending, sortedHeaderCell.getComparator()));
}
} finally {
this.sortedColumn = oldSortedColumn;
}

}

}

/**
Expand Down Expand Up @@ -636,4 +648,13 @@ public void reset() {
this.totaler.reset();
this.totaler.init(this);
}

/**
* Returns the column visibilities to apply sort column properly.
*
* @return The column visibilities.
*/
public List<Boolean> getColumnVisibilities() {
return this.columnVisibilities;
}
}
2 changes: 2 additions & 0 deletions displaytag/src/main/java/org/displaytag/tags/ColumnTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,10 @@ public int doEndTag() throws JspException {
if (ColumnTag.log.isDebugEnabled()) {
ColumnTag.log.debug("skipping column body, currentMediaType={}", currentMediaType);
}
tableTag.getTableModel().getColumnVisibilities().add(false);
return Tag.SKIP_BODY;
}
tableTag.getTableModel().getColumnVisibilities().add(true);

// add column header only once
if (tableTag.isFirstIteration()) {
Expand Down