7

我有一个 GWT CellTable,其中的可排序列与开发人员指南示例( http://code.google.com/webtoolkit/doc/latest/DevGuideUiCellTable.html#columnSorting )非常相似。

但是,我希望某些列默认降序而不是升序。也就是说,如果 A 列当前未排序并且我单击它的标题,我希望 A 列在第一次单击时按降序排序,在第二次单击时按升序排序。但我仍然希望其他列像当前那样排序 - 在第一次点击时使用升序。

除了设置可排序性的 Column.setSortable(boolean) 方法和手动触发排序的 ColumnSortList.push(ColumnSortInfo) 方法之外,似乎对过程没有太多控制。

有没有办法实现这个目标?

4

4 回答 4

7

Column.setDefaultSortAscending(false)是做到这一点的简单方法;无需自定义CellTable

于 2012-08-25T20:46:58.727 回答
5

通过覆盖与表关联的方法,我基本上可以扩展CellTable为完全符合我的要求。这是实施的肉/土豆。我不得不在幕后进行一些维护,以跟踪当您要再次对列进行排序时是否已对其进行排序。onColumnSort(ColumnSortEvent)ListHandler

让我感到困惑的一件事(在 google 的示例中并不清楚)是使用该ColumnSortList.push()方法实际上并不会像单击那样触发排序,而只是改变了列认为它是如何排序的底层状态。

当我准备好使用我制作的这张桌子时,我基本上做了以下事情:

SortedCellTable<MyRowObject> table = new SortedCellTable<MyRowObject>();
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);

这是SortedCellTable实现:

public class SortedCellTable<T> extends CellTable<T> {
/**
 * To keep track of the currently sorted column
 */
private Column<T, ?> currentlySortedColumn;
/**
 * Tells us which way to sort a column initially
 */
private Map<Column<T, ?>, Boolean> defaultSortOrderMap = new HashMap<Column<T, ?>, Boolean>();
/**
 * Comparators associated with their columns
 */
private Map<Column<T, ?>, Comparator<T>> comparators = new HashMap<Column<T, ?>, Comparator<T>>();
/**
 * Column to sort when the data provider's list is refreshed using
 * {@link SortedCellTable#setList(List)}
 */
private Column<T, ?> initialSortColumn;
/**
 * Data provider we will attach to this table
 */
private ListDataProvider<T> dataProvider;
/**
 * Special column sorting handler that will allow us to do more controlled
 * sorting
 */
ListHandler<T> columnSortHandler;

public SortedCellTable() {
    super();
    dataProvider = new ListDataProvider<T>();
    dataProvider.addDataDisplay(this);
    columnSortHandler = new ListHandler<T>(dataProvider.getList()) {

        @Override
        public void onColumnSort(ColumnSortEvent event) {
            @SuppressWarnings("unchecked")
            Column<T, ?> column = (Column<T, ?>) 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<T> 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<T>() {
                        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<T, ?> column,
                Comparator<T> 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<T, ?> 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<T, ?> column, Header<?> 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<T, ?> column) {
    initialSortColumn = column;
}

/**
 * Sets a comparator to use when sorting the given column
 * 
 * @param column
 * @param comparator
 */
public void setComparator(Column<T, ?> column, Comparator<T> 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<T, ?> 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<T> 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<T>() {

            @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;
    }
}
}
于 2012-03-01T13:04:01.290 回答
2

看看GWT CellTable 列排序。(有人投票否决,因为他/她不同意我的做法?)

这不是您问题的答案,但它主张您在尝试使用 celltable 示例说明的任何内容之前先了解 Celltable 的基础知识。

通过了解单元格/网格表的基本行为和要求,列的排序将清楚地体现出来。

排序是通过您提供比较器的实现来执行的。就我而言,我尝试了几种实现比较器的方法,以适应我想要对列进行排序的各种方式。

于 2012-03-01T04:33:08.660 回答
1

代替

someTable.getColumnSortList().push(provider.getDefaultSortColumn());

利用

someTable.getColumnSortList().push(new ColumnSortInfo(provider.getDefaultSortColumn(), false));
于 2012-12-06T18:39:57.333 回答