Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was able to extend <code>CellTable</code> to do exactly what I wanted basically by overriding the <code>onColumnSort(ColumnSortEvent)</code> method of the <code>ListHandler</code> associated with the table. This is the meat/potatoes of the implementation. I had to do a little maintenance under the covers to keep track of whether or not a column had been sorted when you are about to sort it again. </p> <p>One thing that confused me (and was not clear in google's examples) was that using the <code>ColumnSortList.push()</code> method does not actually trigger a sort like clicking does, but instead just changes the underlying state of how the column <em>thinks</em> it is sorted.</p> <p>When I was ready to utilize this table I made, I basically did the following:</p> <pre class="lang-java prettyprint-override"><code>SortedCellTable&lt;MyRowObject&gt; table = new SortedCellTable&lt;MyRowObject&gt;(); table.addColumn(colOne, "one", true); // sortable column table.addColumn(colTwo, "two", true); //sortable column table.addColumn(colThree, "three", false); // unsortable column table.setDefaultSortOrder(colOne, true); // sorts ascending on first click table.setDefaultSortOrder(colTwo, false); // sorts descending on first click table.setInitialSortColumn(colTwo); // sort this column as soon as data is set table.setComparator(colOne, colOneComp); // use this comparator when sorting colOne table.setComparator(colTwo, colTwoComp); // use this comparator when sorting colTwo panel.add(table); // add the table to our view // ...sometime later, asynchronously... table.setList(myRowObjectList); </code></pre> <p>Here is the <code>SortedCellTable</code> implementation:</p> <pre class="lang-java prettyprint-override"><code>public class SortedCellTable&lt;T&gt; extends CellTable&lt;T&gt; { /** * To keep track of the currently sorted column */ private Column&lt;T, ?&gt; currentlySortedColumn; /** * Tells us which way to sort a column initially */ private Map&lt;Column&lt;T, ?&gt;, Boolean&gt; defaultSortOrderMap = new HashMap&lt;Column&lt;T, ?&gt;, Boolean&gt;(); /** * Comparators associated with their columns */ private Map&lt;Column&lt;T, ?&gt;, Comparator&lt;T&gt;&gt; comparators = new HashMap&lt;Column&lt;T, ?&gt;, Comparator&lt;T&gt;&gt;(); /** * Column to sort when the data provider's list is refreshed using * {@link SortedCellTable#setList(List)} */ private Column&lt;T, ?&gt; initialSortColumn; /** * Data provider we will attach to this table */ private ListDataProvider&lt;T&gt; dataProvider; /** * Special column sorting handler that will allow us to do more controlled * sorting */ ListHandler&lt;T&gt; columnSortHandler; public SortedCellTable() { super(); dataProvider = new ListDataProvider&lt;T&gt;(); dataProvider.addDataDisplay(this); columnSortHandler = new ListHandler&lt;T&gt;(dataProvider.getList()) { @Override public void onColumnSort(ColumnSortEvent event) { @SuppressWarnings("unchecked") Column&lt;T, ?&gt; column = (Column&lt;T, ?&gt;) event.getColumn(); if (column == null) { return; } if (column.equals(currentlySortedColumn)) { // Default behavior super.onColumnSort(event); } else { // Initial sort; look up which direction we need final Comparator&lt;T&gt; comparator = comparators.get(column); if (comparator == null) { return; } Boolean ascending = defaultSortOrderMap.get(column); if (ascending == null || ascending) { // Default behavior super.onColumnSort(event); } else { // Sort the column descending Collections.sort(getList(), new Comparator&lt;T&gt;() { public int compare(T o1, T o2) { return -comparator.compare(o1, o2); } }); // Set the proper arrow in the header getColumnSortList().push( new ColumnSortInfo(column, false)); } currentlySortedColumn = column; } } @Override public void setComparator(Column&lt;T, ?&gt; column, Comparator&lt;T&gt; comparator) { comparators.put(column, comparator); super.setComparator(column, comparator); } }; addColumnSortHandler(columnSortHandler); } /** * Adds a column to the table and sets its sortable state * * @param column * @param headerName * @param sortable */ public void addColumn(Column&lt;T, ?&gt; column, String headerName, boolean sortable) { addColumn(column, headerName); column.setSortable(sortable); if (sortable) { defaultSortOrderMap.put(column, true); } } /** * Adds a column to the table and sets its sortable state * * @param column * @param header * @param sortable */ public void addColumn(Column&lt;T, ?&gt; column, Header&lt;?&gt; header, boolean sortable) { addColumn(column, header); column.setSortable(sortable); if (sortable) { defaultSortOrderMap.put(column, true); } } /** * Sets the column to sort when the data list is reset using * {@link SortedCellTable#setList(List)} * * @param column */ public void setInitialSortColumn(Column&lt;T, ?&gt; column) { initialSortColumn = column; } /** * Sets a comparator to use when sorting the given column * * @param column * @param comparator */ public void setComparator(Column&lt;T, ?&gt; column, Comparator&lt;T&gt; comparator) { columnSortHandler.setComparator(column, comparator); } /** * Sets the sort order to use when this column is clicked and it was not * previously sorted * * @param column * @param ascending */ public void setDefaultSortOrder(Column&lt;T, ?&gt; column, boolean ascending) { defaultSortOrderMap.put(column, ascending); } /** * Sets the table's data provider list and sorts the table based on the * column given in {@link SortedCellTable#setInitialSortColumn(Column)} * * @param list */ public void setList(List&lt;T&gt; list) { dataProvider.getList().clear(); if (list != null) { for (T t : list) { dataProvider.getList().add(t); } } // Do a first-time sort based on which column was set in // setInitialSortColumn() if (initialSortColumn != null) { Collections.sort(dataProvider.getList(), new Comparator&lt;T&gt;() { @Override public int compare(T o1, T o2) { return (defaultSortOrderMap.get(initialSortColumn) ? 1 : -1) * comparators.get(initialSortColumn) .compare(o1, o2); } }); // Might as well get the little arrow on the header to make it // official getColumnSortList().push( new ColumnSortInfo(initialSortColumn, defaultSortOrderMap .get(initialSortColumn))); currentlySortedColumn = initialSortColumn; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload